LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-01-2013, 07:15 PM   #1
mmmm13
LQ Newbie
 
Registered: Nov 2013
Posts: 9

Rep: Reputation: Disabled
Question Scripting help wanted (school assignment)


The script requires me to write this:

reads and sets the variable M
reads and sets the variable R
reads and sets the variable T
sum M, R, and T in a variable A
Evaluate A to determine if its greater than 2000
if A is greater than 2000, print on the screen "A is over 2000"
if A is less than 2000, print on the screen "A is 2000 or less"

what I have so far:

read M
read R
read T
while "$M" "$R" "$T" = "A"
read A
if [ "$A" > "2000" ]
then
echo "A is over 2000"
else
if [ "$A" < "2000" ]
then
echo "A is 2000 or less"
fi
fi

not sure how to sum M, R, T maybe : A = M + R + T?
And really don't know where to input it...

and I think I have the top portion wrong as well: "$M".....

can someone give me clarity?
 
Old 12-01-2013, 07:58 PM   #2
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
there is no requirement of while here:

Quote:
A=`expr $M + $R + $T`
would be a help.. or you can use "let" to calculate the sum too.
 
1 members found this post helpful.
Old 12-01-2013, 08:04 PM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

if M, R, and T are integers you can let the shell itself do the sum (assuming bash). eg
Code:
A=$((M+R+T))
Also, you should probably be using -lt instead of < and -gt instead of > for your comparison operators.

Evo2.
 
1 members found this post helpful.
Old 12-01-2013, 08:07 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I usually use bc to perform maths on variables but the other options mentioned are equally valid.
 
1 members found this post helpful.
Old 12-01-2013, 08:12 PM   #5
mmmm13
LQ Newbie
 
Registered: Nov 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,

if M, R, and T are integers you can let the shell itself do the sum (assuming bash). eg
Code:
A=$((M+R+T))
Also, you should probably be using -lt instead of < and -gt instead of > for your comparison operators.

Evo2.
got to use the -gt and -lt. what I don't really understand is the read M, R and T part in the beginning portion of the script...would A=$((M+R+T))give me the sum for variable A?.
 
Old 12-01-2013, 08:24 PM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Are we to assume this assignment should be in bash as the scripting language?

You should likely tell your user what exactly they're inputting. If you just show a user a blank line they won't know what to do. See the echo man page and look up the -n option.

Code:
man echo
#e.g.
echo -n "Enter a number: "
read input
echo $input
Quote:
Originally Posted by mmmm13 View Post
not sure how to sum M, R, T maybe : A = M + R + T?
You're correct it means A = M + R + T. You're not currently doing any arithmetic. Under Compound Commands see the (( expression )) for how to do arithmetic in bash. I'll give you two examples of arithmetic.
Code:
A=1
B=2
((C=A+B))
#or using substitution
C=$((A+B))
Quote:
Originally Posted by mmmm13 View Post
if [ "$A" > "2000" ]
You're not using the [ command (also know as /usr/bin/[ and also the test command) correctly. You need to read the man page for the test command and use it properly.

Code:
man test
Hopefully I haven't made it too easy for you but reading the bash manual really would resolve all of the questions you have. I've read it myself many times.

Last edited by sag47; 12-02-2013 at 09:09 AM.
 
1 members found this post helpful.
Old 12-01-2013, 08:29 PM   #7
mmmm13
LQ Newbie
 
Registered: Nov 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by sag47 View Post
Are we to assume this assignment should be in bash as the scripting language?

You should likely tell your user what exactly they're inputting. If you just show a user a blank line they won't know what to do. See the echo man page and look up the -n option.

Code:
man echo
#e.g.
echo -n "Enter a number: "
read input
echo $input


You're corret it means A = M + R + T. You're not currently doing any arithmatic. Under Compound Commands see the (( expression )) for how to do arithmatic in bash. I'll give you two examples of arithmatic.
Code:
A=1
B=2
((C=A+B))
#or using substitution
C=$((A+B))


You're not using the [ command (also know as /usr/bin/[ and also the test command) correctly. You need to read the man page for the test command and use it properly.

Code:
man test
Hopefully I haven't made it too easy for you but reading the bash manual really would resolve all of the questions you have. I've read it myself many times.
I have figured it out by all the people that posted. I used this:

#!/bin/bash
let M=0
let R=0
let T=0
A=$((M + R + T))
if [ "$A" -gt "2000" ]
then
echo "A is over 2000"
else
if [ "$A" -le "2000" ]
then
echo "A is 2000 or less"
fi
fi

if I change the values for each M, R, and T it gives me the echo that is supposed to be for -gt or -le.

Thank you all for the input.
 
Old 12-01-2013, 08:30 PM   #8
mmmm13
LQ Newbie
 
Registered: Nov 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sag47 View Post
Are we to assume this assignment should be in bash as the scripting language?

You should likely tell your user what exactly they're inputting. If you just show a user a blank line they won't know what to do. See the echo man page and look up the -n option.

Code:
man echo
#e.g.
echo -n "Enter a number: "
read input
echo $input


You're corret it means A = M + R + T. You're not currently doing any arithmatic. Under Compound Commands see the (( expression )) for how to do arithmatic in bash. I'll give you two examples of arithmatic.
Code:
A=1
B=2
((C=A+B))
#or using substitution
C=$((A+B))


You're not using the [ command (also know as /usr/bin/[ and also the test command) correctly. You need to read the man page for the test command and use it properly.

Code:
man test
Hopefully I haven't made it too easy for you but reading the bash manual really would resolve all of the questions you have. I've read it myself many times.
It was bash, sorry.
 
Old 12-01-2013, 09:29 PM   #9
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by mmmm13 View Post
#!/bin/bash
let M=0
let R=0
let T=0
A=$((M + R + T))
if [ "$A" -gt "2000" ]
then
echo "A is over 2000"
else
if [ "$A" -le "2000" ]
then
echo "A is 2000 or less"
fi
fi

if I change the values for each M, R, and T it gives me the echo that is supposed to be for -gt or -le.

Thank you all for the input.
Your assignment is still not complete. You need to read and set M/R/T. I gave you an example of how to read and set a variable in my previous post. Also, your use of the last if statement is unnecessary. One would assume that if it's not greater than 2000 then all other cases would be less than or equal to 2000 (with the assumption that it is always a number comparison). In that case only a "if else" is necessary. You should also note the assumption of valid input only from the user.

Last edited by sag47; 12-01-2013 at 09:30 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Skegness Grammar School, using GNU/Linux and thin-clients across the school LXer Syndicated Linux News 0 08-23-2008 11:00 PM
Scripting Examples Wanted Hasjardl Programming 6 04-03-2008 09:48 PM
Survey (assignment I got from school, due monday) colinstu General 11 11-03-2005 05:01 PM
scripting guru wanted (low pay but emotionally rewarding) dasbooter Programming 9 08-16-2005 06:49 PM
Contact with school-net admins wanted, for migrating discussion. pingu Linux - General 1 02-08-2005 11:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration