Unix Technical Forum

Bash script input from file - an answer

This is a discussion on Bash script input from file - an answer within the Slackware Linux Support forums, part of the Unix Operating Systems category; --> I'm pretty sure this is the NG where I was bitching about the lousy bash docs (and all the ...


Go Back   Unix Technical Forum > Unix Operating Systems > Slackware Linux Support

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-19-2008, 04:28 PM
Rich Grise
 
Posts: n/a
Default Bash script input from file - an answer

I'm pretty sure this is the NG where I was bitching about the lousy bash
docs (and all the rest of the lousy docs ;-) ), anyway, I found out
if not exactly "how to input from a file in a bash script (but not
< stdin)", I saw this little goodie in UPGRADE.TXT on CD 1:

#!/bin/sh
cd /etc
find . -name "*.new" | while read configfile ; do
if [ ! "$configfile" = "./rc.d/rc.inet1.conf.new" \
-a ! "$configfile" = "./group.new" \
-a ! "$configfile" = "./passwd.new" \
-a ! "$configfile" = "./shadow.new" ]; then
cp -a $(echo $configfile | rev | cut -f 2- -d . | rev) \
$(echo $configfile | rev | cut -f 2- -d . | rev).bak 2> /dev/null
mv $configfile $(echo $configfile | rev | cut -f 2- -d . | rev)
fi
done

The output of find just pipes to the while loop, which reads it a
line at a time.

So, I could go:

cat myconfigfile | while read mydata ; do
$SOMETHING_WITH $mydata
done

I think I might find this handy. :-)

Cheers!
Rich

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-19-2008, 04:28 PM
Floyd L. Davidson
 
Posts: n/a
Default Re: Bash script input from file - an answer

Rich Grise <null@example.net> wrote:
>
>cat myconfigfile | while read mydata ; do
> $SOMETHING_WITH $mydata
>done
>
>I think I might find this handy. :-)


Common usage.

You might look at some variations too:

while read mydata < myconfigfile ; do
...
done

and

while read mydata ; do
...
done < myconfigfile

also

{
while read mydata ; do
...
done
} < myconfigfile

--
FloydL. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-19-2008, 04:28 PM
Rich Grise
 
Posts: n/a
Default Re: Bash script input from file - an answer

On Wed, 13 Oct 2004 17:29:41 -0800, Floyd L. Davidson wrote:

> Rich Grise <null@example.net> wrote:
>>
>>cat myconfigfile | while read mydata ; do
>> $SOMETHING_WITH $mydata
>>done
>>
>>I think I might find this handy. :-)

>
> Common usage.
>
> You might look at some variations too:
>
> while read mydata < myconfigfile ; do
> ...
> done


I knew there was a better way! Naturally, it's obvious, once you
see it. I guess it's true what they say - the fastest way to get
an answer on usenet is to post something that can be done better.
:-)

Cheers!
Rich

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-19-2008, 04:28 PM
Faux_Pseudo
 
Posts: n/a
Default Re: Bash script input from file - an answer

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

_.-In alt.os.linux.slackware, Rich Grise wrote the following -._
> cat myconfigfile | while read mydata ; do
> $SOMETHING_WITH $mydata
> done


UUOC (useless use of cat)

while read mydata ; do
$SOMETHING_WITH $mydata
done < myconfigfile


- --
.-')) http://asciipr0n.com/fp ('-. | It's a damn poor mind that
' ..- .:" ) ( ":. -.. ' | can only think of one way to
((,,_;'.;' UIN=66618055 ';. ';_,,)) | spell a word.
((_.YIM=Faux_Pseudo :._)) | - Andrew Jackson
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBbepJSJec2PH9pbURAjWAAJ94uvHYTka2dFzQ61UoR4 YXZBNj4QCfTisY
6gFGWrzU7bUhBl378o2KaJM=
=jiZ1
-----END PGP SIGNATURE-----
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-19-2008, 04:29 PM
Floyd L. Davidson
 
Posts: n/a
Default Re: Bash script input from file - an answer

Rich Grise <rich@example.net> wrote:
>
>I knew there was a better way! Naturally, it's obvious, once you


Once upon a time if anyone posted something like that they
immediately were awared a prize in the Useless Use of Cat Award
program.

But yours wasn't too bad, really. Consider this:

cat - | while read myvar ; do
...
done

>see it. I guess it's true what they say - the fastest way to get
>an answer on usenet is to post something that can be done better.
>:-)


Kinda like walking into a redneck bar and announcing you'll meet
outside with anybody who thinks they're mean enough to try. It
might be true, and you'll know fer sure real soon.

I knew a guy who was 6'6" who used to do that, literally, for
fun. He was only half as stupid as that sounds though... he
said he figured anyone dumb enough to take him up on it deserved
to learn about his two big brothers (both 6'8") waiting outside
in case anyone might want to hurt 'Lil Bro'.

--
FloydL. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-19-2008, 04:29 PM
Stuart Winter
 
Posts: n/a
Default Re: Bash script input from file - an answer

On Thu, 14 Oct 2004 02:55:14 GMT, Faux_Pseudo@yahoo.comERCIAL wrote:

> while read mydata ; do
> $SOMETHING_WITH $mydata
> done < myconfigfile


I think it's worth noting that you shouldn't always receive a
useless use of cat award.

Does nobody know that cat does not always produce the same results
as reading the file directly?

--
Stuart Winter
www.interlude.org.uk & www.armedslack.org
| "Washing machines live longer with Calgon"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-19-2008, 04:29 PM
Floyd L. Davidson
 
Posts: n/a
Default Re: Bash script input from file - an answer

Stuart Winter <use.reply.to@interlude.org.uk> wrote:
>On Thu, 14 Oct 2004 02:55:14 GMT, Faux_Pseudo@yahoo.comERCIAL wrote:
>
>> while read mydata ; do
>> $SOMETHING_WITH $mydata
>> done < myconfigfile

>
>I think it's worth noting that you shouldn't always receive a
>useless use of cat award.
>
>Does nobody know that cat does not always produce the same results
>as reading the file directly?


How does it differ, assuming one doesn't make an effort by using
command line options? Or is that exactly what you do mean?

--
FloydL. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:19 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com