【发布时间】:2020-09-06 02:29:40
【问题描述】:
我正在尝试构建一个脚本,要求用户输入文件大小和路径,如果文件大小大于文件应该删除的限制,问题是即使文件小于输入的值,脚本还是删除了那个文件,我认为我的Else语句有错误
我已经尝试过“https://www.shellcheck.net/”并给了我这个错误,我不知道如何解决它
if [ "$SIZE" -gt "$limit" ];
^-- SC2154: limit is referenced but not assigned.
#!/bin/bash
limit="$1"
shift 1
for file in "$@"; do
SIZE="$(stat --format="%s" "$file")"
if [ "$SIZE" -gt "$limit" ];
then
echo "$file is $SIZE bytes. Deleting..; -rm $file"
else
echo "file is smaller then limit no delete"
fi
done
编辑:我删除了“读取”,现在我得到了这个错误“[:-gt:一元运算符预期” 即使文件大小大于输入的值,它也会直接转到 else 语句
【问题讨论】:
-
read -i limit="$1"应该做什么?你如何调用你的脚本? -
read用于从终端输入设置变量,$1用于访问命令行参数。 -
现在我收到此错误 [: -gt: unary operator expected 并且即使文件大小大于输入的值,它也会直接转到 else 语句
标签: linux centos centos7 redhat