UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old November 10th, 2004, 09:31 AM
dev0173 dev0173 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: UK
Posts: 18 dev0173 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Stdin help

Hi Guys,

Can anyone help me with this small script:

Code:
#!/bin/ksh
function process {
  read confirm?"Process $1 - are you sure? (y/n) : "
  print " You replied '$confirm'"
}

process "TEST"
grep "^[A-Z]" tst1file | while read ifn stuff
do
  print $ifn
  process $ifn
done


It's an extract from a larger script but it demonstrates the problem. Its function is to read the first word on each line of a file like this:

Code:
Row1 stuff
Row2 stuff
Row3 stuff
Row4 stuff


and do something (anything!) if the user confirms the operation.

The problem is that the READ just inside the WHILE and the READ inside the function are both taking input from the file. I was expecting the grep | while read bit to operate independently from the user confirmation code inside the process function.

These are the results I get:

Code:
Process TEST - are you sure? (y/n) : y
 You replied 'y'
Row1
 You replied 'Row2 stuff'
Row3
 You replied 'Row4 stuff'


You can see that the call to process() outside the loop works just fine, but inside the loop it takes input from the grep.

Anyone got any ideas on how to get this working the way I want??

Many thanks

Steve

Reply With Quote
  #2  
Old November 11th, 2004, 05:07 AM
dev0173 dev0173 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: UK
Posts: 18 dev0173 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Feels like I'm talking to myself but here's a solution if anyone is interested:

Code:
#!/bin/ksh
function process {
  read confirm?"Process $1 - are you sure? (y/n) : "
  print " You replied '$confirm'"
}

ifns=`awk '/^[A-Z]/ {print $1}' tst1file`
for ifn in $ifns; do
  process $ifn
done


I've replaced the grep with an awk and put the results into a string which is then parsed but the for loop. Simple really. I don't know why I didn't think of it before

Reply With Quote
  #3  
Old November 11th, 2004, 07:25 AM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
That's not a general solution. Try this:

read confirm?"Process $1 - are you sure? (y/n) : " < /dev/tty

Reply With Quote
  #4  
Old November 12th, 2004, 05:04 AM
dev0173 dev0173 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: UK
Posts: 18 dev0173 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for that. I'll give it a try.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Stdin help


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway