why read line skips some lines...


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
why read line skips some lines...

Dear Guru:

This has got to be a difficult question, as I've worked on it for a good amount of time now & still puzzled...

So here is the simplified logic in my code:

while read LINE
do
#install a pkg with the name extracted from this line
install_pkg name
done < fileWithPkgNamesToBeInstalled

Say, I have 30 of such lines in the file, everything is fine except once it's finished a particular pkg install (which completed successfully with no error), it will skip the next 3 lines in the file, in other words, that 3 pkgs will not get installed.

If I commented out the install function then I can see all the lines including the ones previously being skipped (e.g. "echo $LINE" will not print the 3 lines in the above code):

while read LINE
do
#install a pkg with the name extracted from this line
# install_pkg name
echo $LINE
done < file

I moved that particular line in the file associated with the pkg to any where else, then still with 3 lines skipped, but they moved to a different place just to follow immediately that line.

Can anyone explain why??
Your response would be highly appreciated.
# 2  
Your looping a list without any controls. My guess is while the first package is being installed the system is busy. When the first package is done installing your script is already on line 4 or 5.

You have two ways to solve this problem. Either write a script that starts the install and has the brains to know when the first install stopped and start the next. (Best way)

or

You can find out how long the largest package takes to install and add 10% wait time to that install time between each of your calls and use the sleep command. (Not the best way)

I wouldn't thread something like this because you might need package 4 installed before package 18.
# 3  
Quote:
Originally Posted by bluemoon1
while read LINE
do
#install a pkg with the name extracted from this line
install_pkg name
done < fileWithPkgNamesToBeInstalled
Perhaps you could invoke install_pkg name as a background process and then 'wait' upon that to complete.
# 4  
Hi.

I'd guess that install_pkg is reading 3 times from STDIN, which is inherited from the loop, and thus from the file fileWithPkgNamesToBeInstalled.

Is there an option for install_pkg, say -y, that provides automatic answers, or an option that tells it to take input from a file different from STDIN? ... cheers, drl
# 5  
Guru vino & tomas:-)

What you say seems to make sense to me- that specific pkg takes the longest of all. I made a workaround to make it the last one to be installed- however, I know I have to put into some safety measure in between of any 2 pkgs installations based on what you said.

One thing I have to mention, I do have controls in the loop I omitted earlier just to simplify the example. I validate entries in the line & check returning from the installpkg commond- pkgadd - etc, but there was no error whatsoever.

invoking them in the backgroud may work, but I need to display & save the screen shots of the whole process to a log, as well, some pkgs have interactive prompts.

Perhaps the command pkgadd is asynchronous?
# 6  
Thanks for the replies, but I had simplified the script a lot! Still simplified, but here's a little more detail of what I'm doing ;-)
(3 pkgs would be missing after the red line)

grep -i '^[^#]*.\.pkg' $pkglist | \
while read LINE ; do
….getting pkgfile & pkgname from the LINE….
pkgadd -a $pkgadmin -d ../packages/$pkgfile $pkgname
if [ $? -ne 0 ]; then
echo "ERROR:……"
fi
done

pkglist includes:
…………..
# Tomcat
PSOumTomcat.pkg PSOumTomcat
# AWT
PSOumAWT.pkg PSOumAWT
# Backup restore
PSOumBRServer.pkg PSOumBRServer
PSOumBRClient.pkg PSOumBRClient
......
# 7  
I agree will drl. The solution is to redirect stdin prior to running the program.

pkgadd -a $pkgadmin -d ../packages/$pkgfile $pkgname < /dev/null

Reading from /dev/null may not be great when it happens, but subsequent iterations of the loop should be ok.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #74
Difficulty: Easy
NeXTStep was based on the original BSD operating system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read the file line by line and do something with lines

I have a file file_name_O.txt The file can have different number of other files names or nothing I will check cnt=`wc -l file_name_0.txt` if ;then exit 1 fi Now I have to start checking file names, i.e. read txt file line by line. If amount of ,lines equal 1, I can... (4 Replies)
Discussion started by: digioleg54
4 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

Read line with particular number of lines

Hi all, I have a file sample.txt abc asd adf daf adw add adv wdf I want to control the number of lines to read Like if i give input as ./script_name 2 5 required output asd adf daf (2 Replies)
Discussion started by: krux_rap
2 Replies

4. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (1 Reply)
Discussion started by: erlanq
1 Replies

5. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (2 Replies)
Discussion started by: erlanq
2 Replies

6. UNIX for Dummies Questions & Answers

Foreach loop that skips the header line of a file (csh)

Hello all, I'm working on a foreach loop to compare a couple sets of data. However, each datafile includes a header row. I'm wondering if it is possible to tell the foreach loop to skip the first line of data. I've been using the basic code as follows: foreach line ("`file.csv`") set... (2 Replies)
Discussion started by: meteorologistks
2 Replies

7. Shell Programming and Scripting

Remote script skips "read" command

This script is supposed to display a file ( crontab ), ask the user if they wish to update the file, then it goes through an update routine. #!/bin/bash FILE=/etc/crontab tail -5 $FILE echo -n "Does crontab need updating" read HOURS ...routines ....etc... Runs locally... (8 Replies)
Discussion started by: Bubnoff
8 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

10. UNIX for Dummies Questions & Answers

Read lines till a blank line is encountered

Hi, I have reached at a specified offset from the start of file. My requirement is that I want to read only those lines, which have the string READ / ALTER / UPDATE. As soon as, none of these literals are found in the subsequent line, I want to stop reading. Is there any feature of grep which... (1 Reply)
Discussion started by: saurabhsinha23
1 Replies

Featured Tech Videos