1833792 Members
2279 Online
110063 Solutions
New Discussion

sed nightmare

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

sed nightmare

why does set work from commandline as expected, but not in script?

+ echo
+ 1>> /etc/opt/config/UserConfig.properties
+ set -x
+ sed -e s|^ocmp.config.file.start.*|ocmp.config.file.start=Configitious.xml| /etc/opt/config/UserConfig.properties
+ 1> /tmp/tmp4487
+ echo return code=0 : tempfile=tmp4487 : UserConfig=/etc/opt/config/UserConfig.properties
return code=0 : tempfile=tmp4487 : UserConfig=/etc/opt/config/UserConfig.properties
+ mv /tmp/tmp4487 /etc/opt/config/UserConfig.properties
+ echo return code=0 : tempfile=tmp4487 : UserConfig=/etc/opt/config/UserConfig.properties
return code=0 : tempfile=tmp4487 : UserConfig=/etc/opt/config/UserConfig.properties
+ chown ocmpadm:ocadmin /etc/opt/config/UserConfig.properties
+ chmod 766 /etc/opt/config/UserConfig.properties

script:

echo " " >> $USERPROP
set -x
sed -e "s|^ocmp.config.file.start.*|ocmp.config.file.start=${NEWCONFIG}|" ${USERPROP} > /tmp/tmp$$
echo "return code=$? : tempfile=tmp$$ : UserConfig=$USERPROP"
mv /tmp/tmp$$ $USERPROP
echo "return code=$? : tempfile=tmp$$ : UserConfig=$USERPROP"

Later,
Bill
It works for me (tm)
16 REPLIES 16
Sanjay_6
Honored Contributor

Re: sed nightmare

Hi Bill,

Try using the absolute path to sed, /usr/bin/sed.

you can also put the shell in the first line and try,

#!/usr/bin/sh

Hope this helps.

regds
Bill McNAMARA_1
Honored Contributor

Re: sed nightmare

no joy,
the destination file is empty and the tmp$$ file is not created.
It works for me (tm)
Paula J Frazer-Campbell
Honored Contributor

Re: sed nightmare

Bill

In scripts always put the full path to ANY command that you call - set , sed , mv ,cp , chown ,chmod etc.

9 time out of 10 it will work ok without this, but err on the safe side.

As Sanjay has said it is also best to define your shell as the first line.

#!/bin/sh
# Script from here down.
Bla bla bla


HTH

Paula
If you can spell SysAdmin then you is one - anon
Volker Borowski
Honored Contributor

Re: sed nightmare

Hi Bill,

only diffrence I saw is, that in the Script you have some variables, which you expanded in your direct call. In this case the type of quotes you use may have impact.

The output seems somewhat disformatted.
Could you attach it ?

Volker
Paula J Frazer-Campbell
Honored Contributor

Re: sed nightmare

Hi
Bill
Just an idea have a look in the root dir or the dir you are running from for the tempfile,
as the echo of the tmpfile is not showing a path



Paula
If you can spell SysAdmin then you is one - anon
Bill McNAMARA_1
Honored Contributor

Re: sed nightmare

attached
It works for me (tm)
Alan Riggs
Honored Contributor

Re: sed nightmare

Bill, I'm not entirely certain I understand your problem, but "set -x" sends its output to STDERR. If you are scriting it, you need to redirect output strem 2 into the logfile to capture the "+ messages".
Paula J Frazer-Campbell
Honored Contributor

Re: sed nightmare

Bill

Your line :-

sed -e "s|^ocmp.config.file.start.*|ocmp.config.file.start=Configitious.xml|" $USERPROP > /tmp/tmp$$

try it with the full path to sed:-

/usr/bin/sed -e "s|^ocmp.config.file.start.*|ocmp.config.file.start=Configitious.xml|" $USERPROP > /tmp/tmp$$


Paula
If you can spell SysAdmin then you is one - anon
Bill McNAMARA_1
Honored Contributor

Re: sed nightmare

no joy Paula..

Have you tried it?

Later,
Bill
It works for me (tm)
Bill McNAMARA_1
Honored Contributor

Re: sed nightmare

perhaps the quotes are causing a problem...

Bill
It works for me (tm)
Paula J Frazer-Campbell
Honored Contributor

Re: sed nightmare

Bill

Have you tried:-

1. Put your sh as #!/bin/ksh
2. Run ksh -x <scriptname>


It might give an little more info.

Paula


If you can spell SysAdmin then you is one - anon
Volker Borowski
Honored Contributor
Solution

Re: sed nightmare

works for me ....

# cat bastel.txt
#User defined default and start config
#Thu Feb 07 12:02:28 CET 2002
ocmp.config.file.start=7.intro.xml

# cat script
#!/usr/bin/ksh
set -x
export USERPROP=bastel.txt
sed -e "s|^ocmp.config.file.start.*|ocmp.config.file.start=Configitious.xml|" $USERPROP > out$$
mv out$$ weg

# ./script
+ export USERPROP=bastel.txt
+ sed -e s|^ocmp.config.file.start.*|ocmp.config.file.start=Configitious.xml| bastel.txt
+ 1> out12026
+ mv out12026 weg

# cat weg
#User defined default and start config
#Thu Feb 07 12:02:28 CET 2002
ocmp.config.file.start=Configitious.xml

#

Replace OK, rest of file untouched !

????

Any corrupt chars inside the script or the config file, that do not show up with cat ?
Like ^M when transfered from WIN ?

Sorry, no idea, looks OK !

Volker
Sanjay_6
Honored Contributor

Re: sed nightmare

Hi Bill,

Looks like the sed is working. You don't see the tmp$$ file because it has been moved to $USERPROP

The original $USERPROP has
#User defined default and start config
#Thu Feb 07 12:02:28 CET 2002
ocmp.config.file.start=7.intro.xml

The modified $USERPROP has,

User defined default and start config
#Thu Feb 07 12:02:28 CET 2002
ocmp.config.file.start=Configitious.xml

You have moved the tmp$$ file to $USERPROP over here,

mv /tmp/tmp$$ $USERPROP

Hope this helps.

Regds
Alan Riggs
Honored Contributor

Re: sed nightmare

Bill

Perhaps I am being dense. In the section you out, you appear to move the USERPROP file to the trash bin: "mv /etc/opt/OCMP/UserConfig.properties /home/trsh/backup/UserConfig.properties.back"
Then you send a single echo into the file, which is why sed doesn't complain that it can't find the file.

So, you run the sed command against a basically empty file and generate a new basically empty file. Unles I'm missing something.
Volker Borowski
Honored Contributor

Re: sed nightmare

... guess I need to hand over the rabbit to Alan.

Very good job Alan.
Volker
Bill McNAMARA_1
Honored Contributor

Re: sed nightmare

;)

yea, that was a backup and restore function!

heh!

I changed it to cp... after which!!

Thanks!

Later,
Bill

It works for me (tm)