Returning information from functions in Linux bash script

April 27, 2021

Comments

Adsense

Adsense

Adsense

Adsense

You have learned to pass command-line parameters to functions. Similarly, the function can return integers as a return value. Normally, functions return either TRUE or FALSE. In certain cases, the function can return integer values, such as 5 or 10, as well.

The syntax is:

return N

When the function calls the command return, the function exits with the value specified by N.

If the function does not call the command return, then the exit status returned is that of the last command executed in the function. If what we need is the status of the last command executed in the function, then we need not return any value from the function. This is illustrated in the following script, function_14.sh:

function_14.sh

#!/bin/bash 
is_user_root() { [ $(id -u) -eq 0 ]; } 
is_user_root && echo "You are root user, you can go ahead." 
|| echo "You need to be administrator to run this script"

Test the script as follows:

$ chmod +x function_14.sh
$ ./function_14.sh

If you are a root user, then the output will be as follows:

Output:

You are root user, you can go ahead.

If you are a normal user, then the output will be as follows:

Output:

You need to be administrator to run this script

A modified version of the previous script is function_15.sh:

function_15.sh

#!/bin/bash 
declare -r TRUE=0 
declare -r FALSE=1 
 
is_user_root() 
{ 
[ $(id -u) -eq 0 ] && return $TRUE || return $FALSE 
} 
is_user_root && echo "You can continue" || echo "You need to be root to run this script."

Test the script as follows:

$ chmod +x function_15.sh
$ ./function_15.sh

This should produce the following output:

Output:

You need to be root to run this script.

Let’s see the script in which the function returns a value:

#!/bin/bash 
yes_or_no() 
{ 
echo "Is your name $*?" 
while true 
do 
echo -n "Please reply yes or no :" 
read reply 
case $reply in 
Y | y | yes ) return 0;; 
N | n | no ) return 1;; 
*) echo "Invalid answer" 
esac 
done 
} 
 
if yes_or_no $1 
then 
echo"Hello $1 " 
else 
echo"name not provided" 
fi 

Test the script as follows:

$ chmod +x function_16.sh
$ ./function_16.sh Ganesh

This should produce the following output:

Output:

Is your name Ganesh?
Please reply yes or no : yes
Hello Ganesh

Returning a word or string from a function

In shell scripts, functions cannot return a word or string from a function. If we need to pass data to a script then we will have to store it in a global variable. We can even use echo or print to send data to a pipe or redirect it to the log file.

Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Related Posts

Backup of files in Linux using command line

In IT or our day-to-day computer industry activities, taking backup is one of the most important activities. Previously, offices were required to keep important paper in a safe place; but if a fire breaks out, then everything is finished. In the digital world, taking...

Using awk in Linux

awk is a program that has its own programming language for performing data-processing and generating reports. The GNU version of awk is gawk. awk processes data, which can be received from a standard input, input file, or as the output of any other command or process....

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

one × 2 =

News & Updates

Join Our Newsletter

Adsense

Adsense

Adsense

Adsense