Linux script, add two numbers

Where Penguins and Daemons chill together in the warmth of the Sun.

Moderators: SecretSquirrel, notfred

Linux script, add two numbers

Postposted on Fri Mar 20, 2009 12:50 pm

Can anyone write a simple linux script (than can be run in the terminal) that will take the input of two numbers (maximum two digits), echo back to the screen and then add them up and echo the total? I want to see how this would look so I can start playing with some scripting.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: Linux script, add two numbers

Postposted on Fri Mar 20, 2009 1:27 pm

Code: Select all
> cat plus.sh
#!/bin/sh
echo $1 + $2 = `expr $1 + $2`
>


Code: Select all
> ./plus.sh 3 4
3 + 4 = 7
>
notfred
Grand Gerbil Poohbah
 
Posts: 3760
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Linux script, add two numbers

Postposted on Fri Mar 20, 2009 2:26 pm

notfred wrote:
Code: Select all
> cat plus.sh
#!/bin/sh
echo $1 + $2 = `expr $1 + $2`
>


Code: Select all
> ./plus.sh 3 4
3 + 4 = 7
>


I'm logging onto a unix server at San Francisco State University using SSH to do a lot of my practice and plus.sh is not a valid command.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: Linux script, add two numbers

Postposted on Fri Mar 20, 2009 3:22 pm

It's like you need to create a file plus.sh with the content suggested? I am guessing you never used Linux before, hehe.
radix
Gerbil First Class
 
Posts: 103
Joined: Sun Jan 13, 2002 7:00 pm
Location: Mountain View, CA

Re: Linux script, add two numbers

Postposted on Fri Mar 20, 2009 4:02 pm

radix wrote:It's like you need to create a file plus.sh with the content suggested? I am guessing you never used Linux before, hehe.


Well I did use vi to create a file named plus.sh with that contents and I'm still getting no result. I'm definitely a noobie with the terminal as most of my time with Linux is spent in a GUI.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: Linux script, add two numbers

Postposted on Fri Mar 20, 2009 4:14 pm

At the prompt type "chmod 700 plus.sh", then "./plus.sh".
Think for yourself, schmuck!
i5-2500K@4.3|Asus P8P67-LE|8GB DDR3-1600|Powercolor R7850 2G|1.5TB 7200.11|1988 Model M|Saitek X-45 & P880|Logitech MX 518|Dell 2209WA|Sennheiser PC151|Asus Xonar DX
bthylafh
Grand Gerbil Poohbah
 
Posts: 3219
Joined: Mon Dec 29, 2003 11:55 pm
Location: Southwest Missouri, USA

Re: Linux script, add two numbers

Postposted on Fri Mar 20, 2009 4:56 pm

bthylafh wrote:At the prompt type "chmod 700 plus.sh", then "./plus.sh".


I used chmod uog+rwx plus.sh which should be fine as well.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: Linux script, add two numbers

Postposted on Fri Mar 20, 2009 6:31 pm

dmitriylm wrote:I used chmod uog+rwx plus.sh which should be fine as well.


chmod 777 plus.sh is the shorter way to write that, and 770 is better from a security standpoint.
Think for yourself, schmuck!
i5-2500K@4.3|Asus P8P67-LE|8GB DDR3-1600|Powercolor R7850 2G|1.5TB 7200.11|1988 Model M|Saitek X-45 & P880|Logitech MX 518|Dell 2209WA|Sennheiser PC151|Asus Xonar DX
bthylafh
Grand Gerbil Poohbah
 
Posts: 3219
Joined: Mon Dec 29, 2003 11:55 pm
Location: Southwest Missouri, USA

Re: Linux script, add two numbers

Postposted on Fri Mar 20, 2009 6:35 pm

Are you running it as "./plus.sh" or just "plus.sh"? Usually you don't have . in your path to avoid getting haxored.
notfred
Grand Gerbil Poohbah
 
Posts: 3760
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada


Return to Linux, Unix, and Assorted Madness

Who is online

Users browsing this forum: No registered users and 1 guest