我有一个函数可以读取文件,并在特定行执行在文件中该行找到的命令。该函数似乎工作正常,直到我尝试向一行添加 sed 命令,然后遇到了麻烦。当执行 sed 命令时,我看到以下错误:
sed: -e expression #1, char 1: unknown command `''
在我执行命令的文件中,sed 表达式是:
sed -i 's# libcrypto.a##;s# libssl.a##' Makefile
起初,我认为按照说明 here 可以解决该问题。 。但是,在尝试将文件中的行重写为 bash -c "sed -i 's# libcrypto.a##;s# libssl.a##' Makefile"
后,我收到以下错误:
-i: -c: line 0: unexpected EOF while looking for matching `"'
-i: -c: line 1: syntax error: unexpected end of file
应该执行文件中这些行的函数如下所示:
OLD_IFS=$IFS
IFS=$'\n'
LABEL=($( cat /etc/file/containing/lines/to/execute ))
IFS=$OLD_IFS
for ((i=3; i <= $((${#LABEL[@]} - 1)); i++)); do # Start at i=3 to avoid three lines of comments at the beginning of the file.
. . .
if [[ "${LABEL[i]}" == some criteria ]]; then
exec ${LABEL[i]} &> /dev/null &
fi
done
我做错了什么?
谢谢!
最佳答案
我有两个解决方案。
第一个,在你的文件中写入
sed -i s#libcrypto.a##;s#libssl.a## Makefile
没有顶点和空格,在编辑后的Makefile
中,您将得到两个无害的,我的意思是无害的额外空格。
第二个是修改您的函数,即从文件中执行行的函数,并使用 eval $line
而不是简单地使用 $line
(我的猜测是您的函数从文件中读取一行,将结果存储在变量 line
中,如
while read line ; do
...
done < the_file_with_the_sed_command
关于linux - 从 bash 脚本调用时,Sed 命令无法正确执行,从提示符调用时工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28489079/