Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Environment Variable Count

SOLVED
Go to Solution
Occasional Contributor

Environment Variable Count

[ Edited ]

Hi all,
What I am trying to do is set an environment variable.
SEQ=20000
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
SEQ=20003

 

 

P.S. This thread has been moved from HP-UX > General to HP-UX > languages - HP Forums Moderator

1 ACCEPTED SOLUTIONS
Honored Contributor Honored Contributor

Re: Environment Variable Count

The easiest way to do this would be to have a file somewhere that gets looked when the user logs in and / or runs the report.

# cat /dir/SEQ
2002

# export SEQ=`cat /dir/SEQ`
# echo $SEQ
2002

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.
3 REPLIES
Honored Contributor Honored Contributor

Re: Environment Variable Count

The easiest way to do this would be to have a file somewhere that gets looked when the user logs in and / or runs the report.

# cat /dir/SEQ
2002

# export SEQ=`cat /dir/SEQ`
# echo $SEQ
2002

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.
Valued Contributor Valued Contributor

Re: Environment Variable Count

Pascal:

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...

-dlt-
Highlighted
Frequent Advisor

Re: Environment Variable Count

Hi Pascal,

In order to make sure not to have duplicate you could use the following method :

while :
do
mkdir /tmp/get_new_seq 1>/dev/null 2>/dev/null
if [ $? = 0 ]
then
break
fi
done
export SEQ=`cat /dir/sequence_number`
let NEXTSEQ=SEQ+1
echo $NEXTSEQ > /dir/sequence_number
unset NEXTSEQ
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.

Christophe.