RSS Forums RSS
Please support our Shell Scripting advertiser: Programming Forums
Views: 4396 | Replies: 2
Reply
Join Date: Jul 2005
Posts: 2
Reputation: icui is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
icui icui is offline Offline
Newbie Poster

[Bash] problem with script in sed command

  #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
parametro1=mario
parametro2=ugo
parametro3=elena
parametro4=sara


Script:
#!/bin/bash -x

TMP_FILE=./test.tmp
CONF_FILE=./conf.tmp
TOT_PARAMETERS=$#;
scratch=""

if test $[TOT_PARAMETERS%2] -ne 0
then
        echo "Usage: $0 desc"
        exit 1
fi

if test $# -lt 2
then
        echo "Usage: $0 desc"
        exit 1
fi

ctr=0
until [ -z "$1" ]  
do
  PARAMETER=$1
  NEW_VALUE=$2
  if test ${ctr} -eq 0
  then
    scratch="sed \"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
  else
    scratch="${scratch} | sed \"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
  fi
  let ctr=ctr+1
  shift 2
done

echo -e ""
echo "COMMAND LINE: cat ${CONF_FILE} | ${scratch} > $TMP_FILE";
echo -e ""

cat ${CONF_FILE} | ${scratch} > $TMP_FILE

# ... continua


executing the script:

:~$ ./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:

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:

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: [Bash] problem with script in sed command

  #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  
Join Date: Jul 2005
Posts: 2
Reputation: icui is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
icui icui is offline Offline
Newbie Poster

Re: [Bash] problem with script in sed command

  #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:
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:

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:

cmd=""
while [ -n "$1" ]
do
        cmd="${cmd}s%^$(printf "%q" $1).*%${1}=${2}%;"
        shift 2
done
sed -e "$cmd" conf.file > test.tmp 

it was more efficient, more intelligent and it works
(from comp.unix.shell)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:40 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC