1829585 Members
1888 Online
109992 Solutions
New Discussion

Posix Scripting question

 
SOLVED
Go to solution
Smucker
Regular Advisor

Posix Scripting question

I would like to read a file sequentially ans ask the user for input on selected records. the flow will be something like this

while loop
read file
ask question
process if yes
done

I tried the following but it failed to work

while read
ask question
process if yes
done
the ask question(read) was answered by the shell. Any suggestions.
7 REPLIES 7
RAC_1
Honored Contributor
Solution

Re: Posix Scripting question

cat file | while read line
do
echo "Ask something to user"
read answer
if [[ ${answer} = "user_response" ]]
then
"whatever you want"
else
echo "something bad"
exit 0

Anil
There is no substitute to HARDWORK
Smucker
Regular Advisor

Re: Posix Scripting question

sorry did not work. Same results the input redirection is answering the read command.

any other suggestions? (below is the code snippet
*******************************
cat deletelist.txt|while read -r userid passwd comment
do
echo "Do you wish to process $userid on $1 - password is $passwd and comments are
$comment? \c"
read response
case $response in
[y,Y,yes,YES])
echo " $userid was processed "
;;
[n,N,no,NO])
echo " $userid was not processed "
;;
*)
echo " Assuming no was the intended response "
echo " You typed $response "
echo " $userid was not processed"
;;
esac
done
**********************
Smucker
Regular Advisor

Re: Posix Scripting question

I got it to work by loading the file into seperate array variables then doing a seperate loop to process the array variables

xx=1
cat filename|while read -r var1 var2
do
arr1[xx]=var1
arr2[xx]=var2
let "xx = xx +1"
done

xx=1
cont=yes
while [ cont = yes ]
do
if ! [ ${var1[xx]} ]
then
cont=no
break
fi
askquestion
read
case
.....
esac
done
Chris Vail
Honored Contributor

Re: Posix Scripting question

another way to do this is with the while true loop:

while true
do
echo "Ask the question, or x to exit. \c"
read answer
case $answer in
0) do something;;
1) do something;;
2) do something
x) exit0
esac
done



Chris
Rodney Hills
Honored Contributor

Re: Posix Scripting question

Since you have STDIN defined to the filename, then all "read" statements will be from that file.

To have read, read from the terminal, try-
read answer <&2

Assuming STDERR is still assigned to your terminal.

HTH

-- Rod Hills
There be dragons...
Robert Dill
Advisor

Re: Posix Scripting question

You can have multiple inputs by using the "exec" command to assign a "file descriptor" number 3 to 9 (0=stdin, 1=stdout, 2=stderr) to a file and then use that number in a read command "-u" (or any other i/o command) - you can explicitly open and close these file descriptors and re-use them if you need to - please see "man sh-posix".

example:
exec 3< $SOME_INPUT_FILE
while read -u3 a1 a2 a3 garbage
do
print "$a3"
done

Good luck.
Bob
Smucker
Regular Advisor

Re: Posix Scripting question

Perfect. Worked like a dream thanks