如何在执行脚本时轮询变量?

如何在执行脚本时轮询变量?

我想在执行脚本时连续轮询从文件中读取的布尔变量。

如果脚本启动,则必须启动轮询(该部分正在运行)。我只想让它运行直到脚本完成。可以这样做吗?现在我正在使用下面的代码。问题是每次我在脚本中提出问题或必须填写字段时,变量都会写入那里。

有人能告诉我如何处理这个问题吗?

#!/bin/bash

while read var                 # read variable in specified file (0 or 1)       
do           
if  [ "$var" -eq "0" ] ; then  # if variable is 0 throw error message
    echo "error"
fi

# some code below
/
/
/
done </path_to_file/file        # specified path to file
exit                            # end of script, exit

添加新代码的条目:

放置#poll=... 以使代码更具可读性。

#!/bin/bash                              # start of program

while true 
do

#poll=`grep "1" /Path_To_File/File        # poll if variable = 1
if  [ -n "$poll" ] ; then varpoll=1 ; else varpoll=0 ; fi 

if  [ "$varpoll" -eq "0" ] ; then # if variable = 0 throw error message
echo "Warning"
fi

sleep 0.25    
done 
exit

答案1

#!/bin/bash

while read var                 # read variable in specified file (0 or 1)       
do           
if  [ "$var" -eq "0" ] ; then  # if variable is 0 throw error message
    echo "error"
fi

echo "What is your name?";
read name </dev/tty;
echo "You entered: $name"; sleep 2;

done </path_to_file/file        # specified path to file
exit                            # end of script, exit

相关内容