I have a problem with “sed” command in Shell Script,
I have the following script to try and replace 3 “labels” with information that I capture from 2 other files:
#!/bin/sh
VAR1=$(cat file1.sh | grep --max-count=1 DLC=)
VAR2=$(cat file1.sh | grep --max-count=1 PROPATH=)
VAR3=$(cat file2.sh | grep -e - )
sed 's|PATTERN1|'"$VAR1"'|' < Template.sh > temp1.sh
sed 's|PATTERN2|'"$VAR2"'|' < temp1.sh > temp2.sh
sed 's|PROGRESS_CONNECTION|'"$VAR3"'|' < temp2.sh > Final.sh
Note that the contents of the 3 vars are strings like (without the double quotes):
VAR1="DLC=/user/aaa/bbb;export DLC"
VAR2="PROPATH=/user/ccc/ddd;export PROPATH"
VAR3="-db docdb -trig triggers -H sona01 -S 19001 -N TCP -ld newyork
-db docadm -trig triggers -H sona01 -S 19002 -N TCP -ld docadm
-db dochelp -trig triggers -H sona01 -S 19003 -N TCP -ld dochelp
-yy 1920 -rereadnolock -T /tmp -c 30 -d mdy -Bt 350 -D 100 -mmax 8192 -nb 200 -s 160 -noshvarfix -inp 32000
-cpinternal utf-8 -cpstream utf-8 -cpcoll ICU-UCA"
When I run this script, temp1.sh is correctly created, temp2.sh is correctly created, but Final.sh is not created and I get a sed error message:
sed: -e expression #1, char 91: unterminated `s' command
At this point I am unsure of what is causing the problem.
Any help?
The problem is that your
$VAR3variable does contain some newline characters.A
s///cannot contain a newline (except if preceded by\). A solution would be to remove the newlines withtr -d '\n':Now use it in sed: