Reading from a file

Reading from a file

am 22.04.2008 15:09:26 von invincible

I am trying to read from a file for the list of servers and then
instruct the script to login on these servers and run a command to
check one text file.

So far its done the below way...

cat server-list | \
while read line
do
echo "------------------------------------------------" >>test.out
echo " HOST : $line" >>test.out
echo "________________________________________________" >>test.out
rsh $line cat /home/basks.tbl|awk '{ print $1 }' >>test.out
done

The problem seems to be that script does not work !!! :-)

Any ideas around using cat wiht the while loop in a better way ?

Re: Reading from a file

am 22.04.2008 15:33:51 von Florian Kaufmann

> rsh $line cat /home/basks.tbl|awk '{ print $1 }' >>test.out

Maybee you have to write

rsh "$line" cat ...

because $line might contain blanks.

Re: Reading from a file

am 22.04.2008 15:49:06 von Ed Morton

On 4/22/2008 8:09 AM, invincible wrote:
> I am trying to read from a file for the list of servers and then
> instruct the script to login on these servers and run a command to
> check one text file.
>
> So far its done the below way...
>
> cat server-list | \
> while read line
> do
> echo "------------------------------------------------" >>test.out
> echo " HOST : $line" >>test.out
> echo "________________________________________________" >>test.out
> rsh $line cat /home/basks.tbl|awk '{ print $1 }' >>test.out
> done
>
> The problem seems to be that script does not work !!! :-)
>
> Any ideas around using cat wiht the while loop in a better way ?

Yes, don't use it. Instead of this:

cat file |
while read line
do
whatever
done

do this:

while IFS= read -r line
do
whatever
done < file

and instead of this:

cat file | awk '...'

do this:

awk '...' file

So, do that and then post a followup if your script still doesn't work.

Ed.

Re: Reading from a file

am 22.04.2008 15:50:53 von Bill Marcum

On 2008-04-22, invincible wrote:
>
>
> I am trying to read from a file for the list of servers and then
> instruct the script to login on these servers and run a command to
> check one text file.
>
> So far its done the below way...
>
> cat server-list | \
> while read line
> do
> echo "------------------------------------------------" >>test.out
> echo " HOST : $line" >>test.out
> echo "________________________________________________" >>test.out
> rsh $line cat /home/basks.tbl|awk '{ print $1 }' >>test.out
> done
>
> The problem seems to be that script does not work !!! :-)

How does it not work? What errors do you get? Are you sure rsh on your
system is "remote shell" and not "restricted shell"?
Another possible problem is that rsh is in a loop with a pipe as
standard input. You might need to redirect the rsh command's input to
/dev/null.

>
> Any ideas around using cat wiht the while loop in a better way ?

Re: Reading from a file

am 22.04.2008 16:54:13 von invincible

On Apr 22, 2:49 pm, Ed Morton wrote:
> On 4/22/2008 8:09 AM, invincible wrote:
>
>
>
> > I am trying to read from a file for the list of servers and then
> > instruct the script to login on these servers and run a command to
> > check one text file.
>
> > So far its done the below way...
>
> > cat server-list | \
> > while read line
> > do
> > echo "------------------------------------------------" >>test.out
> > echo " HOST : $line" >>test.out
> > echo "________________________________________________" >>test.out
> > rsh $line cat /home/basks.tbl|awk '{ print $1 }' >>test.out
> > done
>
> > The problem seems to be that script does not work !!! :-)
>
> > Any ideas around using cat wiht the while loop in a better way ?
>
> Yes, don't use it. Instead of this:
>
> cat file |
> while read line
> do
> whatever
> done
>
> do this:
>
> while IFS= read -r line
> do
> whatever
> done < file
>
> and instead of this:
>
> cat file | awk '...'
>
> do this:
>
> awk '...' file
>
> So, do that and then post a followup if your script still doesn't work.
>
> Ed.

Ed,

It seems Im now stuck on below error

rsh dubba awk '{ print $1 }' /home/basks.tbl
syntax error The source line is 1.
The error context is
>>> { <<<
awk: The statement cannot be correctly parsed.
The source line is 1.

Thankyou

Re: Reading from a file

am 22.04.2008 17:21:21 von wayne

invincible wrote:
> On Apr 22, 2:49 pm, Ed Morton wrote:
>> On 4/22/2008 8:09 AM, invincible wrote:
>>
>>
>>
>>> I am trying to read from a file for the list of servers and then
>>> instruct the script to login on these servers and run a command to
>>> check one text file.
>>> So far its done the below way...
>>> cat server-list | \
>>> while read line
>>> do
>>> echo "------------------------------------------------" >>test.out
>>> echo " HOST : $line" >>test.out
>>> echo "________________________________________________" >>test.out
>>> rsh $line cat /home/basks.tbl|awk '{ print $1 }' >>test.out
>>> done
>>> The problem seems to be that script does not work !!! :-)
>>> Any ideas around using cat wiht the while loop in a better way ?
>> Yes, don't use it. Instead of this:
>>
>> cat file |
>> while read line
>> do
>> whatever
>> done
>>
>> do this:
>>
>> while IFS= read -r line
>> do
>> whatever
>> done < file
>>
>> and instead of this:
>>
>> cat file | awk '...'
>>
>> do this:
>>
>> awk '...' file
>>
>> So, do that and then post a followup if your script still doesn't work.
>>
>> Ed.
>
> Ed,
>
> It seems Im now stuck on below error
>
> rsh dubba awk '{ print $1 }' /home/basks.tbl
> syntax error The source line is 1.
> The error context is
> >>> { <<<
> awk: The statement cannot be correctly parsed.
> The source line is 1.
>
> Thankyou

Seems like the quote marks are removed too soon, by rsh. Maybe:

rsh $line cat /home/basks.tbl|awk \'{ print $1 }\' >>test.out

-Wayne

Re: Reading from a file

am 22.04.2008 17:27:26 von Ed Morton

On 4/22/2008 10:21 AM, Wayne wrote:
> invincible wrote:
>
>>On Apr 22, 2:49 pm, Ed Morton wrote:
>>
>>>On 4/22/2008 8:09 AM, invincible wrote:
>>>
>>>
>>>
>>>
>>>>I am trying to read from a file for the list of servers and then
>>>>instruct the script to login on these servers and run a command to
>>>>check one text file.
>>>>So far its done the below way...
>>>>cat server-list | \
>>>>while read line
>>>>do
>>>>echo "------------------------------------------------" >>test.out
>>>>echo " HOST : $line" >>test.out
>>>>echo "________________________________________________" >>test.out
>>>>rsh $line cat /home/basks.tbl|awk '{ print $1 }' >>test.out
>>>>done
>>>>The problem seems to be that script does not work !!! :-)
>>>>Any ideas around using cat wiht the while loop in a better way ?
>>>
>>>Yes, don't use it. Instead of this:
>>>
>>> cat file |
>>> while read line
>>> do
>>> whatever
>>> done
>>>
>>>do this:
>>>
>>> while IFS= read -r line
>>> do
>>> whatever
>>> done < file
>>>
>>>and instead of this:
>>>
>>> cat file | awk '...'
>>>
>>>do this:
>>>
>>> awk '...' file
>>>
>>>So, do that and then post a followup if your script still doesn't work.
>>>
>>> Ed.
>>
>>Ed,
>>
>>It seems Im now stuck on below error
>>
>>rsh dubba awk '{ print $1 }' /home/basks.tbl
>> syntax error The source line is 1.
>> The error context is
>> >>> { <<<
>> awk: The statement cannot be correctly parsed.
>> The source line is 1.
>>
>>Thankyou
>
>
> Seems like the quote marks are removed too soon, by rsh. Maybe:
>
> rsh $line cat /home/basks.tbl|awk \'{ print $1 }\' >>test.out
>
> -Wayne

ITYM:

rsh "$line" awk \'{ print \$1 }\' /home/basks.tbl >>test.out

Also, if you're on Solaris make sure you're using New awk (nawk), GNU awk
(gawk), or /usr/xpg4/bin/awk, not /usr/bin/awk (oawk - old, broken awk).

Regards,

Ed.

Re: Reading from a file

am 22.04.2008 17:29:25 von Bill Marcum

On 2008-04-22, invincible wrote:
>
>
> It seems Im now stuck on below error
>
> rsh dubba awk '{ print $1 }' /home/basks.tbl
> syntax error The source line is 1.
> The error context is
> >>> { <<<
> awk: The statement cannot be correctly parsed.
> The source line is 1.
>
> Thankyou

You might need to write
rsh dubba "awk '{print $1}' /home/basks.tbl"

Re: Reading from a file

am 22.04.2008 17:30:59 von Florian Kaufmann

> while IFS= read -r line
> do
> whatever
> done < file

Why do you set IFS? First I thought you do it so blanks are not
removed from the input when read is separating the input into words.
However, I tried this

$ od -a <<< "$IFS"
0000000 sp ht nl nl
0000004
$ while read -r x; do echo "$x" "-"; done <<< 'a b c
> d e f'
a b c -
d e f -

So it works also without setting IFS. Or am I missing something?

Flo

Re: Reading from a file

am 22.04.2008 17:45:12 von Dave B

On Tuesday 22 April 2008 17:30, Florian Kaufmann wrote:

>> while IFS= read -r line
>> do
>> whatever
>> done < file
>
> Why do you set IFS? First I thought you do it so blanks are not
> removed from the input when read is separating the input into words.
> However, I tried this
>
> $ od -a <<< "$IFS"
> 0000000 sp ht nl nl
> 0000004
> $ while read -r x; do echo "$x" "-"; done <<< 'a b c
>> d e f'
> a b c -
> d e f -
>
> So it works also without setting IFS. Or am I missing something?

Try with leading blanks. Compare

echo " a b c" | while read -r line; do
echo "$line"
done

vs.

echo " a b c" | while IFS= read -r line; do
echo "$line"
done

--
D.

Re: Reading from a file

am 22.04.2008 17:45:28 von invincible

On Apr 22, 4:27 pm, Ed Morton wrote:
> On 4/22/2008 10:21 AM, Wayne wrote:
>
>
>
> > invincible wrote:
>
> >>On Apr 22, 2:49 pm, Ed Morton wrote:
>
> >>>On 4/22/2008 8:09 AM, invincible wrote:
>
> >>>>I am trying to read from a file for the list of servers and then
> >>>>instruct the script to login on these servers and run a command to
> >>>>check one text file.
> >>>>So far its done the below way...
> >>>>cat server-list | \
> >>>>while read line
> >>>>do
> >>>>echo "------------------------------------------------" >>test.out
> >>>>echo " HOST : $line" >>test.out
> >>>>echo "________________________________________________" >>test.out
> >>>>rsh $line cat /home/basks.tbl|awk '{ print $1 }' >>test.out
> >>>>done
> >>>>The problem seems to be that script does not work !!! :-)
> >>>>Any ideas around using cat wiht the while loop in a better way ?
>
> >>>Yes, don't use it. Instead of this:
>
> >>> cat file |
> >>> while read line
> >>> do
> >>> whatever
> >>> done
>
> >>>do this:
>
> >>> while IFS= read -r line
> >>> do
> >>> whatever
> >>> done < file
>
> >>>and instead of this:
>
> >>> cat file | awk '...'
>
> >>>do this:
>
> >>> awk '...' file
>
> >>>So, do that and then post a followup if your script still doesn't work.
>
> >>> Ed.
>
> >>Ed,
>
> >>It seems Im now stuck on below error
>
> >>rsh dubba awk '{ print $1 }' /home/basks.tbl
> >> syntax error The source line is 1.
> >> The error context is
> >> >>> { <<<
> >> awk: The statement cannot be correctly parsed.
> >> The source line is 1.
>
> >>Thankyou
>
> > Seems like the quote marks are removed too soon, by rsh. Maybe:
>
> > rsh $line cat /home/basks.tbl|awk \'{ print $1 }\' >>test.out
>
> > -Wayne
>
> ITYM:
>
> rsh "$line" awk \'{ print \$1 }\' /home/basks.tbl >>test.out
>
> Also, if you're on Solaris make sure you're using New awk (nawk), GNU awk
> (gawk), or /usr/xpg4/bin/awk, not /usr/bin/awk (oawk - old, broken awk).
>
> Regards,
>
> Ed.

Ed,

The problem is resolved . I am able to get the output for the first
server, but it seems it does not go beyond this. Any problems with the
loop we are using ?

Cheers.

Re: Reading from a file

am 22.04.2008 18:01:53 von Ed Morton

On 4/22/2008 10:45 AM, invincible wrote:
> On Apr 22, 4:27 pm, Ed Morton wrote:
>
>>On 4/22/2008 10:21 AM, Wayne wrote:
>>
>>
>>
>>
>>>invincible wrote:
>>
>>>>On Apr 22, 2:49 pm, Ed Morton wrote:
>>>
>>>>>On 4/22/2008 8:09 AM, invincible wrote:
>>>>
>>>>>>I am trying to read from a file for the list of servers and then
>>>>>>instruct the script to login on these servers and run a command to
>>>>>>check one text file.
>>>>>>So far its done the below way...
>>>>>>cat server-list | \
>>>>>>while read line
>>>>>>do
>>>>>>echo "------------------------------------------------" >>test.out
>>>>>>echo " HOST : $line" >>test.out
>>>>>>echo "________________________________________________" >>test.out
>>>>>>rsh $line cat /home/basks.tbl|awk '{ print $1 }' >>test.out
>>>>>>done
>>>>>>The problem seems to be that script does not work !!! :-)
>>>>>>Any ideas around using cat wiht the while loop in a better way ?
>>>>>
>>>>>Yes, don't use it. Instead of this:
>>>>
>>>>> cat file |
>>>>> while read line
>>>>> do
>>>>> whatever
>>>>> done
>>>>
>>>>>do this:
>>>>
>>>>> while IFS= read -r line
>>>>> do
>>>>> whatever
>>>>> done < file
>>>>
>>>>>and instead of this:
>>>>
>>>>> cat file | awk '...'
>>>>
>>>>>do this:
>>>>
>>>>> awk '...' file
>>>>
>>>>>So, do that and then post a followup if your script still doesn't work.
>>>>
>>>>> Ed.
>>>>
>>>>Ed,
>>>
>>>>It seems Im now stuck on below error
>>>
>>>>rsh dubba awk '{ print $1 }' /home/basks.tbl
>>>>syntax error The source line is 1.
>>>>The error context is
>>>> >>> { <<<
>>>>awk: The statement cannot be correctly parsed.
>>>>The source line is 1.
>>>
>>>>Thankyou
>>>
>>>Seems like the quote marks are removed too soon, by rsh. Maybe:
>>
>>> rsh $line cat /home/basks.tbl|awk \'{ print $1 }\' >>test.out
>>
>>>-Wayne
>>
>>ITYM:
>>
>> rsh "$line" awk \'{ print \$1 }\' /home/basks.tbl >>test.out
>>
>>Also, if you're on Solaris make sure you're using New awk (nawk), GNU awk
>>(gawk), or /usr/xpg4/bin/awk, not /usr/bin/awk (oawk - old, broken awk).
>>
>>Regards,
>>
>> Ed.
>
>
> Ed,
>
> The problem is resolved . I am able to get the output for the first
> server, but it seems it does not go beyond this. Any problems with the
> loop we are using ?
>
> Cheers.

Post the code you're now using.

Ed.

Re: Reading from a file

am 22.04.2008 18:23:19 von Bill Marcum

On 2008-04-22, invincible wrote:
>
>>
>> rsh "$line" awk \'{ print \$1 }\' /home/basks.tbl >>test.out
>>
>> Also, if you're on Solaris make sure you're using New awk (nawk), GNU awk
>> (gawk), or /usr/xpg4/bin/awk, not /usr/bin/awk (oawk - old, broken awk).
>>
>> Regards,
>>
>> Ed.
>
> Ed,
>
> The problem is resolved . I am able to get the output for the first
> server, but it seems it does not go beyond this. Any problems with the
> loop we are using ?
>
> Cheers.

rsh might be consuming standard input. Try
rsh "$line" awk \'{ print \$1 }\' /home/basks.tbl >test.out

Re: Reading from a file

am 23.04.2008 00:44:04 von Maxwell Lol

invincible writes:

> The problem is resolved . I am able to get the output for the first
> server, but it seems it does not go beyond this. Any problems with the
> loop we are using ?

Be aware that rsh is unsecure. Use ssh if you can.
On my machine, rsh -> ssh.

Also - Bill says to use Another way to do this, from the ssh man page

-n Redirects stdin from /dev/null (actually, prevents reading from
stdin). This must be used when ssh is run in the background. A
common trick is to use this to run X11 programs on a remote
machine. For example, ssh -n shadows.cs.hut.fi emacs & will
start an emacs on shadows.cs.hut.fi, and the X11 connection will
be automatically forwarded over an encrypted channel. The ssh
program will be put in the background. (This does not work if
ssh needs to ask for a password or passphrase; see also the -f
option.)