-1

运行这个简单的 shell 脚本时出现此错误:

previous_run=$(cat last_completed.txt)
for file in /Users/everardobarriga/testimg
do
   b=$(ls -l "$file"|awk '{print $6$7$8}')
   if [ "$previous_run" <= "$b" ]
     then
       echo $'yes'
   fi
done
date +"%h%d%H:%M"> last_completed.txt

似乎在我比较上一次运行和 b 的那一行上,并没有真正比较它们。我对 shell 脚本完全陌生,所以我不确定发生了什么。有时它会比较它们,有时它不会。任何帮助是极大的赞赏!

4

2 回答 2

0

To sort string lexicographically you can use < or > inside test. However the less or greater than symbol needs to be in quotes so the shell doesn't interpret them as redirection.

Example

if [ "str1" '<' "str2" ]; then
    echo "str1 is before str2"
else
    echo "str1 is after str2"
fi
于 2013-06-01T01:37:37.413 回答
0

您不能将“<=”用作小于或等于比较器。您指定的语法试图从名为 '=' 的文件(不存在)重定向标准输入。请参阅“man test”,您将看到各种可用的比较选项。在这种情况下,您似乎需要“-le”:

if [ "$previous_run" -le "$b" ]; then
    echo "yes"
fi
于 2013-06-01T00:45:30.413 回答