Preserve value between different run of script.

I have a script that test for server up status.
I need to preserve the value of status between every run of the script.

test.sh

log=$(cat /tmp/log_status)

start loop
test for servers
print status for munin
etc
test=$(echo -e "${test}${status},")
end loop

echo $test > /tmp/log_status

This works fine, but do I need to write status to a file, or can it be exported?

If you

echo $test >> /tmp/log_status

instead, then every time you do this another result would be appended to that file.

Hi thanks.
That I do understand, but do I need to create a file, or can it be stored in a variable, using export/import?

Variables can be saved and retrieved within the session (current logon).
Do you only need to compare now vs. 5 minutes ago (and same user session)?

If you can save your values to a file, then that might be better. Depending what shell you are using, there may be different syntax of commands, so if you script was ksh then you could do something like this:-

#!/bin/ksh

# Initialise variables
a=0
b=0
string="Hello"

# Restore previous session values
if [ -r my_state ]
then
   . my_state
fi


# Do processing

((a=$a+1))
((b=$b+$a))


# Store session values for next run

echo "a=$a
b=$b
string=\"$string\"
" > my_state

Using C-shell you would change it to this:-

#!/bin/ksh

# Initialise variables
a=0
b=0
string="Hello"

# Restore previous session values
if ( -r my_state )
then
   source my_state
fi

# Do processing


# Store session values for next run

echo "setenv a=$a
setenv b=$b
setenv string=\"$string\"
" > my_state

Not too certain on the C-shell version as I've only been a decipherer of scripts for that, but you get the idea.

I hope that this is useful.

Robin
Liverpool/Blackburn
UK