I have to write two scripts, one in bash and one in dos. My dos one works perfectly but I'm having trouble with my bash one. The assignment is to make a script that takes a command line argument (from a test script) and return the change. So if the number is 94, the script will say
There are 3 quarters.
there are 1 dimes.
There are 1 nickels
There are 4 pennies.

if the number is 25 it will say that is 1 quarter. if the initial number is 0, give a message saying there is 0 cents entered. If the number is negative, say can't have negative cents, etc.

example of test script:

#!/bin/bash

echo testing with 94
./makecents.sh 94

echo testing with 0
./makecents.sh 0
 ...etc.

Here is my code:

#!/bin/bash

let cents=$1
let cents2=$cents

let quarters=$cents / 25
let cents=$cents % 25
let dimes=$cents / 10
let cents=$cents % 10
let nickels=$cents / 5
let cents=$cents % 5
let pennies=$cents

if [ $cents2 -eq 0 ]
then
        echo Zero cents entered, no change returned.
        exit
elif [ $cents -lt 0 ]
then
        echo Cents can't be negative.
        exit
elif [ $cents -eq 0 ]
then
if [ $quarters -ne 0 ]
then
echo There are $quarters quarters.
fi
if [ $dimes -ne 0 ]
then
echo There are $dimes dimes.
fi
if [ $nickels -ne 0 ]
then
echo There are $nickels nickels.
fi
exit
fi
elif [ $cents -ne 0 ]
then
if [ $cents -ne 0 ]
then
if [$quarters -ne 0 ]
then
echo There are $quarters quarters.
fi
if [ $dimes -ne 0 ]
then
echo There are $dimes dimes.
fi
if [ $nickels -ne 0 ]
then
echo There are $nickels nickels.
fi
if [ $pennies -ne 0 ]
then
echo There are $pennies pennies.
exit
fi

I also tried it a different way.

#!/bin/bash

let cents=$1
let cents2=$cents

let quarters=$cents / 25
let cents=$cents % 25
let dimes=$cents / 10
let cents=$cents % 10
let nickels=$cents / 5
let cents=$cents % 5
let pennies=$cents

if [ $cents2 -eq 0 ]
then
        echo Zero cents entered, no change returned.
        exit
fi
if [ $cents -lt 0 ]
then
        echo Cents can't be negative.
        exit
fi
if [ $cents -eq 0 ] && [ $quarters -ne 0 ]
then
echo There are $quarters quarters.
exit
fi
if [$cents -eq 0 ] && [ $dimes -ne 0 ]
then
echo There are $dimes dimes.
exit
fi
if [ $cents -eq 0  ]&& [ $nickels -ne 0 ]
then
echo There are $nickels nickels.
exit
fi

if [ $cents -ne 0 ] && [$quarters -ne 0 ]
then
echo There are $quarters quarters.
exit
fi
if [ $cents -ne 0 ] && [ $dimes -ne 0 ]
then
echo There are $dimes dimes.
exit
fi
if [ $cents -ne 0 ] && [ $nickels -ne 0 ]
then
echo There are $nickels nickels.
exit
fi
if [ $cents -ne 0 ] && [ $pennies -ne 0 ]
then
echo There are $pennies pennies.
exit
fi

Both of them recieve a bunch of syntax errors. All I'm going off of is a few power point slides that shows the basic syntax =/. If someone could tell me what I'm doing wrong I'd appreciate it. I've also included a file with my dos scripts that seem to be working fine.

Member Avatar for b1izzard

I just modified your second shell script code, You added extra spaces in 'let' statement assignments and You missed out spaces between '[' and arithmetic expression in 'if' statements at line 29,40.

#!/bin/bash

    let cents=$1
    let cents2=$cents
    let quarters=$cents/25
    let cents=$cents%25
    let dimes=$cents/10
    let cents=$cents%10
    let nickels=$cents/5
    let cents=$cents%5
    let pennies=$cents
    if [ $cents2 -eq 0 ]
    then
        echo Zero cents entered, no change returned.
        exit
    fi
    if [ $cents -lt 0 ]
    then
        echo Cents can\'t be negative.
        exit
    fi
    if [ $cents -eq 0 ] && [ $quarters -ne 0 ]
    then
        echo There are $quarters quarters.
        exit
    fi
    if [ $cents -eq 0 ] && [ $dimes -ne 0 ]
    then
        echo There are $dimes dimes.
        exit
    fi
    if [ $cents -eq 0  ]&& [ $nickels -ne 0 ]
    then
        echo There are $nickels nickels.
        exit
    fi
    if [ $cents -ne 0 ] && [ $quarters -ne 0 ]
    then
        echo There are $quarters quarters.
        exit
    fi
    if [ $cents -ne 0 ] && [ $dimes -ne 0 ]
    then
        echo There are $dimes dimes.
        exit
    fi
    if [ $cents -ne 0 ] && [ $nickels -ne 0 ]
    then
        echo There are $nickels nickels.
        exit
    fi
    if [ $cents -ne 0 ] && [ $pennies -ne 0 ]
    then
        echo There are $pennies pennies.
        exit
    fi

FYI: You need to modify your logic to get the desired results.

Member Avatar for dfl1954

I did it this way

#!/bin/bash

typeset -i amount=$1 wamount wchange
wamount=${amount}
typeset -A size

size[quarter]=25
size[dime]=10
size[nickel]=5
size[penny]=1

typeset -A change

if [ $# != 1 ];then
    echo needs a valid \$1
    exit
fi
if [[ $amount < 1  ]];then
    echo \$1 must be a positive integer
    exit
fi

for coin in quarter dime nickel penny
do
    if [[ ${size[$coin]} -le $wamount ]];
    then
        (( wchange = $wamount / ${size[$coin]} ))
        change[$coin]=$wchange
        (( wamount = $wamount - ( $wchange * ${size[$coin]} ) )) 
    else
        change[$coin]=0
    fi
done


for coin in quarter dime nickel penny
do
    echo ${change[$coin]} $coin
done

It can be more economic, but this is how I did it. Well done for debugging the original.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.