SED命令不是从bash脚本运行 [英] SED command not being run from bash script

查看:132
本文介绍了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脚本运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆