Personal computing discussed

Moderators: renee, SecretSquirrel, notfred

 
dmitriylm
Graphmaster Gerbil
Topic Author
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: SFBay
Contact:

Linux script, add two numbers

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.
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Linux script, add two numbers

Fri Mar 20, 2009 1:27 pm

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


> ./plus.sh 3 4
3 + 4 = 7
>
 
dmitriylm
Graphmaster Gerbil
Topic Author
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: SFBay
Contact:

Re: Linux script, add two numbers

Fri Mar 20, 2009 2:26 pm

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


> ./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.
 
radix
Gerbil First Class
Posts: 103
Joined: Sun Jan 13, 2002 7:00 pm
Location: Mountain View, CA
Contact:

Re: Linux script, add two numbers

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.
 
dmitriylm
Graphmaster Gerbil
Topic Author
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: SFBay
Contact:

Re: Linux script, add two numbers

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.
 
bthylafh
Maximum Gerbil
Posts: 4320
Joined: Mon Dec 29, 2003 11:55 pm
Location: Southwest Missouri, USA

Re: Linux script, add two numbers

Fri Mar 20, 2009 4:14 pm

At the prompt type "chmod 700 plus.sh", then "./plus.sh".
Hakkaa päälle!
i7-8700K|Asus Z-370 Pro|32GB DDR4|Asus Radeon RX-580|Samsung 960 EVO 1TB|1988 Model M||Logitech MX 518 & F310|Samsung C24FG70|Dell 2209WA|ATH-M50x
 
dmitriylm
Graphmaster Gerbil
Topic Author
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: SFBay
Contact:

Re: Linux script, add two numbers

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.
 
bthylafh
Maximum Gerbil
Posts: 4320
Joined: Mon Dec 29, 2003 11:55 pm
Location: Southwest Missouri, USA

Re: Linux script, add two numbers

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.
Hakkaa päälle!
i7-8700K|Asus Z-370 Pro|32GB DDR4|Asus Radeon RX-580|Samsung 960 EVO 1TB|1988 Model M||Logitech MX 518 & F310|Samsung C24FG70|Dell 2209WA|ATH-M50x
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Linux script, add two numbers

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.

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On