
How do I re-redirect stdin in a Unix or Linux shell script?Hi Dave. I'm trying to create a shell script in HP-UX Unix that looks like this: while read usrname do lsh $usrname done < $fileand in lsh I added some error checks. If one occurs I ask the user to correct their input, but the code didn't work that way and it continues to read another line from file usrname. I don't know how to stop in while loop, to get input from the user. What's the trick? This is a classic shell scripting question because once you redirect standard input to be from a file (as you do with the < $file after the done statement) then subshells, functions and everything else also inherit the changed file descriptor. The good news is that the solution is straightforward: just change standard input (stdin for you Unix geeks out there) to the keyboard, aka "/dev/tty". Here's a simple little script that demonstrates what I'm talking about: #!/bin/sh
while read text It's obviously not exactly what you have, but you can see how you can use the redirect in the while loop to pull input from a file (in this case I'm using $1 for simplicity). and then notice how the read statement has an input redirect too: < /dev/tty. Try that with your application and see if it works out! Avid shell script programmers will doubtless be psyched to learn more about my best-selling Wicked Cool Shell Scripts, a collection of over 100 fun and educational shell scripts.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon
Categorized:
Shell Script Programming
(Article 4053)
Tagged: Previous: How do I use a custom domain with a blogger account? Next: What are must-have Sony PSP games? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. You are a GOD! Thanks so much for this. I've been stuck on this exact problem for days. Posted by: da dr at September 7, 2006 2:11 PMI have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|