You are not logged in.

#1 2009-04-21 19:54:19

andrew123456
Member
Registered: 2008-08-31
Posts: 32

bash scripting: until eof

hey guys, i was hoping for some help. i need to make a script that will constantly add read numbers together until ctrl-d (end of file) is sent. i've got the basic adding part done, but i can't get the until eof part to work, as i have no idea what the eof character is.

here's what i've got:

#!/bin/bash

a=0
until >> eof #this line doesn't work :(
do 
read b
a=$(($b+$a))
echo "new number is $a"
done

it's the until >> eof line that i baisically made up because i don't know what it is i need there.
any suggestions?

Offline

#2 2009-04-21 20:05:02

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: bash scripting: until eof

Like this?

a=0
while read b; do a=$(($b+$a)); echo $a is new; done

Offline

#3 2009-04-21 20:09:09

kumyco
Member
From: somewhere
Registered: 2008-06-23
Posts: 153
Website

Re: bash scripting: until eof

#!/bin/bash

a=0
while read b #this line doesn't work :(
do 
a=$(($b+$a))
echo "new number is $a"
done

you can also replace #!/bin/bash by #!/bin/sh nothing bash specific about that script as it stands

Offline

#4 2009-04-21 20:14:55

andrew123456
Member
Registered: 2008-08-31
Posts: 32

Re: bash scripting: until eof

wow thanks guys, i knew it was an obvious thing that i was missing, thanks for the help.

sidenote; what's the difference between /bin/bash and /bin/sh, is there a disadvantage to putting it over /bin/sh?

Offline

#5 2009-04-21 20:18:38

kumyco
Member
From: somewhere
Registered: 2008-06-23
Posts: 153
Website

Re: bash scripting: until eof

allows it to be run other shells /bin/sh might point to e.g dash or ksh for someone else

Offline

#6 2009-04-21 20:28:47

andrew123456
Member
Registered: 2008-08-31
Posts: 32

Re: bash scripting: until eof

greeat, thanks guys! i appreciate it!

Offline

#7 2009-04-21 21:30:24

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: bash scripting: until eof

[stijn@hermes ~]$ ll /bin/sh
lrwxrwxrwx 1 root root 4 jan 28 20:35 /bin/sh -> bash

Sh = symlink to the default shell.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#8 2009-04-22 22:52:24

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: bash scripting: until eof

B wrote:

Sh = symlink to the default shell.

I hope not.  It should only ever link to shells which feature a superset of /bin/sh functionality (ie, not csh and derivatives).

Offline

#9 2009-04-22 23:21:32

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: bash scripting: until eof

Interestingly enough, I had to fix some broken shit at work today because of this.  Some old scripts were using #!/bin/sh, but using bashisms.  The scripts were moved over to a ubuntu server which has /bin/sh -> /bin/dash and hence were no longer working.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#10 2009-05-12 23:42:42

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: bash scripting: until eof

rson451 wrote:

Interestingly enough, I had to fix some broken shit at work today because of this.  Some old scripts were using #!/bin/sh, but using bashisms.  The scripts were moved over to a ubuntu server which has /bin/sh -> /bin/dash and hence were no longer working.

You probably know the quote that it's easier to port a shell than a script, so I'm wandering, how did you fix the situation - installed bash & symlinked?

Offline

#11 2009-05-12 23:51:28

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: bash scripting: until eof

Bash was installed, and it wasn't many scripts so I just changed the shebangs.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

Board footer

Powered by FluxBB