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

查看:24
本文介绍了SED 命令不是从 bash 脚本运行的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 bash 脚本,它在文件上调用 sed 命令(除其他外)以完成对 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!

推荐答案

使用双引号代替单引号.单引号会阻止变量扩展.

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"

使用不同的分隔符可以避免在模式和替换中对 / 进行转义.

Using a different separator would obviate the need of escaping / in the pattern and replacement.

这篇关于SED 命令不是从 bash 脚本运行的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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