The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read user input from within a wile loop that is being fed from below broli UNIX for Dummies Questions & Answers 2 01-09-2008 03:19 PM
Apply Regex to input read bonekrusher UNIX for Dummies Questions & Answers 3 09-23-2007 02:45 PM
read input file for batch job kinmak Shell Programming and Scripting 14 07-11-2007 07:22 AM
While read loop and rsh 104234 UNIX for Advanced & Expert Users 1 01-15-2006 11:53 AM
input inside while read loop jhansrod Shell Programming and Scripting 3 08-13-2005 10:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-14-2007
dkieran dkieran is offline
Registered User
  
 

Join Date: May 2007
Posts: 21
Smile ls while read loop - internal read picking up wrong input

Hi,
It's my first time on here, so hello everybody!
Okay, the above message description probably makes no kind of sense so I'll just chuck the code down and explain the problem:

Here's the offending bit of code. Basically it is intended to go through all the files in a directory and ask the user if he wants to delete them or not.

########################################

ls | \
while read LINE
do
echo "Line is $LINE. Delete (y/n)?"
read ANSWER
echo "Answer is $ANSWER"
done

#########################################

I am trying this out on a directory that has 2 files in it, file1 and file2.
Here is the output:

Line is file1. Delete (y/n)?
Answer is file2

Then the loop exits.

So when "ANSWER" is read what is actually happening is that the second item of the WHILE loop (file2) is being read into ANSWER and then, because there is nothing left for the while loop to loop through at that point, the loop is exited. I want the loop to stop and wait for my input (y or n) and only then continue. Any ideas anyone? I'm guessing the answer is something simple, I am quite new to UNIX, so this no doubt a schoolboy error of some sort.
  #2 (permalink)  
Old 05-14-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
dkieran,
See if this works for you:
Code:
for iFile in `ls -1`
do
  echo "File = $iFile. Delete (y/n)?"
  read ANSWER
  echo "Answer is $ANSWER"
done
  #3 (permalink)  
Old 05-14-2007
dkieran dkieran is offline
Registered User
  
 

Join Date: May 2007
Posts: 21
Hey Shell Life,
Thanks very much for your response! It works!
I don't know why it works, though. If you have the time to explain it, I would really appreciate that. Thanks for the tip!
D
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0