Read from user inside a while statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from user inside a while statement
# 1  
Old 02-10-2011
Read from user inside a while statement

Hi there
I want to ask to a user something about each line of a file. This is my code:
Code:
#cat test.txt
first line
second line
third line
#cat test.sh
#!/bin/ksh
read_write () {
echo "Do you want to print the line (YES/NO)?\n"

read TEST
case ${TEST} in
        YES) return 0;;
        NO) return 1;;
        *) read_write;;
esac
}

while read _line 
do
        read_write
done < ./test.txt

but when i run it:
Code:
#test.sh
Do you want to print the line (YES/NO)?

[...]

Do you want to print the line (YES/NO)?

./test.sh[7]: read_write: recursion too deep

How can I solve it?

Thanks in advance.

---------- Post updated at 04:46 PM ---------- Previous update was at 04:40 PM ----------

All right, I found the solution (I promise that I had search before I posted)

https://www.unix.com/shell-programming-scripting/75314-read-command-input-inside-while-loop.html

Sorry for the format, but the site doesn't allow me to publish urls

Last edited by pludi; 02-11-2011 at 06:02 AM..
# 2  
Old 02-10-2011
stdin has already been redirected from file so stdin is no longer the keyboard. You'll need to save a copy of stdin in order to use it elsewhere but how to do that depends on what variant of what shell you have. What is it? What's your system?
# 3  
Old 02-11-2011
Solaris 8
ksh
# 4  
Old 02-11-2011
Code:
read_write () 
{
echo -n "Do you want to print the line (Y/N)?"
<&5 read  TEST   # input from handler 5 = org. stdin
case "$TEST" in
        Y) return 0;;
        N) return 1;;
        *) read_write;;
esac
}

exec 4< ./test.txt  # handler 4 for file
exec 5<&1    # handler 5 is same as stdin handler 1
while read line   
do
        echo $line      # stdout
        read_write
done <&4

This User Gave Thanks to kshji For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to read user input inside a loop

Hi, This query is a part of a much more lengthy script. I wish to look for all the files in a folder named "data" which in this case has two files i.e. plan.war and agent.properties. For all the files found under data I wish to ask the user as to where they wish copy the files to. Below,... (14 Replies)
Discussion started by: mohtashims
14 Replies

2. Shell Programming and Scripting

Command in inside awk statement

Hello can you please help me with below script which is meant to delete clients from multiple netbackup policies I want to run a command insdie awk statement apparelnlty this script is not working for me for i in $( cat clients_list) do bppllist -byclient $i | awk... (6 Replies)
Discussion started by: Sara_84
6 Replies

3. Shell Programming and Scripting

Multiple conditions inside if statement

I was trying to write multiple conditions inside the if statement but its not working. export VAR_NM=abc.txt export CURR_DT=20131011 export PREV_DT=20131012 if && then echo "Yes" else echo "NO" fi It should return Yes but returning NO always.Appreciate any help. (3 Replies)
Discussion started by: dr46014
3 Replies

4. Homework & Coursework Questions

How to read user keyboard input inside the case?

I need to Write a shell script that allows some system-administration tasks to be preformed automatically from a menu-driven interface. with automated following tasks: Copy directory tree Delete files or directories Output Information (this part is done ) *Copy directory tree The “Copy... (2 Replies)
Discussion started by: femchi
2 Replies

5. Shell Programming and Scripting

issues with sql inside if statement

Hi, I have problem with the following code. My IF block is not executed. And I see "syntax error near unexpected token `)'" error for line "EOF" in the stats_function(). but when I comment the IF block I don't see this error. Kindly help me with this issue. clean_function() {... (10 Replies)
Discussion started by: babom
10 Replies

6. Shell Programming and Scripting

Multiple conditions inside my if statement

Hello, I am using shell scripting and I am recieving odd results from my if statement if I want it to enter the loop only if L1 is equal to zero and one of the other criteria are filled, however it is entering at other times as well. What can i do to fix this? i tried seperating it... (6 Replies)
Discussion started by: ryddner
6 Replies

7. Shell Programming and Scripting

Sysdate inside awk print statement

Hi, I am using awk statement to extract data from a file and write a new file with certain columns rearranged and few hard coded values added to new file. Now i need to add a column with sysdate. can i do that inside the awk print statement? Now: nawk ' /^3/ BEGIN {FS=","}... (2 Replies)
Discussion started by: selvankj
2 Replies

8. Shell Programming and Scripting

if inside while statement (PLZ HELP)

hey guys i need some help here i am trying to do a bash shell script to get a parent PID and then search for all childs and kill them all without killing the parent, my problem is in the loop it seems that i dont know how to make a if statment inside a while statment it does not give me what i... (5 Replies)
Discussion started by: q8devilish
5 Replies

9. Shell Programming and Scripting

Exit statement ignored inside KSH function

Hi All, I have multiple functions in my script and I'm trying to capture stdout from some of them, but I also do some error checking in them (in the functions that output something to their stdout that needs capturing) and I need to be able to end the entire script with an error message. ... (2 Replies)
Discussion started by: gkubok
2 Replies
Login or Register to Ask a Question