Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed unknown option to `s' in bash script [duplicate]

Tags:

bash

sed

Im having a bit of trouble putting together a bash script im working on

Here is the code im using.

cat /crawler/bc_daemon.php | sed "s/PORT2/${PORT}/ig" | sed 's/IP2/IPADDRESS/ig' | \
   sed 's/USER2/USER/ig' | sed 's/PASS2/PASSWORD/ig' > bc_daemon.php

cat /crawler/bc_layout.php | sed "s/GITHUB/${REPO}/ig" | sed "s/COINNAME/${NAME}/ig" > bc_layout.php

The odd thing is the lines work individually out of the script. but when inside the script i get this

sed: -e expression #1, char 17: unknown option to `s'

I am using '' when it can take it literally and "" when it needs to print the variable

It seems to me this should be working. But im lost as to where my error is

like image 658
user3566292 Avatar asked Jul 11 '14 20:07

user3566292


2 Answers

One of the values contains a slash. You need to escape it, or use a different delimiter.

Additionally, you are needlessly stringing together multiple invocations where a single one would do. The cat is also useless.

sed -e "s@PORT2@${PORT}@ig" \
    -e 's/IP2/IPADDRESS/ig' \ 
    -e 's/USER2/USER/ig' \
    -e 's/PASS2/PASSWORD/ig'  /crawler/bc_daemon.php > bc_daemon.php

Unfortunately, not all sed dialects are compatible. If yours doesn't like multiple -e options, try a single string of newline-separated sed commands. I'm providing the second script in this syntax as an example.

sed "s!GITHUB!${REPO}!ig
    s!COINNAME!${NAME}!ig"  /crawler/bc_layout.php > bc_layout.php

If your values could contain @ or ! as well, you will need to pick a different delimiter. Any nonalphanumeric ASCII character will do, but backslash and quotes are obviously problematic.

like image 168
tripleee Avatar answered Oct 24 '22 06:10

tripleee


One possible thing is that i may not be recognized by the sed you use that you have no choice but to remove it:

sed "s/GITHUB/${REPO}/g"

(kunwar.sangram suggests that the uppercase I may be recognized instead.)

Another thing is that some of your variables may contain the delimiter you use to s, so perhaps trying to use another delimiter may fix it:

sed "s|GITHUB|${REPO}|ig"
like image 44
konsolebox Avatar answered Oct 24 '22 04:10

konsolebox