Computing.Net > Forums > Unix > Unix shell script for opening a file and

Unix shell script for opening a file and

Reply to Message Icon

Original Message
Name: abby
Date: July 10, 2002 at 08:28:20 Pacific
Subject: Unix shell script for opening a file and
Comment:

Hi,
Can anyone tell me how to write a Unix ksh script to open a file (path provided) and assign a value to a variable in the file and then save the file with the same name ?
The variable can be present multiple times in the file but I need to change at the first instance only.


Report Offensive Message For Removal

Response Number 1
Name: LANkrypt0
Date: July 10, 2002 at 08:49:48 Pacific
Subject: Unix shell script for opening a file and
Reply: (edit)

Ok, what do you mean by variable in the file?

Like
a=1
b=2
c=3
a=2
b=3
c=4

etc

Something along thosel lines?


Report Offensive Follow Up For Removal

Response Number 2
Name: abby
Date: July 10, 2002 at 08:56:58 Pacific
Subject: Unix shell script for opening a file and
Reply: (edit)

Hi Lan,
Yes, the variable in the file can be of the type :
a="/usr/home/abby/dir1"
and I need to change the value of "a" to a different value, say "/usr/home/andy/dir2"

I need to do this via a script which would open the file which contains "a" (ONLY the first instance) and then change the value to the new one and save the file again.
Thanks.


Report Offensive Follow Up For Removal

Response Number 3
Name: LANkrypt0
Date: July 10, 2002 at 12:16:46 Pacific
Subject: Unix shell script for opening a file and
Reply: (edit)

Sorry about the delay, work got busy, fast. But here is the solution.
For your reference.
$FILE is the file where the info is stored
and
$NEWDIR is the directory you want to replace the origional with

sed -e '1s/a="\/usr\/home\/abby\/dir1"\/a=$NEWDIR/;t' -e '1,/a=\/usr\/home\/abby\/dir1/s//b=$NEWDIR/' $FILE > $FILE.temp
mv $FILE.temp $FILE

Now remember when you read the input for the newdirectory you will need to place a \ infront of the / in order for sed to change it correctly. And place " " around the directory.



Report Offensive Follow Up For Removal

Response Number 4
Name: Jerry Lemieux
Date: July 19, 2002 at 13:44:02 Pacific
Subject: Unix shell script for opening a file and
Reply: (edit)

Why not simply:

Var=/home/myhome
exec 3/tmp/file.tmp
while read -u3 Line
do
if [[ ${Line%%=*} = "Var" ]]
then
print -u4 -r "Var=$Var"
else
print -u4 -r $Line
fi
done

exec 3&-

mv /tmp/file.tmp /tmp/file


What is happening is that you are opening 2
file descriptors. One is the file you want to read, the other is the file you are writing to. Once in the loop, you are evaluating the field to the left of the = sign. If it matches, you write the new record. Otherwise, it will write the line intact into the file. Changes all occurances of the variable Var, but you said that didn't matter. Easier the playing around with sed and have to put a \ in front of each / so sed can process them.

Jerry


Report Offensive Follow Up For Removal

Response Number 5
Name: Jerry Lemieux
Date: July 19, 2002 at 13:47:14 Pacific
Subject: Unix shell script for opening a file and
Reply: (edit)

Previous post got messed up in the paste. This is the correct one.

Why not simply:

Var=/home/myhome
exec 3/tmp/file.tmp
while read -u3 Line
do
if [[ ${Line%%=*} = "Var" ]]
then
print -u4 -r "Var=$Var"
else
print -u4 -r $Line
fi
done

exec 3&-

mv /tmp/file.tmp /tmp/file


What is happening is that you are opening 2
file descriptors. One is the file you want to read, the other is the file you are writing to. Once in the loop, you are evaluating the field to the left of the = sign. If it matches, you write the new record. Otherwise, it will write the line intact into the file. Changes all occurances of the variable a, but you said that didn't matter. Easier the playing around with sed and have to put a \ in front of each / so sed can process them.

Jerry


Report Offensive Follow Up For Removal


Response Number 6
Name: Jerry Lemieux
Date: July 19, 2002 at 13:56:36 Pacific
Subject: Unix shell script for opening a file and
Reply: (edit)

I still had a problem posting this. I'll try on more time. For some reason, I can't get the :

exec 3"less than sign"/tmp/file

and the second file

exec 4"greater than sign"/tmp/file.tmp

to show up. When I submit, they get removed along with some information.

Jerry



Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Unix shell script for opening a file and

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software