Thursday, February 3, 2011

unix script problem

Hello everyone,

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?

  • 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.

    Darie Nicolae : 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.
    Darie Nicolae : the .bak file is created (that means the command was executed, however the block of code was not removed)
    From wzzrd
  • 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.

    Darie Nicolae : 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?
    Darie Nicolae : should i post this as a separate problem?
    Dennis Williamson : @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](http://mywiki.wooledge.org/BashFAQ/050).
    Darie Nicolae : 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.
    Dennis Williamson : @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'`
    Darie Nicolae : tried both ways and i get the same thing exec: sed -i .bak "/#start 172.0.0.2/,/#end 172.0.0.2/d" /usr/local/etc/racoon/racoon.conf: not found
    Darie Nicolae : ive tried smth simple like : /usr/local/www/rootexec.sh 'echo lala > /tmp/lala' and i get the same thing : exec: echo lala > /tmp/lala: not found the command not found problem is because of "$1" thingie
    Dennis Williamson : @Darie: Did you try the `$1` without the `""`? Did you read the FAQ that I linked to?
    Darie Nicolae : Without "" for sed i get : sed: 1: ""\:#start": invalid command code " (the error i posted before). For echo however i get : cat: /tmp/lala: No such file or directory BOX# /usr/local/www/rootexec.sh 'echo lala > /tmp>lala' lala > /tmp>lala BOX# It doesnt show any error but the /tmp/lala file doesnt get created. I've looked over the FAQ but i don't see anything that can help me ?
    Dennis Williamson : @Darie: "i don't see anything that can help me" - That's the point of that FAQ: "Patient: Doctor, it hurts when I do this. Doctor: Don't do that."
    Darie Nicolae : I dont see that point, you sure the link is ok?
    Dennis Williamson : @Darie: It's peppered with "doesn't work", "don't do that", etc. However, try this: `script.sh 'sed -i .bak \:#start 172.0.0.3:,\:#end 172.0.0.3:d /usr/local/etc/racoon/racoon.conf'` or, with variables: `script.sh 'sed -i .bak \:#start '${ip}':,\:#end '${ip}':d /usr/local/etc/racoon/racoon.conf'`
    Darie Nicolae : BOX# /usr/local/www/rootexec.sh 'sed -i .bak \:#start 172.0.0.3:,\:#end 172.0.0.3:d /usr/local/etc/racoon/racoon.conf' sed: 1: "\:#start": unterminated regular expression Nope.. seems its not working :(
    Dennis Williamson : @Darie: I'm running out of ideas. It could be a difference in the BSD dialect of `sed` versus the GNU version. One more thing to try: Change the `$1` in `rootexec.sh` to `$@` and try it with some of the variations we've gone through for specifying the `sed` command.

0 comments:

Post a Comment