Click to See Complete Forum and Search --> : Bash Script using Foreach
singlespeed
08-15-2003, 08:26 AM
Q1) is foreach a valid command in bash scripting?
Q2) assuming it is what is the syntax to get a foreach loop to read the contents of a file as the input?
for example sudo code...
foreach name (read from file "names.txt")
echo $name
end
the syntax is
for foo in foobar ; do
<block of code here>
done
ok. after some messing around.
here is what you need.
use the while command.
#!/bin/bash
while read newline; do
echo $newline
done < /etc/fstab
substitute a variable or another file for the < /etc/fstab
rid3r
08-15-2003, 08:51 AM
yep it's
for name in `cat names.txt | <grep-sed-awk-whatever>` ; do echo $name ; done
singlespeed
08-15-2003, 03:48 PM
while our internet was down due to fallout from the NE Power Outage, this is what I came up with.....
#! /bin/sh
tables=`/bin/grep -v Tables tables.txt`
for table in $tables
{
/usr/bin/mysql telaurus -ufaa --password=******* -e "Show columns from $table" > /root/$table.txt
mail foo@foo.org -s "$table" < /root/$table.txt
}
end
Thanks for all the suggestions though, I'll take a look at those approaches for my own education later!
i bet there are a dozen more ways to do it. :)