简体   繁体   中英

Sed command does not execute correctly when called from bash script and works fine when called from prompt

I have a function that reads through a file and, at specific lines, executes the command found on that line in the file. The function seemed to be working OK until I tried adding a sed command to a line, and then I ran into trouble. When the sed command should execute, I see the following error:

sed: -e expression #1, char 1: unknown command `''

In the file that I am executing commands from, the sed expression is:

sed -i 's# libcrypto.a##;s# libssl.a##' Makefile

At first, I thought that the issue could be resolved by following the instructions here . However, after trying to rewrite the line in the file as bash -c "sed -i 's# libcrypto.a##;s# libssl.a##' Makefile" , I received the following error:

-i: -c: line 0: unexpected EOF while looking for matching `"'
-i: -c: line 1: syntax error: unexpected end of file

The function that should be executing these lines in the file looks like this:

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

What am I doing incorrectly?

Thanks!

I have two solutions.

First one, in your file write

sed -i s#libcrypto.a##;s#libssl.a## Makefile

without the apices and without the spaces, in the edited Makefile you'll get two innocouos, I mean harmless, extra spaces.

The second one is to modify your function, the one that executes lines from the file, and using eval $line instead of using simply $line (my guess is that your function reads a line from the file, stores the result in a variable line , like in

while read line ; do
    ...
done < the_file_with_the_sed_command 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM