[Bash] problem with script in sed command

Reply

Join Date: Jul 2005
Posts: 2
Reputation: icui is an unknown quantity at this point 
Solved Threads: 0
icui icui is offline Offline
Newbie Poster

[Bash] problem with script in sed command

 
0
  #1
Jul 21st, 2005
I made a little script that searches and substitutes parameters in a configuration file but I've a problem:

Configuration file:

conf.tmp
Shell Scripting Syntax (Toggle Plain Text)
  1. parametro1=mario
  2. parametro2=ugo
  3. parametro3=elena
  4. parametro4=sara


Script:
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash -x
  2.  
  3. TMP_FILE=./test.tmp
  4. CONF_FILE=./conf.tmp
  5. TOT_PARAMETERS=$#;
  6. scratch=""
  7.  
  8. if test $[TOT_PARAMETERS%2] -ne 0
  9. then
  10. echo "Usage: $0 desc"
  11. exit 1
  12. fi
  13.  
  14. if test $# -lt 2
  15. then
  16. echo "Usage: $0 desc"
  17. exit 1
  18. fi
  19.  
  20. ctr=0
  21. until [ -z "$1" ]
  22. do
  23. PARAMETER=$1
  24. NEW_VALUE=$2
  25. if test ${ctr} -eq 0
  26. then
  27. scratch="sed \"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
  28. else
  29. scratch="${scratch} | sed \"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
  30. fi
  31. let ctr=ctr+1
  32. shift 2
  33. done
  34.  
  35. echo -e ""
  36. echo "COMMAND LINE: cat ${CONF_FILE} | ${scratch} > $TMP_FILE";
  37. echo -e ""
  38.  
  39. cat ${CONF_FILE} | ${scratch} > $TMP_FILE
  40.  
  41. # ... continua


executing the script:

Shell Scripting Syntax (Toggle Plain Text)
  1. :~$ ./script.sh parametro1 pippo parametro2 pluto parametro3 paperino

look the output, the printed command line is correct but it works only if executed out of the script.
The same command in the script fails.

Command line:

Shell Scripting Syntax (Toggle Plain Text)
  1. cat ./conf.tmp | sed "s%^parametro1=.*%parametro1=pippo%g" | sed "s%^parametro2=.*%parametro2=pluto%g" | sed "s%^parametro3=.*%parametro3=paperino%g" > ./test.tmp

in debug mode I look that:

Shell Scripting Syntax (Toggle Plain Text)
  1. sed '"s%^parametro1=.*%parametro1=pippo%g"' '|' sed '"s%^parametro2=.*%parametro2=pluto%g"' '|' sed '"s%^parametro3=.*%parametro3=paperino%g"'

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

Any hints?


Ps. Sorry for my terrible english
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: [Bash] problem with script in sed command

 
0
  #2
Jul 22nd, 2005
I don't think I follow your problem, what exactly are you trying to do with sed?
You said it works form the command line but not in the script?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 2
Reputation: icui is an unknown quantity at this point 
Solved Threads: 0
icui icui is offline Offline
Newbie Poster

Re: [Bash] problem with script in sed command

 
0
  #3
Jul 23rd, 2005
Originally Posted by gritty
You said it works form the command line but not in the script?
exactly,

this line works fine:
Shell Scripting Syntax (Toggle Plain Text)
  1. cat $filename | sed "s%^p1=.*%p1=v1%g" | sed "s%^p2=.*%p2=v2%g" > $filetarget

But in the script same command fails because the command line it's changed in:

Shell Scripting Syntax (Toggle Plain Text)
  1. sed '"s%^p1=.*%p1=v1%g"' '|' sed '"s%^p2=.*%p2=v2%g"'

look the argument of sed all with added single quote, also the pipe.

here the solution at problem:

Shell Scripting Syntax (Toggle Plain Text)
  1. cmd=""
  2. while [ -n "$1" ]
  3. do
  4. cmd="${cmd}s%^$(printf "%q" $1).*%${1}=${2}%;"
  5. shift 2
  6. done
  7. sed -e "$cmd" conf.file > test.tmp

it was more efficient, more intelligent and it works
(from comp.unix.shell)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC