Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

Ok, what do you mean by variable in the file?
Like
a=1
b=2
c=3
a=2
b=3
c=4etc
Something along thosel lines?

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.

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 withsed -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 $FILENow 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.

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
doneexec 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

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
doneexec 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

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.tmpto show up. When I submit, they get removed along with some information.
Jerry

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |