我已经写了这一个文件调用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脚本运行
发布评论