943,942 Members | Top Members by Rank

Ad:
Jul 21st, 2005
0

[Bash] problem with script in sed command

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
icui is offline Offline
2 posts
since Jul 2005
Jul 22nd, 2005
0

Re: [Bash] problem with script in sed command

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gritty is offline Offline
23 posts
since Jul 2005
Jul 23rd, 2005
0

Re: [Bash] problem with script in sed command

Quote 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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
icui is offline Offline
2 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: how to get appended lines per second?
Next Thread in Shell Scripting Forum Timeline: Regex for password





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC