“阅读” bash 脚本中的命令被跳过
如果我需要擦除硬盘或者需要在另一台机器上安装 Linux,我正在创建一个脚本来更新我的 Linux 发行版。所以这个脚本基本上安装了我通常需要的所有程序。一开始有一个“读取”命令,询问我是否要自动安装所有软件包。如果我选择不,对于每个未找到的程序,它应该询问我是否要安装它并使用此代码
if [[ $installall == "yes" ]]; then
echo " Installing $sciprog..."
sudo apt-get install -y $sciprog >/dev/null
{
scitest=`dpkg -s $sciprog | grep Status`
} 2>${HOME}/musthave.errorlog
if [[ $scitest != "Status: install ok installed" ]]; then
echo " I've encountered problems installing $sciprog that I can't resolve. "
echo " Consider installing $sciprog manually. "
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.notinstalled
else
echo " $sciprog installed correctly!"
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.installed
fi
else
echo " Seems like $sciprog is not installed... Do you want to download it?"
echo " Type 'y' for yes."
read secondyn ### THIS IS THE GUILTY COMMAND ###
if [[ $secondyn == "y" ]]; then
echo " Installing $sciprog ..."
sudo apt-get install -y $sciprog >/dev/null
{
checkinstall=`dpkg -s $sciprog | grep Status`
} 2>>${HOME}/musthave.errorlog
if [[ $checkinstall != "Status: install ok installed" ]]; then
echo " I've encountered problems installing $sciprog that I can't resolve. "
echo " Consider installing $sciprog manually. "
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.notinstalled
else
echo " $sciprog installed correctly!"
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.installed
fi
else
echo " Skipping $sciprog ..."
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.notinstalled
fi
### some more code which works as expected. All the code above is inside a
### while...do...done loop which reads line by line the file at the end
done <${HOME}/file.list
但是如果我运行脚本,它会跳过 else 子句中的“read”命令并假设它是“n “...
我不明白为什么,if...then...else...fi
循环中还有其他读取函数,并且它们按预期工作...
任何想法?
I'm creating a script to update my linux distribution if I need to wipe the HD or I need to install Linux on another machine. So this script basically install all the programs I usually need. At the beginning a have a "read" command that asks if I want to install all the packages automatically or not. If I choose not, for each program not found it should ask me I want it to be installed and I use this code
if [[ $installall == "yes" ]]; then
echo " Installing $sciprog..."
sudo apt-get install -y $sciprog >/dev/null
{
scitest=`dpkg -s $sciprog | grep Status`
} 2>${HOME}/musthave.errorlog
if [[ $scitest != "Status: install ok installed" ]]; then
echo " I've encountered problems installing $sciprog that I can't resolve. "
echo " Consider installing $sciprog manually. "
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.notinstalled
else
echo " $sciprog installed correctly!"
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.installed
fi
else
echo " Seems like $sciprog is not installed... Do you want to download it?"
echo " Type 'y' for yes."
read secondyn ### THIS IS THE GUILTY COMMAND ###
if [[ $secondyn == "y" ]]; then
echo " Installing $sciprog ..."
sudo apt-get install -y $sciprog >/dev/null
{
checkinstall=`dpkg -s $sciprog | grep Status`
} 2>>${HOME}/musthave.errorlog
if [[ $checkinstall != "Status: install ok installed" ]]; then
echo " I've encountered problems installing $sciprog that I can't resolve. "
echo " Consider installing $sciprog manually. "
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.notinstalled
else
echo " $sciprog installed correctly!"
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.installed
fi
else
echo " Skipping $sciprog ..."
{
echo "=========="
echo " $sciprog"
} >>${HOME}/musthave.notinstalled
fi
### some more code which works as expected. All the code above is inside a
### while...do...done loop which reads line by line the file at the end
done <${HOME}/file.list
But if I run the script, it skips the "read" command in the else clause and assumes it to be "n"...
I can't figure out why, there are other read function also inside if...then...else...fi
loops and they work as expected...
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码的相关部分仍然不完整,但根据注释,我猜测您的 while 循环看起来像
这样,问题立即显而易见:内部
read
正在从以下位置获取输入与外循环相同的位置,即从file
重定向的 stdin,而不是用户。对于不依赖于/dev/tty
的内核级支持的稍微更便携的替代方案,只需在 while 循环中使用 stdin 以外的不同文件描述符即可。请注意,此示例使用 fd 9 作为文件,仅保留 fd 0 (stdin)。请查看 BashFAQ 089 了解更多详细信息。
The relevant portions of the code are still not complete but based on the comments I'm going to guess that your while loop looks like
From this the problem is immediately apparent: the inner
read
is getting its input from the same place as the outer loop, namely stdin which has been redirected fromfile
, and not the user. For a slightly more portable alternative that does not depend on kernel-level support for/dev/tty
, just use a different file descriptor other than stdin for the while loop.Notice that this example uses fd 9 for the file, leaving fd 0 (stdin) alone. Take a look at the BashFAQ 089 for more details.
尝试从控制终端设备读取:
Try reading from the controlling terminal device:
这将查看父级的输入,该输入仍然应该是标准输入。
That will look to the parent's input, which should still be stdin.