08-14-2002 03:53 PM - last edited on 10-22-2012 12:08 AM by Maiko-I
What I am trying to do is set an environment variable.
This variable will change when the user runs a specific report.
run no 1 SEQ=20001
run no 2 SEQ=20002
What I want to do is retain this new value so as next time the user logs on the sequence continues
P.S. This thread has been moved from HP-UX > General to HP-UX > languages - HP Forums Moderator
Solved! Go to Solution.
08-14-2002 04:44 PM
# cat /dir/SEQ
# export SEQ=`cat /dir/SEQ`
# echo $SEQ
Then when you increment SEQ you do a:
echo $SEQ > /dir/SEQ
and the new value will then be kept in the file for the next run.
08-14-2002 06:33 PM
Patrick's is good if you are keeping the sequence per user (only one copy of the script running at a time will update the file that records the sequence number). If that is not true, you will need to come up with a solution that is better than "the last person to write the file wins".
If so, here are two suggestions: 1) use a directory and touch a file with the sequence number in the name (be careful of how ls(1) lists numbered file names -- not necessairly in numerical order), get the last file in the list, increment the number and touch the file name (only if it does not already exist). 2) use a single file, get the last number off the bottom, increment it, append it to the file, and make sure it is not a duplicate (unfortunately, I ran out of ideas on how to deal with a duplicate -- sigh). 3) If you run your sequencing script as an LP spool script, you could use the job number...
08-15-2002 03:05 AM
In order to make sure not to have duplicate you could use the following method :
mkdir /tmp/get_new_seq 1>/dev/null 2>/dev/null
if [ $? = 0 ]
export SEQ=`cat /dir/sequence_number`
echo $NEXTSEQ > /dir/sequence_number
rmdir /tmp/get_new_seq 1>/dev/null 2>/dev/null
As the loop will continue until the directory is created, 2 users could not read /dir/sequence_number at the same time.
Hope that will help.