LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Need help getting started simple simple shell script (http://www.linuxquestions.org/questions/programming-9/need-help-getting-started-simple-simple-shell-script-607383/)

dhonnoll78 12-17-2007 12:13 PM

Need help getting started simple simple shell script
 
OKAY I need help getting started with a simple and dumb shell script but we all have to start somewhere right?
Okay Here is what I have so far
#!/bin/sh
#by Drew Honnoll
#Just a dumb script to practice user imput and screen oupt
NEWAGE=
GRANDCHILDREN=
echo "Please enter your name and press enter"
read name
echo "Please enter the number of children you have and press enter"
read children
echo "Please enter your age when you had your last child and press enter"
read age
echo "Now, $name if your children have 2 children each by the time they are at least 25, then by the time you are $NEWAGE you will have $GRANDCHILDREN grandchildren"

What I don't know and what every attempt of mine has failed is getting NEWAGE and GRANDCHILDREN set up
NEWAGE needs to be a math equation of $age + 25
and GRANDCHILDREN needs to be an equation of $children * 2 How do I go about doing that?

Hobbletoe 12-17-2007 12:38 PM

Try the $(()) expersion. It is used in BASH to do arthimetic.

Code:

NEWAGE=$(( $age + 25 ))
Check out the Advanced Bash-Scripting Guide, Section 9.7 for more information.

dhonnoll78 12-17-2007 12:50 PM

Thanks but this is a korn shell looks like anytime I say use a command not found I get ksh :wrongcommand: not found

Hobbletoe 12-17-2007 01:02 PM

Your first line (#!/bin/sh) should be telling the computer to execute the remainder of the script as if it were in the Bourne shell (sh). Most Linux distributions have a link from sh to bash (Bourne Again Shell). (This is why I had assumed that you were using bash.) If you want to run it in Korn, it should read

Code:

#!/bin/ksh
I don't do much in Korn (ok, nothing actually), but have tried the same commands, and it should work. The error that you are getting makes it look like you are trying to run something called "wrongcommand" from the command line.

Did you remember to change the permissions on your script so that it can be run? If your script is say age.sh, then you would want to add execute permissions to it.

Code:

chmod u+x age.sh
Then run the script.

Code:

./age.sh

cmnorton 12-17-2007 01:13 PM

#!/bin/dash
 
Quote:

Originally Posted by Hobbletoe (Post 2993656)
Your first line (#!/bin/sh) should be telling the computer to execute the remainder of the script as if it were in the Bourne shell (sh). Most Linux distributions have a link from sh to bash (Bourne Again Shell). (This is why I had assumed that you were using bash.) If you want to run it in Korn, it should read

Code:

#!/bin/ksh
I don't do much in Korn (ok, nothing actually), but have tried the same commands, and it should work. The error that you are getting makes it look like you are trying to run something called "wrongcommand" from the command line.

Did you remember to change the permissions on your script so that it can be run? If your script is say age.sh, then you would want to add execute permissions to it.

Code:

chmod u+x age.sh
Then run the script.

Code:

./age.sh

You might not get what you expect on Ubuntu if you assume #!/bin/sh points to #!/bin/bash. #!/bin/sh points to /bin/dash, and you might find the need to use #!/bin/bash explicitly.

drewmeister 12-17-2007 01:22 PM

Drew,

I would use the let function built into bash:

# No need to declare variables in bash unless you want to specify type.

age=35
children=3
let NEWAGE=age+25
let GRANDS=children*2
echo "$NEWAGE"
echo "$GRANDS"

I put in the echoes to check it.

chrism01 12-17-2007 05:34 PM

Never just accept the default shell, always specify exactly which one you want used. That way you know which syntax applies...


All times are GMT -5. The time now is 05:20 AM.