Computing.Net > Forums > Unix > cat vs. read

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

cat vs. read

Reply to Message Icon

Name: Jo
Date: December 4, 2001 at 09:59:14 Pacific
Comment:

I am writing a shell script (it happens to be on Solaris in KSH) which recieves a file from standard input -- in other words, when I run the script, I do this:

cat text_file | ./my_script

In the script, I send the input to an output file ( "cat > out_file"). Later in the script, I try to prompt for input from the user. However, I've tried both a "read" and a "ckyorn" to get input from the user, but the script does not wait for input from the user. It just goes on to the next line of the script and uses nulls for the answer. Any thoughts on how I can force the script to read new input from stdin? Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: December 5, 2001 at 10:48:15 Pacific
Reply:

Jo,
An exec command will switch stdin from the pipe to the terminal, as shown below. But note that less-than-signs get special handling by this web site, so in the following code, on the first exec line, the X should actually be a less-than-sign:

cat > out_file
exec X /dev/tty
echo 'enter ans: \c'
read ans
echo "your answer was $ans"

Also, when I want my ALL script output to go to a log file, I can do that inside the script with:

exec > /mydir/mylogfile

and with the above in effect, individual commands can still be forced to the screen, such as:

echo 'Enter answer: \c' > /dev/tty


0

Response Number 2
Name: Jo
Date: December 10, 2001 at 06:26:46 Pacific
Reply:

THANK YOU! This actually works!! :) I had all my co-System Administrators stumped with this one!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: cat vs. read

problem using cat to read data www.computing.net/answers/unix/problem-using-cat-to-read-data/6692.html

cat file.cfg read HOST PORT www.computing.net/answers/unix/cat-filecfg-read-host-port-/3570.html

Script: Read file to set variables www.computing.net/answers/unix/script-read-file-to-set-variables/3849.html