我被困在一个部分,我不明白为什么,让我粘贴我的代码:
local correctId=false
echo $ticketMessage
read deviceId
until [[ $deviceId =~ [0-9]+ && correctId = true ]]; do
if [ ! -e $baseDevicesPath"/$deviceId" ]; then
echo $deviceError
correctId=false
else
correctId=true
fi
if [[ ! $deviceId =~ [0-9]+ ]]; then
echo $ticketMessage
fi
read deviceId
done
echo "I DONT COME HERE?"
如果deviceId和correctId都为真,它应该退出until循环并继续前进?但它没有,不知道我在这里做错了什么?
答案 0 :(得分:1)
你只是有一个简单的拼写错误。在您的情况下,您在错误的ID前面缺少$
:
local correctId=false
echo $ticketMessage
read deviceId
until [[ $deviceId =~ [0-9]+ && $correctId = true ]]; do
if [ ! -e $baseDevicesPath"/$deviceId" ]; then
echo $deviceError
correctId=false
else
correctId=true
fi
if [[ ! $deviceId =~ [0-9]+ ]]; then
echo $ticketMessage
fi
read deviceId
done
echo "NOW YOU WILL END HERE"
答案 1 :(得分:0)
将correctId更改为$ correctId($ {correctId})。 我会添加双引号:
local correctId="false"
echo ${ticketMessage}
read deviceId
until [[ $deviceId =~ [0-9]+ && "${correctId}" = "true" ]]; do
if [ ! -e "${baseDevicesPath}/${deviceId}" ]; then
echo ${deviceError}
correctId="false"
else
correctId="true"
fi
if [[ ! "${deviceId}" =~ [0-9]+ ]]; then
echo ${ticketMessage}
fi
read deviceId
done
echo "Do you come here?"
答案 2 :(得分:0)
这里有一个更易读的解决方案
function findDevice {
echo $ticketMessage;
read deviceId;
while true; do
local errorMessage;
if [[ $deviceId =~ [0-9]+ ]]; then
if [ -e $baseDevicesPath"/$deviceId" ]; then
#valid input, breaking the loop
break;
fi
errorMessage=$deviceError;
else
errorMessage=$ticketMessage;
fi
echo $errorMessage;
read deviceId;
done
}