Reputation: 127
in a script, I am returning negative integer from a function. I one envrionment with Bash Version : GNU bash, version 3.2.51(1)-release (x86_64-suse-linux-gnu) the script is running
and in other envrionment with Bash Version : GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) the script is not running and it terminates as soon as it gets return -1008 in a function. when I am trying to return positive integer instead of negative, its working fine.
I am not sure whether this is because the bash version difference. but what could be the issue ?
return -1008
Upvotes: 1
Views: 3008
Reputation: 20002
Return a value with an echo when you don't write other things to stdout:
exists_hdfs_folder()
{
local folder=$1
# info "Checking if \"${folder}\" exists in hdfs.... "
hadoop fs -test -d $folder 2>&1 > /dev/null
if [ "$?" -eq "$OK" ]; then
# info "HDFS FOLDER EXISTS - folder:${folder}"
echo "$OK"
else
# info "HDFS FOLDER DOES NOT EXIST - folder:${folder}"
echo "$ERROR_GENERIC_HDFS_FOLDER_NOT_EXISTS"
# offtopic: Better lowercase and braces like
# echo "${error_generic_hdfs_folder_not_exists}"
fi
}
And call that function with
returnvalue=$(exists_hdfs_folder)
I commented the info
function, it might write to stdout.
When you do want functions writing to stdout, you might use a variable
exists_hdfs_folder()
{
local folder=$1
# global
retval_exists_hdfs_folder="-99"
info "Checking if \"${folder}\" exists in hdfs.... "
hadoop fs -test -d $folder 2>&1 > /dev/null
if [ "$?" -eq "$OK" ]; then
info "HDFS FOLDER EXISTS - folder:${folder}"
retval_exists_hdfs_folder="$OK"
return 0
else
info "HDFS FOLDER DOES NOT EXIST - folder:${folder}"
retval_exists_hdfs_folder="${error_generic_hdfs_folder_not_exists}"
return 1
fi
}
And call that function with
exists_hdfs_folder || { echo "Failure: ${retval_exists_hdfs_folder}"; }
Upvotes: 0
Reputation: 212248
Return values are generally modulo 256:
#!/bin/sh
foo() { printf "Return $1: "; return $1; }
foo -5; echo $?
foo 300; echo $?
$ ./a.sh
Return -5: 251
Return 300: 44
Upvotes: 2