bash - 如何修复 '1: No such file or directory'

标签 bash

如何将 bash shell 脚本中的变量与小于 1 的变量进行比较?

我以前做过,但我得到“1:没有这样的文件或目录”

grep_result=`grep -r $ID . | wc -l`

echo $grep_result
# it said '1: No such file or directory'
if [ $grep_result < 1 ]; then
    echo "Wrong id.
    exit 1
fi

最佳答案

从 bash 的角度来看,这意味着将名为 1 的文件通过管道传输到 $grep_result 变量中。在这种情况下,您需要使用 -lt 运算符。 (le 代表小于)

grep_result=`grep -r $ID . | wc -l`

echo $grep_result
# it said '1: No such file or directory'
if [ $grep_result -lt 1 ]; then
    echo "Wrong id.
    exit 1
fi

关于bash - 如何修复 '1: No such file or directory',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13500337/

相关文章:

bash - 如何从异步调用的函数中获取返回值

linux - 如何使用 bash 从文本文件中删除前两行和后四行?

mysql - Bash 脚本 - 将 .TAR 文件分割成更小的部分

bash - POSIX 和 Bash 脚本

bash - 如何设置 Laravel 安装程序和 Laravel Spark 安装程序的 bash PATH?

json - 使用 jq 和引用值处理 json 数组

php - 为什么从 CRON 运行 php 脚本会导致字符编码问题?

php - 是什么杀死了这个进程以及如何阻止它这样做

arrays - 我可以使用 read 内置函数将换行符分隔的字符串放入数组中吗?

bash - 似乎不理解 bash 的正确性