Free tech support / small logo


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.









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
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

What if the input is coming from the terminal and you want to see the prompt and printf at the terminal (see below), but when the command was issued on the console it redirected stdout and stderr, like:

===> cmd 1>t1 2>t2

read -rep "Input y/n: " newinput
printf "You responded: %s\n" "${newinput}"

What I've done is redirect both, &>/dev/tty, but I'm not understanding exactly why it works. Thoughts...

Posted by: dj at September 4, 2010 3:14 PM

dj, it's because /dev/tty isn't the same as /dev/console. On most Linux systems (as I recall) the console gets its own device driver. Try redirecting INPUT like this:

read variable < /dev/tty

then

read variable < /dev/console

and see what happens.

Posted by: Dave Taylor at September 6, 2010 8:06 AM

Hi Dave, I'm new to the Linux environment and trying to develop an embedded app in C on a Digi cc9p9215. I wish to include a dedicated monitor/control via telnet but have hit a road block. I can establish a connection on a port other than 23, but when I try to use port 23 I cannot bind because the address is already in use (by the OS) How can I redirect the connection on port 23 so that instead of dropping into the OS prompt, it is picked up by the telnet task in my program? I believe I can use a redirect in "shells" but I have been struggling with this for a couple of weeks now! Any pointers would be VERY welcome. Thanks.

Posted by: David Briggs at May 19, 2011 11:57 AM

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.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 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.