The return value from the called function in the shell script

I want to return a value from a function called in a shell script. Perhaps I lack syntax. I tried to use global variables. But that doesn't work either. The code:

lockdir="somedir" test() { retval="" if mkdir "$lockdir" then # Directory did not exist, but it was created successfully echo >&2 "successfully acquired lock: $lockdir" retval="true" else echo >&2 "cannot acquire lock, giving up on $lockdir" retval="false" fi return retval } retval=test() if [ "$retval" == "true" ] then echo "directory not created" else echo "directory already created" fi 
+112
shell return-value
Jan 05 2018-12-12T00:
source share
4 answers

The Bash function cannot return a string the way you want. You can do three things:

  • Echo string
  • Returns the exit status, which is a number, not a string
  • Share Variable

This is also true for some other shells.

Here's how to make each of these options:

1. Echo lines

 lockdir="somedir" testlock(){ retval="" if mkdir "$lockdir" then # Directory did not exist, but it was created successfully echo >&2 "successfully acquired lock: $lockdir" retval="true" else echo >&2 "cannot acquire lock, giving up on $lockdir" retval="false" fi echo "$retval" } retval=$( testlock ) if [ "$retval" == "true" ] then echo "directory not created" else echo "directory already created" fi 

2. Return exit status

 lockdir="somedir" testlock(){ if mkdir "$lockdir" then # Directory did not exist, but was created successfully echo >&2 "successfully acquired lock: $lockdir" retval=0 else echo >&2 "cannot acquire lock, giving up on $lockdir" retval=1 fi return "$retval" } testlock retval=$? if [ "$retval" == 0 ] then echo "directory not created" else echo "directory already created" fi 

3. Use a variable

 lockdir="somedir" retval=-1 testlock(){ if mkdir "$lockdir" then # Directory did not exist, but it was created successfully echo >&2 "successfully acquired lock: $lockdir" retval=0 else echo >&2 "cannot acquire lock, giving up on $lockdir" retval=1 fi } testlock if [ "$retval" == 0 ] then echo "directory not created" else echo "directory already created" fi 
+243
Jan 05 '12 at 13:13
source share

You work too hard. Your whole scenario should be:

 if mkdir "$lockdir" 2> /dev/null; then echo lock acquired else echo could not acquire lock >&2 fi 

but even that is probably too verbose. I would write this:

 mkdir "$lockdir" || exit 1 

but the error message received is a little unclear.

+16
Jan 05 '12 at 16:22
source share

If this is just a true / false test, execute your function return 0 for success and return 1 for failure. Then the test will be as follows:

 if function_name; then do something else error condition fi 
+12
Jan 05 '12 at 15:57
source share

I think returning 0 for succ / 1 for failure (glenn jackman) and oliber clearly and the explanatory answer says it all; just mention a kind of “combined” approach for cases where the results are not binary, and you would prefer to set a variable rather than “drive off” the result (for example, if your function is ALSO, suppose it repeats something, this approach will not work). What then? (below is Burn's shell)

 # Syntax _w (wrapReturn) # arg1 : method to wrap # arg2 : variable to set _w(){ eval $1 read $2 <<EOF $? EOF eval $2=\$$2 } 

as in (yep, the example is somewhat dumb, this is just an example)

 getDay(){ d=`date '+%d'` [ $d -gt 255 ] && echo "Oh no a return value is 0-255!" && BAIL=0 # this will of course never happen, it just to clarify the nature of returns return $d } dayzToSalary(){ daysLeft=0 if [ $1 -lt 26 ]; then daysLeft=`expr 25 - $1` else lastDayInMonth=`date -d "`date +%Y%m01` +1 month -1 day" +%d` rest=`expr $lastDayInMonth - 25` daysLeft=`expr 25 + $rest` fi echo "Mate, it another $daysLeft days.." } # main _w getDay DAY # call getDay, save the result in the DAY variable dayzToSalary $DAY 
+2
Dec 14 '16 at 23:17
source share



All Articles