How to Read the entire file using while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Read the entire file using while loop
# 8  
Old 05-22-2009
ssh is reading the standard input channel from your while loop and exhausting the input. By the time it has finished, there is no input left and your loop exits.

To avoid this problem, you need to present ssh with the normal stdin file descriptor while keeping it separate from the input to your while loop. You can do this by a couple of I/O redirections and a subshell. The following example works for me, I duplicate fd 0 to fd 3 before entering the loop and present that fd to ssh. Changes in bold red:

Code:
FILENAME=/tmp/Parser_list_beauty
exec 3>&0
while read PARSER_NAME HOST_NAME
do
    echo "-------------------------------------------------------"
    echo "Extracting information for $PARSER_NAME on $HOST_NAME"
    echo " "
    (
        exec 0>&3
        ssh $HOST_NAME grep STANDBY /ilx/gtp/pkg/$PARSER_NAME/log/$PARSER_NAME.log | tail -2
    )
    echo "-------------------------------------------------------"
done < $FILENAME >> /tmp/parser_extract.txt


Last edited by cambridge; 05-22-2009 at 08:09 AM..
# 9  
Old 05-22-2009
awk is also redundant, something like:

Code:
FILENAME=/tmp/Parser_list_beauty
while read PARSER_NAME HOST_NAME
do
  echo "-------------------------------------------------------" >> /tmp/parser_extract.txt
  echo "Extracting information for $PARSER_NAME on $HOST_NAME" >> /tmp/parser_extract.txt
  echo " " >> /tmp/parser_extract.txt
  ssh $HOST_NAME grep STANDBY /ilx/gtp/pkg/$PARSER_NAME/log/$PARSER_NAME.log  >> /tmp/parser_extract.txt
  echo "-------------------------------------------------------" >> /tmp/parser_extract.txt
done < $FILENAME

# 10  
Old 05-22-2009
Quote:
Originally Posted by sdosanjh
> awk '{
>> PARSER_NAME=$1
>> HOST_NAME=$2
>> ssh -X $HOST_NAME grep STANDBY /ilx/gtp/pkg/$PARSER_NAME/log/$PARSER_NAME.log >> /tmp/parser_extract.txt
>> echo "--------------------------------------------------------------------"
>> echo " "
>> }' $FILENAME
Everyone else but you is thinking "shell". Your code above is clearly awk. Awk doesn't understand such syntax.
# 11  
Old 05-22-2009
Hi Cambridge,

Thanks, it worked for me as well... Smilie
# 12  
Old 05-22-2009
Quote:
Originally Posted by cambridge
ssh is reading the standard input channel from your while loop and exhausting the input. By the time it has finished, there is no input left and your loop exits.

To avoid this problem, you need to present ssh with the normal stdin file descriptor while keeping it separate from the input to your while loop. You can do this by a couple of I/O redirections and a subshell.
Quote:
-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.)
ssh -n $HOST_NAME grep ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

User with read-only to entire filesystem

Is this even possible? Its a request for external auditors to login remotely. (5 Replies)
Discussion started by: psychocandy
5 Replies

2. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

3. Shell Programming and Scripting

Bash script read specific value from files of an entire folder

Hello, I heva a problem creating a script that read specifc value from all the files of an entire folder I have a number of email files into a directory and i need to extrect from each file 2 specific values. After that i have to put them into a new file that looks like that: To: value1 ... (1 Reply)
Discussion started by: ahmenty
1 Replies

4. Shell Programming and Scripting

Using awk instead of while loop to read file

Hello, I have a huge file, I am currently using while loop to read and do some calculation on it, but it is taking a lot of time. I want to use AWK to read and do those calculations. Please suggest. currently doing: cat input2 | while read var1 do num=`echo $var1 | awk... (6 Replies)
Discussion started by: anand2308
6 Replies

5. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

6. UNIX for Dummies Questions & Answers

How to read entire output from a file?

Hello- I am trying to view a file which is quite large. However, whenever I do 'cat (file name)' it shows me just the half.. I am using Putty to access my server. Also, is it possible to edit a file from a unix system on a 'Gedit for Windows" text editor? Thanks (7 Replies)
Discussion started by: DallasT
7 Replies

7. Shell Programming and Scripting

Problem reading file in while/read loop

I know I should be able to see a way of doing this easily, but my brain just won't engage. I have a script working on an embedded device that checks to see if an item is in a blacklist before performing some actions. At the moment the code reads thus.... while read BLACKLIST ; do ... (7 Replies)
Discussion started by: Bashingaway
7 Replies

8. UNIX for Dummies Questions & Answers

How to read a file in unix using do....done loop

Hi , can some give me idea about how to use do...done while loop in UNIX to read the contents of a file.. (2 Replies)
Discussion started by: sreenusola
2 Replies

9. UNIX for Advanced & Expert Users

Read the entire output fired by ps command

Actually I want to display the entire output fired by ps command. My output gets trucated after 80 chars. Thus, i am not able to see the entire command running when i give a ps -eaf .... Does anyone know how do i display the entire output fired by ps command .. (i.e the command along with... (5 Replies)
Discussion started by: vinithepoo
5 Replies

10. Shell Programming and Scripting

Read from a file and use the strings in a loop

Hello all, I need some help to create a script which contain a few strings on every line, and use those strings in a loop to fire some commands. for exmaple the file looks like tom dave bill andy paul I want to read one line at a time and use it in loop like command tom command dave... (3 Replies)
Discussion started by: xboxer21
3 Replies
Login or Register to Ask a Question