operações matemáticas dos elementos da matriz no script bash [closed]

0

Usando o Ubuntu 13.10, bash e zenity, estou construindo um utilitário simples de talão de cheques. No código abaixo, a seção com todos os comandos de matemática comentados são os que eu tentei. Há também um problema com a parte do valor à direita do ponto decimal, conforme evidenciado pela seguinte mensagem de erro: / home / larry / bin / chkbk: linha 144: let: tmp [7] = 163.41-40.00 + 0: erro de sintaxe: operador aritmético inválido (token de erro é "0,41-40,00 + 0")  Eu apreciaria qualquer ajuda. obrigado ....

#! /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 
#! /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 %pre%7"
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 %pre%7"
reset
#   end of program
7" 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 %pre%7" reset # end of program
    
por user220880 14.02.2014 / 10:28

1 resposta

0

Float não é suportado diretamente na aritmética de shell, use bc para esse cálculo ou complexos. Exemplo:

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

Referência: Matemática nos Scripts do Shell , também uma pergunta semelhante: ShellScript UNIX, aritmética com ponto flutuante

    
por user.dz 14.02.2014 / 13:59

Tags