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 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 |
| |||
| 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 |
| |||
| 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 |
| |||
| -----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----- |
| |||
| 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 |
| |||
| 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" |
| ||||
| 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 |