[OT SED AWK BASH] How to run a shell command for every line in a file?
Howdy, In order to run a command form bash fr every file in a directory, let's use tar xzvf for instance, I'd use something like this: for tarball in ./*.tar.gz; do tar xzvf $tarball ; done And it would open every tar.gz archive in the current directory. Now my question is, is it possible to do something like that "for" every line in a file? In my case, I now have a list of hostnames and I want to run nslookup on each of them with a shell script (so I won't have to do it by hand). for host in hostnames.lst ; do nslookup $host; done But obviously I'm going to have to do something more then that in order to feed the hostnames to the shell one at a time. Possible? ---------------------------------------------------- Jonathan Wilson System Administrator Cedar Creek Software http://www.cedarcreeksoftware.com Central Texas IT http://www.centraltexasit.com
* Jonathan Wilson
Howdy,
In order to run a command form bash fr every file in a directory, let's use tar xzvf for instance, I'd use something like this:
for tarball in ./*.tar.gz; do tar xzvf $tarball ; done
And it would open every tar.gz archive in the current directory.
Now my question is, is it possible to do something like that "for" every line in a file?
In my case, I now have a list of hostnames and I want to run nslookup on each of them with a shell script (so I won't have to do it by hand).
for host in hostnames.lst ; do nslookup $host; done
for host in `cat hostnames.lst` ; do nslookup $host; done -- Mads Martin Joergensen, http://mmj.dk "Why make things difficult, when it is possible to make them cryptic and totally illogic, with just a little bit more effort." -- A. P. J.
If I understand you right, you need to do something like this: #!/bin/bash HOSTS=`cat hosts.txt` for host in $HOSTS; do nslookup $host >> resolved.txt done; exit 0; Note: I have not actually tested the `cat hosts.txt` piece but it should work, otherwise, somehow get your hosts into a variable. If you know perl, it would be much easier in perl.
Howdy,
In order to run a command form bash fr every file in a directory, let's use tar xzvf for instance, I'd use something like this:
for tarball in ./*.tar.gz; do tar xzvf $tarball ; done
And it would open every tar.gz archive in the current directory.
Now my question is, is it possible to do something like that "for" every line in a file?
In my case, I now have a list of hostnames and I want to run nslookup on each of them with a shell script (so I won't have to do it by hand).
for host in hostnames.lst ; do nslookup $host; done
But obviously I'm going to have to do something more then that in order to feed the hostnames to the shell one at a time.
Possible?
---------------------------------------------------- Jonathan Wilson System Administrator
Cedar Creek Software http://www.cedarcreeksoftware.com Central Texas IT http://www.centraltexasit.com
-- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/support/faq and the archives at http://lists.suse.com
On Wed, 2 May 2001, Jonathan Wilson wrote:
for host in hostnames.lst ; do nslookup $host; done Try: for host in `cat hostnames.lst` ; do nslookup $host; done
The ` ` enables a command to be run and the output to be used in place of what is between them =o) Hope this helps! -- -=[Tigersden Internet Services]=- ************COMING SOON************* -=[For All Your Internet Needs]=- * HTTP://www.tigersden.demon.co.uk * -=[P.G.P. Public Key Available]=- ************COMING SOON************* -=[ !!Powered by SuSE Linux!! ]=- My eyes are 20-20, but my brain isn't too good at interpreting what they see =o)
participants (4)
-
Just Another SuSE User
-
Mads Martin Jørgensen
-
The Purple Tiger
-
wilson@claborn.net