Register
It is currently Sat Apr 19, 2014 11:03 am

[ SOLVED ] reading a variable from terminal.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Mar 22, 2010 7:28 am   
User avatar

Joined: Mon Mar 22, 2010 7:18 am
Posts: 2
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:
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:
Code:
/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:
Code:
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.


Last edited by HUB on Mon Mar 22, 2010 10:06 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Mon Mar 22, 2010 10:06 am   
User avatar

Joined: Mon Mar 22, 2010 7:18 am
Posts: 2
I think, I've solved the problem. I'm writing this post hoping it will be indexed by search engines and the next, who will meet this problem, will find the solution.

The source of the error was in what was the script's stdin. Executing ls -l /dev/$$/fd from the script showed:
Code:
+ ls -l /proc/$$/fd/
total 0
l-wx------ 1 root root 64 Mar 22 18:49 0 -> /dev/null
lrwx------ 1 root root 64 Mar 22 18:49 1 -> /dev/pts/4
lrwx------ 1 root root 64 Mar 22 18:49 2 -> /dev/pts/4
lr-x------ 1 root root 64 Mar 22 18:49 255 -> /etc/vz/dists/scripts/postcreate_sles11.sh
l-wx------ 1 root root 64 Mar 22 18:49 3 -> /var/log/vzctl.log
lrwx------ 1 root root 64 Mar 22 18:49 4 -> /dev/vzctl
lr-x------ 1 root root 64 Mar 22 18:49 5 -> pipe:[2137390]
l-wx------ 1 root root 64 Mar 22 18:49 6 -> pipe:[2137390]
lr-x------ 1 root root 64 Mar 22 18:49 7 -> pipe:[2137391]
l-wx------ 1 root root 64 Mar 22 18:49 8 -> pipe:[2137391]
l-wx------ 1 root root 64 Mar 22 18:49 9 -> /dev/null


So, here is the solution! The developer of vzctl (which runs the script) has set stdin to /dev/null. But, fortunately, he left stdout and stderr untouched. This is why, the only thing to fix my problem was to add
Code:
exec 0<&1

right before the read statement in the script.
After I added this line, the script began working well.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP