2012-10-26 5 views
0

도움 주셔서 대단히 감사합니다 !!!값이 배열에 있는지 확인

나는 다음과 같은 코드가 있습니다

base[0]='coordfinder' 
base[1]='editor_and_options' 
base[2]='global' 
base[3]='gyro' 
base[4]='movecamera' 
base[5]='orientation' 
base[6]='sa' 

for d in $dest_include/*; do 
    if [ $d == "${base[@]}" ]; then 
     echo $plugin='y' >> vt_conf.sh 
    else 
     plugin=$(basename $d) 
     echo $plugin'?' 
     read $plugin 
     echo $plugin=${!plugin} >> vt_conf.sh 
    fi 
done 

그것은 작동하지 않습니다, 그러나 그것은 좋은 출발점입니다. 기본적으로 작동하지 않는 것은 if 루프입니다. 나는 그것을하는 방법을 모르기 때문에 나는 그것을 만들었다.

나는 다음을 수행 싶습니다

루프를 $ dest_include 폴더의 내용을 통해. forlders ($ d) 중 하나라도 배열의 요소 중 하나와 일치하면 다른 작업을 수행하십시오.

감사합니다 !!!

+0

정확히 어떻게 실패합니까? –

+0

'-bash : [: too many arguments coordfinder?']를 반환합니다. 실제로 배열에있는 coordfinder를 요구해서는 안됩니다. 너무 많은 인수 오류를 반환하기 때문에 놀라지 않습니다. – RafaelGP

+0

@RafaelGP'['명령으로 전달되면 (예, 네이티브가 아닌 명령입니다. 쉘 구문) 배열은 이미 확장되어 있으므로 쉘은 다음을 얻습니다 :'[''coordfinder'' ==''coordfinder'' editor_and_options'' global'' 자이로''movecamera'' orientation''sa'''' ; 그것은 당신의 잘못 뒤에 "왜"입니다. (그런데,'[]'와'=='를 사용하는 것은 유효한 POSIX 문법이 아닙니다. 이것은 쉘이'=='을 받아들이도록 해주는 bash 확장입니다. –

답변

1

일치하는 항목이 있으면 플래그를 설정하여 내부 루프를 반복합니다.

base=(coordfinder editor_and_options global gyro movecamera orientation sa) 
for d in "$dest_include/"*; do 
    found_match=0 
    for i in "${base[@]}"; do 
    [[ $d = "$i" ]] && { found_match=1; break; } 
    done 
    if ((found_match)) ; then 
    ...do one thing... 
    else 
    ...do the other... 
    fi 
done 
+0

무슨 프로? 나는 너의 대답을 좋아한다! 작동 시키려면 5 번째 줄을 다음과 같이 변경해야했습니다 : [[$ (basename $ d) = "$ i"]] && {found_match = 1; 단절; } basename을 추가하면 코드가 아름답게 작동합니다! – RafaelGP

+0

@RafaelGP 당신은 basename보다 나은 것을 할 수 있습니다 :'[[$ {d ## * /} = "$ i"]]'는 같은 일을하지만 서브 쉘을 사용하지 않는 (너무 빠름) 매개 변수 확장입니다. –

0

당신은 또한 주위에 체크를 돌 수 있었다 : 공백으로 구분 된 문자열로 전체 배열을 사용하고, 그 안에 공백으로 구분 된 단어와 일치하려고합니다.

for d in "$dest_include"/* do 
    if [[ " ${base[*]} " == *" $(basename "$d") "* ]]; then 
     do something with matching directory 
    else 
     do another thiing 
    fi 
done 
관련 문제