Operating System - HP-UX
1846763 Members
4738 Online
110256 Solutions
New Discussion

scripting problem with functions

 
SOLVED
Go to solution
Ratzie
Super Advisor

scripting problem with functions

I have a script that I have cleaned up by incorporating functions, instead of one big ugly mess.
I am alittle unclear with the use of functions.

This is what I want to do...
I need to keep track on how long it takes to run a specific function, (with in this function it reads another script, and so on)
So I though I would set it up this way...

backup () {
STARTOFFLINEBU=$SECONDS

script stuff...

ENDOFFLINEBU=$SECONDS
((OFFLINEBU_TIME=($ENDOFFLINEBU - $STARTOFFLINEBU) /60))
# export $OFFLINEBU_TIME
echo;echo;echo "Time to complete Offline Backup: $OFFLINEBU_TIME minutes."
}

This script runs alright, until I get...
87: This is not an identifier.

I am assuming that the export is the problem?

Do I need to export the variable in the function if I want to use it else where? Like later on in the script?

# Time it took to complete backup

#ENDENTIREBU=$SECONDS
# ((OFFLINEBU_TIME=($ENDENTIREBU - $STARTENTIREBU) /60))

#echo;echo;echo
#echo " TIME "
#echo "Offline Backup Tar & Bzip2 Backup Complete Time"
#echo "==============================="

#echo $OFFLINEBACKUP_TIME%$TARBZIP2_TIME%$OFFLINEBU_TIME |
#awk -F% '{printf "%-10s %-15s %-15s" $1, $2, $3}'




3 REPLIES 3
Mark Grant
Honored Contributor
Solution

Re: scripting problem with functions

Variables declared in functions have scope throughout the entire script unless specifically specified as "local". You don't have to export them.

I would like to know what line number 87 is in your script. Annoying problems like this are often the result of a test ("[[") where the variables tested are not what you were expecting them to be.
Never preceed any demonstration with anything more predictive than "watch this"
john korterman
Honored Contributor

Re: scripting problem with functions

Hi,
I think it is this line:
# export $OFFLINEBU_TIME
that produces the error, assumed it is not uncommented. Try changing it to:
# export OFFLINEBU_TIME
although - as Mark has pointed out - the line may not be necessary at all.

regards,
John K.
it would be nice if you always got a second chance
Sridhar Bhaskarla
Honored Contributor

Re: scripting problem with functions

Hi,

What is your line 87?.

Below is a small example that the value of the variable will be saved through rest of the script.

$cat scr
func()
{
START=$SECONDS
sleep 10
END=$SECONDS
(( DIFF = ($END - $START) ))
echo "DIFF is $DIFF inside the function"
}

func

echo diff is $DIFF outside the function


$./func
DIFF is 10 inside the function
diff is 10 outside the function

-Sri
You may be disappointed if you fail, but you are doomed if you don't try