Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In bash, how to store a return value in a variable?

Tags:

linux

bash

I know some very basic commands in Linux and am trying to write some scripts. I have written a function which evaluates the sum of last 2-digits in a 5-digit number. The function should concatenate this resultant sum in between the last 2-digits and return it. The reason I want to return this value is because I will be using this value in the other function.

Ex: if I have 12345, then my function will calculate 4+5 and return 495.

#!/bin/bash  set -x echo "enter: "         read input  function password_formula {         length=${#input}         last_two=${input:length-2:length}         first=`echo $last_two| sed -e 's/\(.\)/\1 /g'|awk '{print $2}'`         second=`echo $last_two| sed -e 's/\(.\)/\1 /g'|awk '{print $1}'`         let sum=$first+$second         sum_len=${#sum}         echo $second         echo $sum          if [ $sum -gt 9 ]         then                sum=${sum:1}         fi          value=$second$sum$first         return $value } result=$(password_formula) echo $result 

I am trying to echo and see the result but I am getting the output as shown below.

-bash-3.2$ ./file2.sh  +++ password_formula +++ echo 'enter: ' +++ read input 12385 +++ length=8 +++ last_two=85 ++++ echo 85 ++++ sed -e 's/\(.\)/\1 /g' ++++ awk '{print $2}' +++ first=5 ++++ echo 85 ++++ sed -e 's/\(.\)/\1 /g' ++++ awk '{print $1}' +++ second=8 +++ let sum=5+8 +++ sum_len=2 +++ echo 5 +++ echo 8 +++ echo 13 +++ '[' 13 -gt 9 ']' +++ sum=3 +++ value=835 +++ return 835 ++ result='enter:  5 8 13' ++ echo enter: 5 8 13 enter: 5 8 13 

I also tried to print the result as:

password_formula RESULT=$? echo $RESULT 

But that is giving some unknown value:

++ RESULT=67 ++ echo 67 67 

How can I properly store the correct value and print (to double check) on the screen?

Thanks in advance.

like image 690
itsh Avatar asked Feb 21 '13 22:02

itsh


People also ask

How does bash store return value?

Bash function can return a string value by using a global variable. In the following example, a global variable, 'retval' is used. A string value is assigned and printed in this global variable before and after calling the function. The value of the global variable will be changed after calling the function.

How do you save the output of a shell command to a variable?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]

What does $_ mean in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.


1 Answers

Simplest answer:

the return code from a function can be only a value in the range from 0 to 255 . To store this value in a variable you have to do like in this example:

#!/bin/bash  function returnfunction {     # example value between 0-255 to be returned      return 23 }  # note that the value has to be stored immediately after the function call : returnfunction myreturnvalue=$?  echo "myreturnvalue is "$myreturnvalue 
like image 61
Samuele Catuzzi Avatar answered Sep 22 '22 18:09

Samuele Catuzzi