返回错误 [:非法数字:100k

返回错误 [:非法数字:100k

我在 Ubuntu 终端中运行一个程序,输入如下

10万

示例文件

~/

和程序代码

#!/bin/sh
#!/bin/bash
echo "File size you want to archive"
read size
echo "File name to archive"
read name
echo "Path to archive"
read path
echo "Size is: $size filename is: $name path is: $path"
a=99
b=100
if [ "$size" -lt $a ] #line 19
then
    echo "File is small. Refused to archive"
elif [ "$size" -lt $b ] #line 22
then
    find "$path" -type f -size "$size" | tar cvzf ~/"$name".tar.gz
else
    echo "Wrong input"
fi

执行后返回错误

./try.sh:18:[:非法数字:100k

./try.sh:21:[:非法数字:100k

有人能解释一下这段代码有什么问题吗?

答案1

用于numfmt将人类可读的字符串转换为数字。

#!/bin/bash

echo "File size you want to archive"
read size

echo "File name to archive"
read name

echo "Path to archive"
read path

echo "Size is: $size filename is: $name path is: $path"

a=99000
b=100000

num=$(numfmt --from auto "$size" 2>&-)

if [[ -z $num ]]
then
    echo "Invalid size!"
    exit 1
fi

if (( $num < $a )) #line 19
then
    echo "File is small. Refused to archive"
elif (( $num <= $b )) #line 22
then
    find "$path" -type f -size +$((num - 1))c ! -size +${b}c | tar cvzf ~/"$name".tar.gz
else
    echo "file is too large!"
fi

相关内容