[SOLVED] Scripting help wanted (school assignment)
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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".....
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?.
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
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.
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.
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.
#!/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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.