BASH - 使用 until 或 while 循环时得到无效响应
BASH - getting invalid response when using until or while loop
我能够在单独的脚本上创建 if-elif-then-else 代码块并验证工作正常。但是,当我添加 UNTIL 或 WHILE 逻辑时,我得到了无效响应。如果我手动创建 VALIDATE.txt,我 NOT 期望得到此响应 = "invalid operation" 文件,其中包含 "proceed" 或 "rollback"。请检查下面我的代码
同时脚本
#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
ACTION=`cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt >>/dev/null 2>&1`
while [ ! -f "$FILE" ]
do
echo "not exist"
sleep 60
done
if [ "${ACTION}" = "proceed" ];
then
echo "performing proceed"
rm -rf $FILE
elif [ "${ACTION}" = "rollback" ];
then
echo "performing rollback"
rm -rf $FILE
else
echo "invalid operation"
rm -rf $FILE
fi
直到脚本
#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
ACTION=`cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt >>/dev/null 2>&1`
until [ -f "$FILE" ]
do
echo "not exist"
sleep 60
done
if [ "${ACTION}" = "proceed" ];
then
echo "performing proceed"
rm -rf $FILE
elif [ "${ACTION}" = "rollback" ];
then
echo "performing rollback"
rm -rf $FILE
else
echo "invalid operation"
rm -rf $FILE
fi
我在单独的 window 上创建了 VALIDATE.txt 并检查了自己
lagot-pc:BASH-TEST lagot$ echo proceed >> VALIDATE.txt
lagot-pc:BASH-TEST lagot$ pwd
/Users/lagot/Documents/BASH-TEST
lagot-pc:BASH-TEST lagot$ cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt
proceed
我收到无效响应 "invalid operation" 而不是 "performing proceed" 如果我尝试创建 VALIDATE.txt 并在文件
中回显 "rollback"
lagot-pc:BASH-TEST lagot$ ./while-script.sh
not exist
not exist
not exist
not exist
not exist
invalid operation
原始代码的第一部分 ,更糟糕的是,它在确保 $FILE
存在之前错误地尝试赋值。所以更改这些行:
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
ACTION=`cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt >>/dev/null 2>&1`
until [ -f "$FILE" ]
do
echo "not exist"
sleep 60
done
收件人:
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
until [ -f "$FILE" ]
do
echo "not exist"
sleep 60
done
ACTION=$(<$FILE)
或者,(因为 $(<$FILE)
returns 一个错误代码),少一行:
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
until { ACTION=$(<$FILE); } 2> /dev/null
do
echo "not exist"
sleep 60
done
在那之后,测试 ${ACTION}
的原始代码的其余部分应该可以工作。
不要混淆 >>
重定向(附加到现有文件,如果文件不存在则创建)与 >
(覆盖文件,如果文件不存在则创建)。所以这样的代码 echo proceed >> VALIDATE.txt
应该是 echo proceed > VALIDATE.txt
,否则如果 VALIDATE.txt 已经存在并且包含 "rollback" 结果将是两行 VALIDATE.txt file:
rollback
proceed
现在已经修复了,我还在下面添加了 UNTIL 和 WHILE 脚本的新格式
在脚本中修复
#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
while [ ! -f "$FILE" ]
do
echo "not exist"
sleep 60
done
if [ `cat "$FILE"` = "proceed" ];
then
echo "performing proceed"
rm -rf $FILE
elif [ `cat "$FILE"` = "rollback" ];
then
echo "performing rollback"
rm -rf $FILE
else
echo "invalid operation"
rm -rf $FILE
fi
在 UNTIL 脚本中修复
#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
until [ -f "$FILE" ]
do
echo "not exist"
sleep 60
done
if [ `cat $FILE` = "proceed" ];
then
echo "performing proceed"
rm -rf $FILE
elif [ `cat $FILE` = "rollback" ];
then
echo "performing rollback"
rm -rf $FILE
else
echo "invalid operation"
rm -rf $FILE
fi
我能够在单独的脚本上创建 if-elif-then-else 代码块并验证工作正常。但是,当我添加 UNTIL 或 WHILE 逻辑时,我得到了无效响应。如果我手动创建 VALIDATE.txt,我 NOT 期望得到此响应 = "invalid operation" 文件,其中包含 "proceed" 或 "rollback"。请检查下面我的代码
同时脚本
#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
ACTION=`cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt >>/dev/null 2>&1`
while [ ! -f "$FILE" ]
do
echo "not exist"
sleep 60
done
if [ "${ACTION}" = "proceed" ];
then
echo "performing proceed"
rm -rf $FILE
elif [ "${ACTION}" = "rollback" ];
then
echo "performing rollback"
rm -rf $FILE
else
echo "invalid operation"
rm -rf $FILE
fi
直到脚本
#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
ACTION=`cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt >>/dev/null 2>&1`
until [ -f "$FILE" ]
do
echo "not exist"
sleep 60
done
if [ "${ACTION}" = "proceed" ];
then
echo "performing proceed"
rm -rf $FILE
elif [ "${ACTION}" = "rollback" ];
then
echo "performing rollback"
rm -rf $FILE
else
echo "invalid operation"
rm -rf $FILE
fi
我在单独的 window 上创建了 VALIDATE.txt 并检查了自己
lagot-pc:BASH-TEST lagot$ echo proceed >> VALIDATE.txt
lagot-pc:BASH-TEST lagot$ pwd
/Users/lagot/Documents/BASH-TEST
lagot-pc:BASH-TEST lagot$ cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt
proceed
我收到无效响应 "invalid operation" 而不是 "performing proceed" 如果我尝试创建 VALIDATE.txt 并在文件
中回显 "rollback"lagot-pc:BASH-TEST lagot$ ./while-script.sh
not exist
not exist
not exist
not exist
not exist
invalid operation
原始代码的第一部分 $FILE
存在之前错误地尝试赋值。所以更改这些行:
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
ACTION=`cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt >>/dev/null 2>&1`
until [ -f "$FILE" ]
do
echo "not exist"
sleep 60
done
收件人:
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
until [ -f "$FILE" ]
do
echo "not exist"
sleep 60
done
ACTION=$(<$FILE)
或者,(因为 $(<$FILE)
returns 一个错误代码),少一行:
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
until { ACTION=$(<$FILE); } 2> /dev/null
do
echo "not exist"
sleep 60
done
在那之后,测试 ${ACTION}
的原始代码的其余部分应该可以工作。
不要混淆 >>
重定向(附加到现有文件,如果文件不存在则创建)与 >
(覆盖文件,如果文件不存在则创建)。所以这样的代码 echo proceed >> VALIDATE.txt
应该是 echo proceed > VALIDATE.txt
,否则如果 VALIDATE.txt 已经存在并且包含 "rollback" 结果将是两行 VALIDATE.txt file:
rollback
proceed
现在已经修复了,我还在下面添加了 UNTIL 和 WHILE 脚本的新格式
在脚本中修复
#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
while [ ! -f "$FILE" ]
do
echo "not exist"
sleep 60
done
if [ `cat "$FILE"` = "proceed" ];
then
echo "performing proceed"
rm -rf $FILE
elif [ `cat "$FILE"` = "rollback" ];
then
echo "performing rollback"
rm -rf $FILE
else
echo "invalid operation"
rm -rf $FILE
fi
在 UNTIL 脚本中修复
#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
until [ -f "$FILE" ]
do
echo "not exist"
sleep 60
done
if [ `cat $FILE` = "proceed" ];
then
echo "performing proceed"
rm -rf $FILE
elif [ `cat $FILE` = "rollback" ];
then
echo "performing rollback"
rm -rf $FILE
else
echo "invalid operation"
rm -rf $FILE
fi