【发布时间】:2015-08-23 23:06:50
【问题描述】:
我已经创建了这个函数:
function promptFile()
{
while true;
do
read -p "Please provide a full path [q to quit]: " file
if [ $file == q ]; then
echo "Exiting.."
return 1
fi
if [ ! -f $file ]; then
echo "File does not exist, please try again"
else
echo $file
break
fi
done
}
要提示用户文件位置,再次询问文件是否不存在,如果存在则将输出保存到变量中,调用该函数:
tempLoc=$(promptFile)
if [ !tempLoc ]; then
fileLocation=$tempLoc
fi
除非有人写了错误的文件位置,否则一切正常,然后在有人单击 q 或输入现有文件位置之前不会显示回显。 在这种情况下,将打印回显消息 * 错误输入的数量,如下所示。
[root@tsting:0]# ./tst
Please provide a full path [q to quit]: tst1
Please provide a full path [q to quit]: tst2
Please provide a full path [q to quit]: tst3
Please provide a full path [q to quit]: tst4
Please provide a full path [q to quit]: q
File does not exist File does not exist File does not exist File does not exist Exiting..
[root@tsting:0]#
我猜这是因为循环在发生时折叠回打印所有回声,有没有办法避免这种情况并在输入错误的文件位置时打印回声?
【问题讨论】:
-
它肯定应该马上打印出来。如果你自己运行那个 sn-p 你还有同样的问题吗?
-
嗯,它不是,我认为这与我调用函数的方式有关
-
如果我自己运行一切正常:
标签: bash file loops while-loop echo