在bash中接收条件运算符预期错误

时间:2014-10-19 02:24:43

标签: bash

请原谅这个效率极低的脚本,我是shell脚本新手。我在函数matchFS()中的if子句附近收到错误。我在下面发布了错误。谁能给我一些指导?

 #!/bin/bash

function matchFS() {

usage=$(df -h | tail -n +2 | awk '{print $5}' | sed 's/%//g')
usagearr=( $usage )
for i in "${usagearr[@]}"
do
        if [[ $1 eq "${usagearr[$i]}" ]]; then
        # print matching row from df -h
        fi
done

}

usage=$(df -h | tail -n +2 | awk '{print $5}' | sed 's/%//g')
usagearr=( $usage )
len=${#usagearr[@]}

for (( i=0; i<$len; i++ )) # we have to use (( )) here to represent the c style for loop
do
        if [ "${usagearr[$i]}" -gt "10" ]; then
                matchFS ${usagearr[$i]}
        fi
done

错误:第13行:预期的条件二元运算符 第13行:eq' line 13:附近的语法错误,如果[[$ 1 eq“49]];那么'

2 个答案:

答案 0 :(得分:1)

如果你看help test,你会很快意识到eq不是其中一个选择。至少,不是没有添加别的东西。

答案 1 :(得分:0)

#!/bin/bash

function matchFS() {

 ### duplicate definition, these are already known to the function.
usage=$(df -h | tail -n +2 | awk '{print $5}' | sed 's/%//g') 
usagearr=( $usage )
### you probably did want to use another variable here,
###  because the "i" is also shared with the caller
for i in "${usagearr[@]}"
do
 ### -eq instead of eq
        if [[ $1 -eq "${usagearr[$i]}" ]]; then
 ### the if statement can not be empty
    # print matching row from df -h
    fi
done
}

usage=$(df -h | tail -n +2 | awk '{print $5}' | sed 's/%//g')
usagearr=( $usage )
len=${#usagearr[@]}
for (( i=0; i<$len; i++ )) # we have to use (( )) here to represent the c style for loop
do
        if [ "${usagearr[$i]}" -gt "10" ]; then
                matchFS ${usagearr[$i]}
        fi
done