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

前端 未结 7 2174
臣服心动
臣服心动 2020-11-29 09:06

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 fun

7条回答
  •  忘掉有多难
    2020-11-29 09:41

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

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

提交回复
热议问题