使用sed在目录的每个脚本中用$ dir(replace / with $)替换每次出现的/ dir。
sed“s#/ dir#$ dir #g”
$被解释为函数或变量调用。 有办法解决这个问题吗?
感谢
答案 0 :(得分:1)
阅读shell的友好手册:
man sh
在shell中,文本周围的“双引号”允许内部的变量解释,而“单引号”则不允许,这是后来的语言(如Perl和PHP)(但不是JavaScript)采用的约定。
sed 's#/dir#$dir#g' *
要在脚本中执行替换,请执行
之类的操作find * -maxdepth 0 -type f | while read f; do mv $f $f.old && sed 's#/dir#$dir#' $f.old > $f; done
或只是
perl -pi.old -e 's#/dir#\$dir#' * # Perl also interpolates variables in s commands
答案 1 :(得分:0)
您可以使用反斜杠转义它:
sed "s#/dir#\$dir#g"
答案 2 :(得分:0)
shell方法
for file in file*
do
if [ -f "$file ];then
while read -r line
case "$line" in
*/dir* ) line=${line///dir/\$dir}
esac
echo $line > temp
done < "file"
mv temp $file
fi
done