1845950 Members
2278 Online
110250 Solutions
New Discussion

shell script question

 
SOLVED
Go to solution
iranzo
Frequent Advisor

shell script question

hello,
I work on HPUX
I want to replace on all
scripts in the machine
the expression :
" $EXPHOME/dir/ "
with this expression :
" $EXPDIR "
Thanks.
Bonjour
6 REPLIES 6
Ross Zubritski
Trusted Contributor

Re: shell script question

Can you be a bit more specific as to what you are trying tom accomplish? Is your question how to replace the string $EXPHOME/dir/ in multiple scripts/files?

Thanks

RZ
Pete Randall
Outstanding Contributor

Re: shell script question

Something along the lines of:

for file in `cat listofscripts`
do
sed 's/$EXPHOME\/dir\//$EXPDIR/' $file > $file.out
mv $file.out $file
done


might do it (though I expect that's pretty crude - I didn't test anything).

Pete

Pete
F. X. de Montgolfier
Valued Contributor
Solution

Re: shell script question

Hi,

you'll have to change INI_DIR to "/", of course...

the original files with extension $SHELL_EXT will be backed up to .bak before the change is made.

#cat test.sh
#!/usr/bin/ksh
INI_DIR=.
SHELL_EXT=".sh"
echo $0
for i in `find $INI_DIR -name "*$SHELL_EXT"|grep -v $0`
do
sed -e 's|$EXPHOME/dir/|$EXPDIR|g' $i >/tmp/tmp_file
j=`basename $i $SHELL_EXT`.bak
mv $i $j
mv /tmp/tmp_file $i
done

#./test.sh
./test.sh
find: cannot open ./.AgentSockets
find: cannot open ./DBI-1.18

#diff test2.bak test2.sh
1c1
< $EXPHOME/dir/
---
> $EXPDIR



Cheers,

FiX

Robin Wakefield
Honored Contributor

Re: shell script question

Hi,

Use perl to do an in-line replace:

perl -i -pe '{s+\$EXPHOME/dir/+\$EXPDIR+g}' filename

Put a loop around this to do it on all files.

rgds, Robin
Steven E. Protter
Exalted Contributor

Re: shell script question

Here is a cool little perl script that will let you actually generate a file list by searching file contents for an expression and then go out and make the changes.

It actually uses sed, just like the guys in the posts above. It's just got a little front end.

You'll need to update the perl location, and its pretty well documented. Buzz back if you have any questions.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Wilfred Chau_1
Respected Contributor

Re: shell script question

# cd /
# ls -1 |while read i
do
cp $i $i.bak
sed -e "s/\$EXPHOME\/dir\//\$EXPDIR/" $i.bak > $i
done