|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 ![]() |
|
#3
|
|||
|
|||
|
That's not a general solution. Try this:
read confirm?"Process $1 - are you sure? (y/n) : " < /dev/tty |
|
#4
|
|||
|
|||
|
Thanks for that. I'll give it a try.
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Stdin help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|