HELP! If statement with remainder testing

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions HELP! If statement with remainder testing
# 1  
Old 05-13-2011
HELP! If statement with remainder testing

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. 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


2. Relevant commands, code, scripts, algorithms:

if statment
%

3. 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


4. 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).
# 2  
Old 05-13-2011
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:

Code:
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.
# 3  
Old 05-13-2011
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'

Code:
#!/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 $

Code:
elif [ $1 -ge 5 -a $1 -le 105 ] && $(( $1 % 5 == 0 ))


Last edited by pludi; 05-13-2011 at 07:41 AM.. Reason: didn't see reply
# 4  
Old 05-14-2011
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???
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script to get total size off of remainder calculated

I am working on a script to get the final total size and so far have the following and wondering if this can be improved. # Compare the desired size of each lvm to the standard size. If it is desired is larger than calculate the difference and keep that value as the amount to add to that LVM. ... (5 Replies)
Discussion started by: user3528
5 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. Shell Programming and Scripting

Use sed to delete remainder of line after pattern

Greetings, I need some assistance here as I cannot get a sed line to work properly for me. I have the following list: Camp.S01E04.720p.HDTV.X264-DIMENSION Royal.Pains.S05E07.720p.HDTV.x264-IMMERSE Whose.Line.is.it.Anyway.US.S09E05.720p.HDTV.x264-BAJSKORV What I would like to accomplish is to... (3 Replies)
Discussion started by: choard
3 Replies

5. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

6. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

7. Shell Programming and Scripting

Remainder of x/y in shell script

Hi, Can anyone help me with the syntax for finding out remainder of two integers. Ex: 10/3 ... it should return 1. Is fmod function will be helpful here? I am not able to find out usage of it. Kindly help. Regards, Malay Maru (3 Replies)
Discussion started by: malaymaru
3 Replies

8. UNIX for Advanced & Expert Users

testing

what is the difference between white box and black box testing? (1 Reply)
Discussion started by: areef4u
1 Replies
Login or Register to Ask a Question