Preserve value between different run of script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preserve value between different run of script.
# 1  
Old 01-02-2013
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
Code:
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?
# 2  
Old 01-02-2013
If you
Code:
echo $test >> /tmp/log_status

instead, then every time you do this another result would be appended to that file.
# 3  
Old 01-02-2013
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?
# 4  
Old 01-02-2013
Depends...

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)?
# 5  
Old 01-02-2013
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:-
Code:
#!/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:-

Code:
#!/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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

Script fails to run properly when run from CRONTAB

Hello all, I'm trying to write a script to gather and send data and it works just fine at the bash command line, but when executing from CRON, it does not run properly. My scripting skills are pretty limited and there's probably a better way, but as I said it works at the command line, but... (12 Replies)
Discussion started by: rusman
12 Replies

3. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

4. Shell Programming and Scripting

how to run an already made script run against a list of ip addresses solaris 8 question

how to run an already developed script run against a list of ip addresses solaris 8 question. the script goes away and check traffic information, for example check_GE-VLANStats-P3 1.1.1.1 and returns the results ok. how do I run this against an ip list? i.e a list of 30 ip addresses (26 Replies)
Discussion started by: llcooljatt
26 Replies

5. Shell Programming and Scripting

Preserve extented ascii character when run echo comand inside bash script

Hi everyone, I'm echo some text with extended ascii characters as below: echo -e "Pr\xE9sentation du spectacle" > output or echo -e "Présentation du spectacle" > outputIf I open the file created I see this text Présentation du spectacleThe text is shown correctly in this created file when... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

6. AIX

My script didn't run every run every minute at cronjob

In my cronjob, I would like to schedule my script.sh to run every minutes. I crontab -e and have in line below but it didn't seems to run at all. * * * * * script.sh When I run it manually, I can run it. Is that anything wrong with the above line? If I change it to something like below,... (4 Replies)
Discussion started by: ngaisteve1
4 Replies

7. UNIX for Advanced & Expert Users

Automated SCP script passing password to preserve source file timestamp

Hi My requirement is i want to copy files from remote server to the local server and also i need to preserve the timestamp of the remote file. By using scp -p , it is working fine in the interactive call but it is not preserving he file timestamp when i use it in the non interactive scp call... (1 Reply)
Discussion started by: skumar75
1 Replies

8. Shell Programming and Scripting

Run a shell script from one host which connext to remote host and run the commands

I want to write a script which would run from one host say A and connect to other remote host B and then run rest of commands in that host. I tried connecting from A host to B with SSH but after connecting to host B it just getting me inside Host B command prompt. Rest of the script is not running... (6 Replies)
Discussion started by: SN2009
6 Replies

9. UNIX for Advanced & Expert Users

script to run different shells which run different processes

Hi, Would like to ask the experts if anyone knows how to run a script like this: dtterm -title shell1 run process1 on shell1 dtterm -title shell2 run process2 on shell2 cheers! p/s: sorry if i used the wrong forum, quite concussed after watching world cup for several nights; but I... (2 Replies)
Discussion started by: mochi
2 Replies
Login or Register to Ask a Question