#!/usr/bin/bash
for (( i=0; i<=10; i++ ))
do
if [ "$i" -eq 3 ] || [ "$1" -eq 7 ]
then
continue
fi
echo "$i"
done
运行这个时,我得到
file: line 4: [: : integer expression expected
0
file: line 4: [: : integer expression expected
1
file: line 4: [: : integer expression expected
2
file: line 4: [: : integer expression expected
4
file: line 4: [: : integer expression expected
5
file: line 4: [: : integer expression expected
6
file: line 4: [: : integer expression expected
7
file: line 4: [: : integer expression expected
8
file: line 4: [: : integer expression expected
9
file: line 4: [: : integer expression expected
10
...我不明白为什么。
答案1
如果您不带参数调用脚本,则命令:
[ "$1" -eq 7 ]
将评估为:
[ "" -eq 7 ]
这是一个错误,就像-eq
数字运算符一样:它需要两个整数作为操作数;空字符串不是整数。
如果您不希望使用参数调用脚本,而是希望比较i
to的值7
,您可能需要:
[ "$i" -eq 7 ]
而不是:
[ "$1" -eq 7 ]