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