在bash中,如何在变量中存储返回值?

时间:2022-04-25 06:29:05

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.

我知道Linux中一些非常基本的命令,并且正在尝试编写一些脚本。我写了一个函数,它计算最后两位数的和的值。函数应该在最后两位数之间连接这个结果和并返回它。我之所以要返回这个值是因为我将在另一个函数中使用这个值。

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

如果我有12345,那么我的函数会计算4+5,返回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.

提前谢谢。

7 个解决方案

#1


8  

Change this line:

改变这条线:

return $value

like this:

是这样的:

echo $value

Then you can capture it's output.

然后你可以捕获它的输出。

#2


20  

The return value (aka exit code) is a value in the range 0 to 255 inclusive. It's used to indicate success or failure, not to return information. Any value outside this range will be wrapped.

返回值(即出口代码)是包含范围为0到255的值。它用来表示成功或失败,而不是返回信息。任何超出此范围的值都将被包装。

To return information, like your number, use

要返回信息,如你的号码,请使用

echo "$value"

To print additional information that you don't want captured, use

要打印不想捕获的附加信息,请使用

echo "my irrelevant info" >&2 

Finally, to capture it, use what you did:

最后,为了捕捉它,用你所做的:

 result=$(password_formula)

In other words:

换句话说:

echo "enter: "
        read input

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 >&2
        echo $sum >&2

        if [ $sum -gt 9 ]
        then
               sum=${sum:1}
        fi

        value=$second$sum$first
        echo $value
}
result=$(password_formula)
echo "The value is $result"

#3


5  

It is easy you need to echo the value you need to return and then capture it like below

很容易您需要返回您需要返回的值,然后像下面这样捕获它。

demofunc(){
    local variable="hellow"
    echo $variable    
}

val=$(demofunc)
echo $val

#4


3  

The answer above suggests changing the function to echo data rather than return it so that it can be captured.

上面的答案建议将函数更改为echo数据,而不是返回数据,以便捕获数据。

For a function or program that you can't modify where the return value needs to be saved to a variable (like test/[, which returns a 0/1 success value), echo $? within the command substitution:

对于一个函数或程序,您不能修改返回值需要保存到变量的地方(比如test/[返回0/1成功值),echo $?在命令替换:

# Test if we're remote.
isRemote="$(test -z "$REMOTE_ADDR"; echo $?)"
# Or:
isRemote="$([ -z "$REMOTE_ADDR" ]; echo $?)"

# Additionally you may want to reverse the 0 (success) / 1 (error) values
# for your own sanity, using arithmetic expansion:
remoteAddrIsEmpty="$([ -z "$REMOTE_ADDR" ]; echo $((1-$?)))"

E.g.

如。

$ echo $REMOTE_ADDR

$ test -z "$REMOTE_ADDR"; echo $?
0
$ REMOTE_ADDR=127.0.0.1
$ test -z "$REMOTE_ADDR"; echo $?
1
$ retval="$(test -z "$REMOTE_ADDR"; echo $?)"; echo $retval
1
$ unset REMOTE_ADDR
$ retval="$(test -z "$REMOTE_ADDR"; echo $?)"; echo $retval
0

For a program which prints data but also has a return value to be saved, the return value would be captured separately from the output:

如果程序打印数据,但也保存返回值,则返回值将与输出分开捕获:

# Two different files, 1 and 2.
$ cat 1
1
$ cat 2
2
$ diffs="$(cmp 1 2)"
$ haveDiffs=$?
$ echo "Have differences? [$haveDiffs] Diffs: [$diffs]"
Have differences? [1] Diffs: [1 2 differ: char 1, line 1]
$ diffs="$(cmp 1 1)"
$ haveDiffs=$?
$ echo "Have differences? [$haveDiffs] Diffs: [$diffs]"
Have differences? [0] Diffs: []

# Or again, if you just want a success variable, reverse with arithmetic expansion:
$ cmp -s 1 2; filesAreIdentical=$((1-$?))
$ echo $filesAreIdentical
0

#5


3  

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:

函数的返回代码只能是0到255之间的值。要将这个值存储在一个变量中,您必须像本例中那样:

#!/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

#6


1  

It's due to the echo statements. You could switch your echos to prints and return with an echo. Below works

这是由于echo语句。你可以把你的回声切换到指纹,然后返回一个回声。下面的工作

#!/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}
        print $second
        print $sum

        if [ $sum -gt 9 ]
        then
           sum=${sum:1}
        fi

        value=$second$sum$first
        echo $value
}
result=$(password_formula)
echo $result

#7


0  

Use the special bash variable "$?" like so:

使用特殊的bash变量“$?”

function_output=${my_function}
function_return_value=$?

#1


8  

Change this line:

改变这条线:

return $value

like this:

是这样的:

echo $value

Then you can capture it's output.

然后你可以捕获它的输出。

#2


20  

The return value (aka exit code) is a value in the range 0 to 255 inclusive. It's used to indicate success or failure, not to return information. Any value outside this range will be wrapped.

返回值(即出口代码)是包含范围为0到255的值。它用来表示成功或失败,而不是返回信息。任何超出此范围的值都将被包装。

To return information, like your number, use

要返回信息,如你的号码,请使用

echo "$value"

To print additional information that you don't want captured, use

要打印不想捕获的附加信息,请使用

echo "my irrelevant info" >&2 

Finally, to capture it, use what you did:

最后,为了捕捉它,用你所做的:

 result=$(password_formula)

In other words:

换句话说:

echo "enter: "
        read input

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 >&2
        echo $sum >&2

        if [ $sum -gt 9 ]
        then
               sum=${sum:1}
        fi

        value=$second$sum$first
        echo $value
}
result=$(password_formula)
echo "The value is $result"

#3


5  

It is easy you need to echo the value you need to return and then capture it like below

很容易您需要返回您需要返回的值,然后像下面这样捕获它。

demofunc(){
    local variable="hellow"
    echo $variable    
}

val=$(demofunc)
echo $val

#4


3  

The answer above suggests changing the function to echo data rather than return it so that it can be captured.

上面的答案建议将函数更改为echo数据,而不是返回数据,以便捕获数据。

For a function or program that you can't modify where the return value needs to be saved to a variable (like test/[, which returns a 0/1 success value), echo $? within the command substitution:

对于一个函数或程序,您不能修改返回值需要保存到变量的地方(比如test/[返回0/1成功值),echo $?在命令替换:

# Test if we're remote.
isRemote="$(test -z "$REMOTE_ADDR"; echo $?)"
# Or:
isRemote="$([ -z "$REMOTE_ADDR" ]; echo $?)"

# Additionally you may want to reverse the 0 (success) / 1 (error) values
# for your own sanity, using arithmetic expansion:
remoteAddrIsEmpty="$([ -z "$REMOTE_ADDR" ]; echo $((1-$?)))"

E.g.

如。

$ echo $REMOTE_ADDR

$ test -z "$REMOTE_ADDR"; echo $?
0
$ REMOTE_ADDR=127.0.0.1
$ test -z "$REMOTE_ADDR"; echo $?
1
$ retval="$(test -z "$REMOTE_ADDR"; echo $?)"; echo $retval
1
$ unset REMOTE_ADDR
$ retval="$(test -z "$REMOTE_ADDR"; echo $?)"; echo $retval
0

For a program which prints data but also has a return value to be saved, the return value would be captured separately from the output:

如果程序打印数据,但也保存返回值,则返回值将与输出分开捕获:

# Two different files, 1 and 2.
$ cat 1
1
$ cat 2
2
$ diffs="$(cmp 1 2)"
$ haveDiffs=$?
$ echo "Have differences? [$haveDiffs] Diffs: [$diffs]"
Have differences? [1] Diffs: [1 2 differ: char 1, line 1]
$ diffs="$(cmp 1 1)"
$ haveDiffs=$?
$ echo "Have differences? [$haveDiffs] Diffs: [$diffs]"
Have differences? [0] Diffs: []

# Or again, if you just want a success variable, reverse with arithmetic expansion:
$ cmp -s 1 2; filesAreIdentical=$((1-$?))
$ echo $filesAreIdentical
0

#5


3  

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:

函数的返回代码只能是0到255之间的值。要将这个值存储在一个变量中,您必须像本例中那样:

#!/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

#6


1  

It's due to the echo statements. You could switch your echos to prints and return with an echo. Below works

这是由于echo语句。你可以把你的回声切换到指纹,然后返回一个回声。下面的工作

#!/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}
        print $second
        print $sum

        if [ $sum -gt 9 ]
        then
           sum=${sum:1}
        fi

        value=$second$sum$first
        echo $value
}
result=$(password_formula)
echo $result

#7


0  

Use the special bash variable "$?" like so:

使用特殊的bash变量“$?”

function_output=${my_function}
function_return_value=$?