Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

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 < $file
and 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?

Dave's Answer:

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
do
  if [ "$text" = "fi" ] ; then
    echo -n 'saw FI, need something else: '
    read newinput < /dev/tty
    echo new input. Read $newinput
  else
    echo "> " $text
  fi
done < $1

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    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

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 PM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.








Ask Dave Taylor: The iPhone App: Advertisement



Follow me on Twitter @DaveTaylor

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!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2010 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.