1834926 Members
2404 Online
110071 Solutions
New Discussion

Re: Script Variables

 
SOLVED
Go to solution
Jeff Martin_3
Advisor

Script Variables

I am trying to write a script that will run from the cron to check (ping) some network devices.

What I really need to know is how to write a variable off to a file or the environent so I can reference that same variable the next time the script runs from cron.

Thanks,

Jeff
5 REPLIES 5
John Poff
Honored Contributor

Re: Script Variables

Hi Jeff,

One way to do it would be to write the variable to a file, and then use the dot command to source in that file at the start of your script. You could do something like this:

MYVAR=something

echo "MYVAR=${MYVAR}" >mysrc


And then at the beginning of your script do:

. mysrc

There are probably better ways to do it but that one comes to mind first.

JP
Paul Sperry
Honored Contributor

Re: Script Variables

more list | while read x #list is the file to read from
do
ping -n 4 $x #command to execute on list elements
done



the file "list" would contain a
list of devices you wanted to ping
Caesar_3
Esteemed Contributor

Re: Script Variables

Hello!

You can make some file that will be
the configuration file and inside write on/off
let say : /etc/your-file.conf

Write inside on/off what you need and
is your script made cat to the file and
check if you get "on" or "off"
and work as you need to what you had.

Caesar
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script Variables

Hi Jeff:

Write your variable to a temporary file. To retrieve it read the temporary file into a variable. Thus:

# FILE=/tmp/myfile
# echo 48 > ${FILE}
...
[ -r ${FILE} ] && VAR=`< ${FILE}
...

The construct VAR=`< ${FILE}` is a very efficient method of reading a file into a variable.

Regards!

...JRF...
Chris Vail
Honored Contributor

Re: Script Variables

The others recommended a couple of good ways to answer your question. But let me point out that ping is not necessarily a good way to determine whether or not a networked device is available. I've had LOTS of circumstances where a completely hosed computer still responded to a ping. This is because ping is ICMP, and is a low-level utility. It merely means that the network card is working, not necessarily that the device it is attached to is up.
For that purpose, use something that actually does something on the remote host. We use secure shell here, and you should too. Here's why:
ssh $HOST "ls -l /etc/hosts"
if test "$?" -ne "0"
then
print "Houston, we have a problem on $HOST"
fi
Every morning at 8 AM, I have a cron job that iterates through a miniature HOSTS table and pretty much does the above. This gives management a warm fuzzy feeling that each host has been checked, and is up and running. A ping all by itself won't do that.


Chris