Re: Environment Variable Count (110 Views)
Reply
Occasional Contributor
Pascal Kouknas
Posts: 3
Registered: ‎08-13-2002
Message 1 of 4 (110 Views)
Accepted Solution

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

Honored Contributor
Patrick Wallek
Posts: 13,776
Registered: ‎06-21-2000
Message 2 of 4 (110 Views)

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
David Totsch
Posts: 44
Registered: ‎02-07-1999
Message 3 of 4 (110 Views)

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-
Frequent Advisor
Christophe MAILHE
Posts: 40
Registered: ‎03-03-2002
Message 4 of 4 (110 Views)

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.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.