Issue
I have a script.
I need to execute a particular command only for the first time i try to run this script in my shell. Otherwise, it should not be executed and the rest of the commands should be done.
How can i implement this? All pointers are welcome.
Thanks,
Sen
This is a code which i tried to implement :
start_time=`date +%s`
echo $script_instance
if [ `echo $script_instance` == true ]; then
end_time=`date +%s`
echo '#########################################################################'
echo '# Build Date : '`date`
echo '# Compilation time : '`expr $end_time - $start_time` s
echo '#########################################################################'
else
echo '-------------------------------------------------------------------------'
echo 'Updating the APIs'
echo '-------------------------------------------------------------------------'
fi
script_instance=true
export $script_instance
This is not working correctly. Please correct me if I am wrong somewhere.
Solution
You can set an environment variable the first time you're running the script:
- csh/tcsh:
setenv MYVARIABLE something
- bash:
export MYVARIABLE="something"
Then check with an if-clause if the variable is set. If so, do the other stuff, if not then this is the first time the script is executed.
Answered By - dertkw Answer Checked By - Senaida (WPSolving Volunteer)