Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
- The problem statement, all variables and given/known data:
accept the selling price (range 5� to $1.05) as a command line argument (cents only); reject prices out of range or not multiples of 5; return to the operating system with a usage message
- Relevant commands, code, scripts, algorithms:
if statment
%
-
The attempts at a solution (include all code and scripts):
if [ $1 -ge 5 -a $1 -le 105 -a $(( $1 % 5 == 0 )) ]
then
echo The selling price has been set to $1
else
Invalid amount
-
Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Ryerson University, Toronto, Canada, Ikhari, CCPS 393
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
Your fi is missing. You're also out an echo.
$(( )) isn't part of the [ ] syntax, it gets substituted, so what you're doing amounts to "-a 0" or "-a 1" which doesn't make sense. I'd put it outside the [ ].
You certainly made a good start on it though! Only minor fixes needed:
if [ $1 -ge 5 -a $1 -le 105 ] && $(( $1 % 5 == 0 ))
then
echo The selling price has been set to $1
else
echo Invalid amount
fi
Before doing that if statement at all you should check if $1 is blank if [ -z "$1" ] ; then echo "no first parameter" ; exit ; fi otherwise that $(( )) will be a syntax error when $1 is blank.
thank you
---------- Post updated at 01:37 AM ---------- Previous update was at 12:44 AM ----------
Wait, still not working.... it works if no parameter entered and when parameter for else entered but not if number between 5 and 105 entered
this is what i have'
#!/bin/bash
if [ -z "$1" ]
then echo "no first parameter"
exit
elif [ $1 -ge 5 -a $1 -le 105 ] && $(( $1 % 5 == 0 ))
then
echo The selling price of pop has been set to $1 cents as per your request
else
echo Invalid amount. Price must be between 5 and 105 cents
fi
---------- Post updated at 02:25 AM ---------- Previous update was at 01:37 AM ----------
Ok got it....had to remove the $
elif [ $1 -ge 5 -a $1 -le 105 ] && $(( $1 % 5 == 0 ))
one more question. I want to skip the first echo statement the first time the loop gets entered
while [ $total_inserted -lt $selling_price ] #keep prompting for more funds until selling price achieved
do
echo "You have inserted a total of ${total_inserted} cents. Please insert $total_remaining more cents"
echo 'Please insert coins (NDQR)'
read coin_inserted
Otherwise the progam will tell user that he entered 0 cents, which is pointless. Any ideas???