3

I have a simple script which runs on a FreeBSD machine with the following code:

#!/bin/sh
`sed -i .bak '\:#start 172.0.0.3:,\:#end 172.0.0.3:d' /usr/local/etc/racoon/racoon.conf`
echo $?

It should delete a block of text between the two patterns.

The problem is that if I run the sed command directly from shell it works, if i run the script the return code is 0.

Why's that?

2
  • the command is sed -i .bak '\:#start 172.0.0.3:,\:#end 172.0.0.3:d' /usr/local/etc/racoon/racoon.conf, between `` so it can be executed within the shell script Jun 18, 2010 at 13:10
  • From the phrasing of your question, it seems like you weren't expecting a return value of 0 for a successfully executed command, when this is precisely the behavior a command should exhibit.
    – Tekhne
    Jun 18, 2010 at 18:22

2 Answers 2

7

You need to eliminate the backticks and change the single quotes to double quotes:

#!/bin/sh
ip=$1
echo $ip
sed -i .bak "\:#start ${ip}:,\:#end ${ip}:d" /usr/local/etc/racoon/racoon.conf

Using double quotes will allow the variables to be expanded. The backticks would try to execute the standard output of the command (which there is none in this case). For example, if you did `echo hi` it would try to execute "hi" as a command. sed always returns 0 unless there's an error regardless of whether a replacement was made.

14
  • thanks!worked. however if we are here i want to ask you something else (since this was a sub-problem of my big problem). I have a script called rootexec.sh which contains the code : #!/bin/sh exec $1 (this script is used as a parameter for a sudo). The problem is that if i use script.sh 'sed -i .bak "\:#start 172.0.0.3:,\:#end 172.0.0.3:d" /usr/local/etc/racoon/racoon.conf' (the sed being the $1 parameter for the exec command), i get this error : sed: 1: ""\:#start": invalid command code " . exec seems to have a problem with it? Jun 18, 2010 at 13:54
  • should i post this as a separate problem? Jun 18, 2010 at 14:41
  • @Darie: There's a serious security risk of passing commands as parameters to scripts that get run using sudo. That said, you might try putting double quotes round $1: exec "$1". Also read BashFAQ/050. Jun 18, 2010 at 14:51
  • with "$1" i get this error : exec: sed -i .bak "\:#start 172.0.0.2:,\:#end 172.0.0.2:d" /usr/local/etc/racoon/racoon.conf: not found I am working on a system build by somebody else and i need to use their scripts. Jun 18, 2010 at 15:15
  • @Darie: The next thing I would suggest trying is, instead of the double quotes, escape the backslashes or use forward slashes: script.sh 'sed -i .bak "\\:#start 172.0.0.3:,\\:#end 172.0.0.3:d" /usr/local/etc/racoon/racoon.conf' or script.sh 'sed -i .bak "/#start 172.0.0.3/,/#end 172.0.0.3/d" /usr/local/etc/racoon/racoon.conf' Jun 18, 2010 at 15:48
1

The backticks really are not necessary (or perhaps I understand you wrong). The fact that the script returns 0 is also expected: exit code 0 is OK. Edit: sed will always exit with 0, even if there has been no substitution. Another exit code is only then generated when there is an error in your syntax.

More interesting is: is the code deleted or not?

Judging from your comments, you probably have a problem with regexp and / or escaping characters in it.

2
  • actually the script is using a parameter (the ip) #!/bin/sh ip=$1 echo $ip sed -i .bak '\:#start ${ip}:,\:#end ${ip}:d' /usr/local/etc/racoon/racoon.conf echo $? The text block doesnt get deleted from the file even if the return code is OK. Jun 18, 2010 at 13:16
  • the .bak file is created (that means the command was executed, however the block of code was not removed) Jun 18, 2010 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.