I'm trying to write a small script to be executed after OpenVZ
container creation. Everything goes fine until I need a user input.
Here is the code:
while [ "$var_ans" != "y" ] && [ "$var_ans" != "n" ]; do
echo -n "Would you like to configure 2 veth interfaces (DMZ
setup) inside the container? [y/n] "
read -e var_ans
done
When I run this script manually ( ./script.sh ), the input is
performed normally, the variable is set and everything is OK. But when
the script is run by a program ("vzctl" in my case), I'm getting
errors like these in the terminal:
/etc/vz/dists/scripts/postcreate_sles11.sh: line 137: read: read
error: 0: Bad file descriptor
And the "while" loop continues, producing these messages until I kill
the script. So, where is the pitfall? Is this because the script isn't
run directly from the terminal (by vzctl program) or because all this
is done by SSH? May be, I should use any other input method in this
case?
Version info:
GNU bash, version 3.2.48(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
If any additional info is required, let me know.
Thank you.
On Mar 22, 3:10 pm, HUB <net.for....@gmail.com> wrote:
> Hello, everyone!
>
> I'm trying to write a small script to be executed after OpenVZ
> container creation. Everything goes fine until I need a user input.
> Here is the code:
>
> while [ "$var_ans" != "y" ] && [ "$var_ans" != "n" ]; do
> echo -n "Would you like to configure 2 veth interfaces (DMZ
> setup) inside the container? [y/n] "
> read -e var_ans
> done
>
> When I run this script manually ( ./script.sh ), the input is
> performed normally, the variable is set and everything is OK. But when
> the script is run by a program ("vzctl" in my case), I'm getting
> errors like these in the terminal:
>
> /etc/vz/dists/scripts/postcreate_sles11.sh: line 137: read: read
> error: 0: Bad file descriptor
>
> And the "while" loop continues, producing these messages until I kill
> the script. So, where is the pitfall? Is this because the script isn't
> run directly from the terminal (by vzctl program) or because all this
> is done by SSH? May be, I should use any other input method in this
> case?
>
This happens because you are not running in an interactive shell.
You might want to read this page http://tldp.org/LDP/abs/html/intandnonint.html
>
> Thank you.
HTH
Maxim.
On 25 мар, 13:31, Maxim Veksler <hq4e...@gmail.com> wrote:
> This happens because you are not running in an interactive shell.
> You might want to read this pagehttp://tldp.org/LDP/abs/html/intandnonint.html
Thanks for your answer, Maxim.
The article is quite useful.
The problem was in file descriptors. As I wrote before the script is
run by a program and inherited its stdin, stdout and stderr
descriptors. For some purpose stdin for this program was pointing to /
dev/null. As I work mostly via SSH, I've just added "exec 0<&1" to the
begining of the script. This line sets stdin equal to stdout, which
is /dev/pty* in SSH case.