Ask Dave Taylor: Tech and Business

Friday

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?

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...