Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] Scripting help wanted (school assignment)
 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

 12-01-2013, 07:15 PM #1 mmmm13 LQ Newbie   Registered: Nov 2013 Posts: 9 Rep: 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?
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: 664

Rep:
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.
 12-01-2013, 08:04 PM #3 evo2 LQ Guru   Registered: Jan 2009 Location: Japan Distribution: Mostly Debian and Scientific Linux Posts: 5,753 Rep: 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.
 12-01-2013, 08:07 PM #4 kbp Senior Member   Registered: Aug 2009 Posts: 3,790 Rep: I usually use bc to perform maths on variables but the other options mentioned are equally valid. 1 members found this post helpful.
12-01-2013, 08:12 PM   #5
mmmm13
LQ Newbie

Registered: Nov 2013
Posts: 9

Original Poster
Rep:
Quote:
 Originally Posted by evo2 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?.

12-01-2013, 08:24 PM   #6
sag47
Senior Member

Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,850
Blog Entries: 36

Rep:
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: "
echo \$input```
Quote:
 Originally Posted by mmmm13 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 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.
12-01-2013, 08:29 PM   #7
mmmm13
LQ Newbie

Registered: Nov 2013
Posts: 9

Original Poster
Rep:

Quote:
 Originally Posted by sag47 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.

12-01-2013, 08:30 PM   #8
mmmm13
LQ Newbie

Registered: Nov 2013
Posts: 9

Original Poster
Rep:
Quote:
 Originally Posted by sag47 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.

12-01-2013, 09:29 PM   #9
sag47
Senior Member

Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,850
Blog Entries: 36

Rep:
Quote:
 Originally Posted by mmmm13 #!/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.

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post LXer Syndicated Linux News 0 08-23-2008 11:00 PM Hasjardl Programming 6 04-03-2008 09:48 PM colinstu General 11 11-03-2005 05:01 PM dasbooter Programming 9 08-16-2005 06:49 PM pingu Linux - General 1 02-08-2005 11:52 AM

LinuxQuestions.org

All times are GMT -5. The time now is 09:53 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -