I'm trying to figure out why the code below doesn't work, and gives me an error Bad file descriptor. It is sort of a follow-up to this question as applies to the script I'm currently working on.
Early on in the caller, exec 3>&1 is run, and nothing later explicitly changes it before the (generalized) function below is called like so:
exec 3>&1
...
string=$(GetString)
GetString looks like this:
GetString()
{
4>&1 1>&3 #save pipe end and change output back to caller's
controlvar=0
while ((controlvar != 1))
do
printf "some stuff for the interactive user\n"
read -p "my prompt" variable
if ValidationFunction $variable; controlvar=1;fi #tests for valid input
done
exec 1>&4- #change output back to pipe end
echo $variable
}
I'm getting the Bad file descriptor error on the second to last line.
What's going on here? Note that I'm also not doing anything explicitly with fd/4 elsewhere in the script.
exec 1>4-is incorrect, useexec 1>&4-.exec's... Can't you just pass file names? E.g.<command> << "$infile" >> "$outfile", or something like that.exec 1>4&-is still wrong, And I am leaning toward recommending the use of the simplerexec 4>&-to close file descriptor4but only afterexec 1>&0to recover stdout.1>&4-, well in fact, the equivalent>&4-. What you have written here is>4&-. If you fail to see the difference, well, nothing else to say.4>&1 1>&3line.