Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Trouble with bash script

$
0
0
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 receive 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.

Viewing all articles
Browse latest Browse all 51036

Trending Articles