Link to home
Start Free TrialLog in
Avatar of AceAA
AceAA

asked on

How can i set a Environment Variable in my Unix script ?

Below is the code that I have, where I need to replace a server name with the hostname of the box I logged into, can someone please help? The below script is not working as expected, I might be doing something correct, may be?

server=$(hostname)

echo $server

for file in $(find /home/ace/ -type f)
    do
           sed 's/acee123/$server/g' $file > $file.tmp
           mv $file.tmp $file
done
ASKER CERTIFIED SOLUTION
Avatar of tel2
tel2
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AceAA
AceAA

ASKER

Thank you tel2, I may not be able to use "sed -i" option as I am using Solaris. Your answer of using double qoutes instead of single quotes helped me.

Thank you,
Thanks for the points, Ace, and good point about Solaris.
You're probably right because I think Solaris doesn't come with GNU sed, which is what has the '-i' switch.  (Although you can install GNU sed if you want to.)

But I think Solaris ships with Perl, and that has the '-i' switch, so you can do things like:
perl -i -pe 's/\Qacee123\E/$ENV{HOSTNAME}/' /home/ace/*

Open in new window

Note that single quotes are OK with this Perl solution.

Note, I've just realised that the following 2 commands won't necessarily return the same result:
hostname
echo $HOSTNAME

Open in new window

So if they don't on your system, then you might need to change things a bit.  Let me know if you want help with that.