LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-17-2007, 12:13 PM   #1
dhonnoll78
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Rep: Reputation: 0
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?
 
Old 12-17-2007, 12:38 PM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
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.

Last edited by Hobbletoe; 12-17-2007 at 12:40 PM.
 
Old 12-17-2007, 12:50 PM   #3
dhonnoll78
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks but this is a korn shell looks like anytime I say use a command not found I get ksh :wrongcommand: not found
 
Old 12-17-2007, 01:02 PM   #4
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
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
 
Old 12-17-2007, 01:13 PM   #5
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
#!/bin/dash

Quote:
Originally Posted by Hobbletoe View Post
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.
 
Old 12-17-2007, 01:22 PM   #6
drewmeister
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Rep: Reputation: 0
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.
 
Old 12-17-2007, 05:34 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,251

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Simple Shell Script Help Kristijan Programming 3 06-13-2005 09:13 PM
simple shell script sharpie Programming 9 06-02-2004 11:47 PM
a simple shell script Warchief Programming 1 07-31-2003 05:01 AM
Very Simple Shell Script cli_man Linux - Software 5 04-24-2003 10:14 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration