Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Using Ubuntu 13.10, bash and zenity, I am building a simple checkbook utility. In the code below, the section with all the commented math commands are the ones that I have tried. There is also a problem with the part of the value to the right of the decimal point as evidenced by the following error message: /home/larry/bin/chkbk: line 144: let: tmp[7]=163.41-40.00+0: syntax error: invalid arithmetic operator (error token is ".41-40.00+0") I would appreciate any help. thanks....

#! /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
share|improve this question

closed as off-topic by karel, Olli, Amith KK, souravc, mikewhatever Feb 15 at 18:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – karel, Olli, Amith KK, souravc, mikewhatever
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 0 down vote accepted

Float is not supported directly in shell arithmetic, use bc for such calculation or complex ones. Example:

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

Reference: Math in Shell Scripts , also similar question: UNIX ShellScript, arithmetic with floating point

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.