Bash 脚本中数组元素的数学运算

Bash 脚本中数组元素的数学运算

使用 Ubuntu 13.10、bash 和 zenity,我正在构建一个简单的支票簿实用程序。在下面的代码中,所有带注释的数学命令部分都是我尝试过的。小数点右侧的值部分也存在问题,如以下错误消息所示:/home/larry/bin/chkbk:第 144 行:let:tmp[7]=163.41-40.00+0:语法错误:无效算术运算符(错误标记是“.41-40.00+0”)我将不胜感激任何帮助。谢谢……

#! /bin/bash
#
#####################################
#  Check Book Utility               #
#  --using chkbk as program         #
#  --using chkdat as data file      #
#  --using chkhlp as help file      #
#####################################
#
##################
#  Declarations  #
##################
declare -a chk=()
declare -a hlp=()
declare -a tmp=()

declare -r patdat="/home/larry/bin/chkdat"
declare -r pathlp="/home/larry/bin/chkhlp"
#   end declarations

###############
#  Functions  #
###############
loadfiles() {
zenity --progress\
    --title=Initializing....\
    --no-cancel\
    --text='Loading....'\
    --pulsate\
    --auto-close\
    --width=250\
    --height=50\
    --timeout=3

    if [ -e ~/bin/chkdat ];
      then
        let i=1
        while IFS=$'\n' read -r line_data; do
            chk[i]="${line_data}"
            ((++i))
        done < ~/bin/chkdat;
    else
        > ~/bin/addrdata;
    fi

    if [ -e ~/bin/chkhlp ];
      then
        let i=1
        while IFS=$'\n' read -r line_data; do
            hlp[i]="${line_data}"
            ((++i))
        done < ~/bin/chkhlp;
    else
        > ~/bin/chkhlp;
    fi
}   # end loadfiles function

main() {
ans=$(zenity  --list\
    --radiolist\
    --text "Select:"\
    --width 300\
    --height 250\
    --title "Main Menu"\
    --column "Choice"\
    --column "Action"\
    FALSE "Add New Transaction"\
    FALSE "Edit Transaction"\
    FALSE "Search Transactions"\
    FALSE "View Help"\
    TRUE "Exit Program"\
    --hide-header)  # this must be at the bottom

    case $ans in
        "Add New Transaction")
            d=$(zenity --calendar\
            --title "Add Transaction "\
            --text "Click OK for today's date"\
            --date-format='%d %b %Y')
            tmp[1]=${d}
            a=$(zenity --list --radiolist\
                --title "Add Transaction"\
                --text "Account Codes"\
                --column "Choice"\
                --column "Action"\
                --width 300\
                --height 500\
                FALSE "000 Null Account"\
                TRUE "101 SSI"\
                FALSE "102 SSA"\
                FALSE "103 Gifts"\
                FALSE "104 Other"\
                FALSE "201 Rent"\
                FALSE "202 Electric"\
                FALSE "203 General"\
                FALSE "204 Food"\
                FALSE "205 Clothing"\
                FALSE "206 Pet Care"\
                FALSE "207 Charity"\
                FALSE "208 Health Care"\
                FALSE "209 Transportation"\
                --hide-header)
                tmp[2]=${a:0:3}
            m=$(zenity --list --radiolist\
                --title "Add Transaction"\
                --text "Method Codes"\
                --column "Choice"\
                --column "Action"\
                --width 300\
                --height 250\
                TRUE "bb Beginning Balance"\
                FALSE "ck Check"\
                FALSE "cs Cash"\
                FALSE "dc Debit Card"\
                FALSE "dd Direct Deposit"\
                --hide-header)
                tmp[3]=${m:0:2}

            v=$(zenity --entry\
                --title "Add Transaction"\
                --text "Enter Payee / Payor"\
                --width 400)
                tmp[4]=${v}

            t=$(zenity --entry\
                --title "Add Transaction"\
                --text "Enter amount of transaction:")
            if [ ${a:0:3} -lt 200 ]; then 
                tmp[5]=0
                tmp[6]=${t};
            else 
                tmp[5]=${t}
                tmp[6]=0;
            fi

        if [ ${a:0:1} -eq 0 ]; then
            tmp[7]=${tmp[6]}
        else # [ ${a:0:1} -gt 0 ]; then
            #echo "tmp[7] = chk[7] - tmp[5] + tmp[6]"
            #tmp[7]=`expr ${chk[7]} - ${tmp[5]} + ${tmp[6]}`
            #$(( tmp[7] = ${chk[7]} - ${tmp[5]} + ${tmp[6]} ))
            let x=${#chk[@]}-1
            #$(( tmp[7] = chk[x] - tmp[5] + tmp[6] ))
            #let tmp[7]="${chk[x]}"
            #let tmp[7]="${tmp[7]}"-"${tmp[5]}"
            #let tmp[7]="${tmp[7]}"+"${tmp[6]}"
            #let tmp[7] = chk[7] - tmp[5] + tmp[6]
            #tmp[7]=${chk[x]}-${tmp[5]}+${tmp[6]}
            #tmp[7]=chk[x]-tmp[5]+tmp[6]
            let tmp[7]=chk[x]-tmp[5]+tmp[6]
        fi;

        zenity --question\
            --title="Add Transaction"\n\
            --text="You have entered:\n\
               Date:  ${d}\n\
            Account:  ${a}\n\
             Method:  ${m}\n\
             Vendor:  ${v}\n\
            Amounmt:  ${t}\n\n\
            Are these entries correct?"
        case $? in
            0)
                for i in $(seq 1 7); do
                    chk=( "${chk[@]}" "${tmp[i]}" )
                    echo "${tmp[i]}" >> ~/bin/chkdat
                done
                main
                ;;
            1)  
                main;;
        esac
        ;;

         "Edit Transaction")
            let x=${#chk[@]}
            ret=$(for i in $(seq 0 $x)
                do
                    echo ${chk[i]}
                done | yad --list\
                    --title "  Checkbook"\
                    --text "  Edit Check Records:"\
                    --column Date\
                    --column Account\
                    --column Method\
                    --column 'Pay To/From'\
                    --column Debit\
                    --column Credit\
                    --column Balance\
                    --width 800\
                    --height 450\
                    --center\
                    --editable)
            main
            ;;
         "Search Transactions")
            let x=${#chk[@]}
             ret=$(for i in $(seq 0 $x)
                do
                    echo ${chk[i]}
                done | yad --list\
                    --title "  Checkbook"\
                    --text "  View Check Records:"\
                    --column Date\
                    --column Account\
                    --column Method\
                    --column 'Pay To/From'\
                    --column Debit\
                    --column Credit\
                    --column Balance\
                    --width 800\
                    --height 450\
                    --center)
            main
            ;;
         "View Help")
            zenity --text-info\
            --title "Checkbook Help"\
            --filename=/home/larry/bin/chkhlp\
            --width=450\
            --height=450;
            main
            ;;
         "Exit Program")
            ;;
    esac
}   # end main function
#   end functions

##################
#  Main Program  #
##################
echo -en "\e]0;  Check Book \007"
echo -en "\e[0;46m"; clear
yad --text-info\
    --timeout=5\
    --borders=0\
    --width=300\
    --height=50\
    --center\
    --no-buttons\
    --fixed\
    --title=Welcome\
    --text="Larry\'s Checkbook\n\n version 1.0.0\n\n 2014 Avdiel Data Solutions"\
    --text-align=center
loadfiles
main
echo -en "\e]0;  Larry's Bash \007"
reset
#   end of program

答案1

浮点数不直接支持 shell 运算,请使用浮点数bc进行此类计算或复杂计算。例如:

result=`echo "163.41-40.00+0" | bc`
echo $result

参考: Shell 脚本中的数学,还有类似的问题:UNIX ShellScript,浮点运算

相关内容