2014-10-19 1 views
0

매우 비효율적 인이 스크립트를 사용하십시오. 쉘 스크립팅을 처음 사용합니다. matchFS() 함수의 if 절 근처에서 오류가 발생했습니다. 아래에 오류를 게시했습니다. 누구든지 나에게 약간의 지침을 제공 할 수 있습니까?bash에서 조건부 연산자 예상 오류가 발생했습니다

#!/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) 예상 : [[$ 1 당량은 "] 49] 경우 eq' line 13: 근처에 구문 오류, 당신은 help test 보면 다음 '

+1

가 http://www.shellcheck.net/ – Cyrus

답변

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 
1

이 빠르게 실현하겠습니다 eq 그게

+0

에서 살펴 보자. 적어도이 아니라 그것에 다른 것을 추가하지 않고. 선택 중 하나가 아닌하지만 숫자가 아닌 문자열을 비교하려면 인용문은 정확하거나 뭔가? – ams

+0

아직 보셨습니까? –

+0

와우 나는 그런 작은 일을 놓쳤다 니 믿을 수가 없어! 감사. – ams