SED命令不是从bash脚本运行

编程入门 行业动态 更新时间:2024-10-12 20:26:47
本文介绍了SED命令不是从bash脚本运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经写了这一个文件调用sed命令(其中包括)一个bash脚本来完成一个查找/ 2个不同的字符串替换。

I have written a bash script which calls a sed command (amongst other things) on a file to complete a find/replace of 2 different strings.

麻烦的是,运行该脚本后,我查看这些文件并没有什么已更新。但是,如果我运行正在生成的命令(我赞同他们作为输出反正),那么他们的工作。

The trouble is, after running the script, I check the files and nothing has been updated. However, if I run the commands that are being produced (I echo them as output anyway) then they work.

例如,脚本我有里面:

echo "/usr/local/bin/sed -i -e 's/${String1}/${String1R}/g;s/\/${String2}\//\/${String2R}\//g' ${ROOT_DIR}/data/file.sql" /usr/local/bin/sed -i -e 's/${String1}/${String1R}/g;s/\/${String2}\//\/${TString2R}\//g' ${ROOT_DIR}/data/file.sql

运行脚本不会file.sql变化;但是,如果我运行打印到控制台命令如在/ usr / local / bin目录/ SED -i -e'S / file_name1 / file_name2 / G; S // /// path_substring1 // path_substring2 G'!/path/to/file/file.sql它完美的作品。

Running the script does not change file.sql; however, if I run the command that is printed to console e.g. /usr/local/bin/sed -i -e 's/file_name1/file_name2/g;s//path_substring1///path_substring2//g' /path/to/file/file.sql it works perfectly!

推荐答案

使用双引号,而不是单引号。单引号将prevent变量扩展。

Use double quotes instead of single quotes. Single quotes would prevent variable expansion.

/usr/local/bin/sed -i -e "s/${String1}/${String1R}/g;s/\/${String2}\//\/${TString2R}\//g" ${ROOT_DIR}/data/file.sql

此外,似乎你的变量是可能包含正斜杠,即路径字符串 / 。在这种情况下使用不同的分离器:

Moreover, it seems that your variables are path strings which might contain forward slashes, i.e. /. In that event use a different separator:

"s|${String1}|${String1R}|g"

使用不同的分隔将在消除模式和替换逃逸 / 的需要。

更多推荐

SED命令不是从bash脚本运行

本文发布于:2023-10-09 05:36:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是从   脚本   命令   SED   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!