Operating System - HP-UX
1855734 Members
2176 Online
104103 Solutions
New Discussion

Unix Script updating a text file

 
SOLVED
Go to solution
Laurie A. Krumrey
Regular Advisor

Unix Script updating a text file

Hi All,

I have a backup application called Netbackup
and I need to pass parameters (the name of each server to backup) to an existing
text file.

SERVER= sundance
CLIENT_NAME = sundance

How do I go about updating the CLIENT_NAME with
arbitrary parameters ?

In other words, I'm not sure how to go about
updating a text file rather than creating a
new file each time the scripts runs..

I really appreciate the help.

Thanks,
Laurie
Happiness is a choice
6 REPLIES 6
Rainer von Bongartz
Honored Contributor
Solution

Re: Unix Script updating a text file

Laurie,

Simply call your script with parameter
netbackup server1

in netbackup substitute CLIENT_NAME with the passed parameter which is named $1
CLIENT_NAME = $1
and off you go

egards
Rainer
He's a real UNIX Man, sitting in his UNIX LAN making all his UNIX plans for nobody ...
Rodney Hills
Honored Contributor

Re: Unix Script updating a text file

I'm note sure exactly what you are asking, but let's see if this answers your question.

If you had a text file called "template", which had the following
server=sundance
client_name=xxxxxx

Then if you built the following script
#!/usr/bin/sh
for client in groucho harpo chico zeppo ;
do sed "s/xxxxxx/$client/"
There be dragons...
Sachin Patel
Honored Contributor

Re: Unix Script updating a text file

Hi Laurie,
I am assuming that netbackup is script and it opens file names.txt which has server and client_name information.

run netbackup with argument client name
#./netbackup sachin
now script netbackup knows $1=sachin

Sachin
Is photography a hobby or another way to spend $
A. Clay Stephenson
Acclaimed Contributor

Re: Unix Script updating a text file

Hi Laurie:

There are tons of ways to do this but all involve using the shell, awk, perl, or even C to build your text file 'on the fly'.

I suspect I would do this is using command line args:

something like this:

#!/usr/bin/sh
STAT=0
PROG=$0
if [ $# -ge 2 ]
then
server=$1
shift
client=$1
shift
echo "SERVER=${server}"
echo "CLIENT_NAME=${client}"
# add what ever else you need with echo's
else
echo "${PROG} requires at least 2 args" >&2
STAT=255
fi
exit $STAT

You would then use it like this:
buildfile.sh sundance butch > myfile


Clay
If it ain't broke, I can fix that.
Joseph C. Denman
Honored Contributor

Re: Unix Script updating a text file

You really can't use substitue because it may change. I would delete the line and re-add it.

sed '/CLIENT_NAME/d' configfile > configfile.out
echo "CLIENT_NAME = newclient" >> configfile.out
mv configfile.out configfile

There are several ways to do it.

...jcd...
If I had only read the instructions first??
Magdi KAMAL
Respected Contributor

Re: Unix Script updating a text file

Hi Laurie,

1. Create a text file with all your servers that that you want to backup ( example : serversFile.txt ).
2. Call your netbackup script using parameter serversFile.txt like :
# netbackup serversFile.txt .

3.
for currServer in `cat $1`
do
CLIENT_NAME=$currServer

... commands ....

done

4.
exit 0


Magdi