Results 1 to 3 of 3
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 01-29-2013 #1Just Joined!
- Join Date
- Jan 2013
- Posts
- 1
problem with bash script
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:
...etc.Code:#!/bin/bash echo testing with 94 ./makecents.sh 94 echo testing with 0 ./makecents.sh 0
Here is my code:
I also tried it a different way.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
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.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 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
- 01-29-2013 #2Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 415
We do not answer assignments here. The problem that you are having is because spaces are very important as field seperators and when they are placed where they break a field into two and one field is expected it breaks.
Here is a question to get you started:
What happens when you do the following from the command line?
Code:cents=94 let quarters=$cents / 25 echo $quarters
Why does this make a difference??
Now what happens when you do the following from the command line?
Code:cents=94 let quarters=$cents/25 echo $quarters
- 01-29-2013 #3Linux Newbie
- Join Date
- Nov 2012
- Posts
- 135
hi,
you should indent correctly your code.
you'll see you'vegot a problem with your conditions.


Reply With Quote
