read from console in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read from console in ksh
# 1  
Old 12-28-2007
Question read from console in ksh

I am stuck with a problem while reading data from a file..

while [ 1 ]
do
read line
#do some operations and if some condition is satisfied, ask the user to enter his choice.
# using the choice continue operations.
done < fileBeingRead.txt

The problem is, when i am inside the while loop, I am not able to read data from the console/terminal. Is there any way to temporarily read data from console? and then continue looping ?
# 2  
Old 12-28-2007
I would go with this:
Code:
#! /usr/bin/ksh

exec 5<> /dev/tty

while read line ; do
        print -u5 I just read: $line
        print -u5 -n "continue? "
        read -u5 ans
        print ans = $ans
        if [[ $ans != y* ]] ; then
                print -u5 good-bye
                exit 1
        fi
done < data
exit 0

# 3  
Old 12-30-2007
Bug Thank you!

Smilie Thank you! But is it possible for you to explain me how it works ?

exec 5<> /dev/tty

I can understand that you are setting both input and output to /dev/tty. A little explanation will help me a lot.

thanks!
# 4  
Old 12-30-2007
It's not clear what needs explaining. Smilie

The exec opens fd 5 for i/o to /dev/tty. The read and print statement use this fd via the -u5 argument.
# 5  
Old 12-30-2007
no tensions......

thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Read in numbers from console won't stop at newline.

Hello, I have snippet code from Lippman's <<C++ primer>>. The program is to convert regular decimal (0 ~ 15) numbers to basic hexdecimals. The instruction tells the program will execute by hitting newline at the end. When I tried to run the compiled program, hitting ENTER did not work as... (3 Replies)
Discussion started by: yifangt
3 Replies

2. Shell Programming and Scripting

While read -a line not working in ksh

while read -a line; this is not working in ksh. what is the equivalent of this in ksh. read: -a: unknown option (2 Replies)
Discussion started by: archana25
2 Replies

3. Shell Programming and Scripting

ksh while read issue

Hello, I have used a chunk of ksh script similar to this in many places without any issue: while : do print; read OPTION?"Enter a number (q to quit): " expr ${OPTION} + 1 >/dev/null 2>&1 CHECKVAL=$? if }" != ${OPTION} ]; then ... (2 Replies)
Discussion started by: port43
2 Replies

4. Shell Programming and Scripting

Read a multiline text from a console - supporting arrow keys

Hi, I try to read a multiline text from a console and write it to a file using a bash script. I want to end reading from a console if a user types the key twice. I tried this loop: while read LINE; do if 2>/dev/null; then break fi echo -e ${LINE} >> $file done However, it doesn't... (8 Replies)
Discussion started by: wenclu
8 Replies

5. UNIX for Dummies Questions & Answers

Question with read command in ksh

Hi, I was writing a script to accept input using read. When I put !#/usr/bin/ksh in the beginning of the script, it accepts only 258 characters. And I remove it, it accepts more. I'm wondering what the difference is. Thanks in advance, (7 Replies)
Discussion started by: swmk18
7 Replies

6. Shell Programming and Scripting

Read from Log file in Ksh

I have a log file like.. IMPORT from /dataserver/ftp/bits/mdr/mdr_data_discon.dat OF DEL ..... Number of rows read = 1376 Number of rows skipped = 0 Number of rows inserted = 1374 Number of rows updated = 0 Number of rows rejected = 2 Number of rows... (4 Replies)
Discussion started by: ramse8pc
4 Replies

7. UNIX for Dummies Questions & Answers

simple way to read an array in ksh

hi, I'm a newbie to shell scripting. I wanted to initialise an array using basic for loop and read it. Then i want to print it as a .CSV file.. Any help would me much appreciated.. (1 Reply)
Discussion started by: pravsripad
1 Replies

8. Shell Programming and Scripting

is there any way to read a line twice in KSH

Hi All, Is there any way to read the previous line in file reading ? or is there any way to read a line twice in KSH ? thanks in advance !! Srini (6 Replies)
Discussion started by: Srini75
6 Replies

9. Shell Programming and Scripting

ksh read timeout

any idea on how to timeout the read statement for ksh? for bash u can use read -t option -t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has ... (2 Replies)
Discussion started by: ashterix
2 Replies

10. UNIX for Dummies Questions & Answers

How do i access (mount, read & write) a floppy disk from the console, not being root?

welll, the title quite explains what i want to do thanks for your time! (4 Replies)
Discussion started by: kfaday
4 Replies
Login or Register to Ask a Question