2014-05-12 4 views
0

ssh 인바운드 연결에서 3 개의 변수를 사용하는 스크립트를 작성합니다. ID A 이름 스위치 포트 번호명령에 대해 변수를 잘못 입력했습니다.

모두 처리 할 때까지 지느러미입니다. 그리고 저에게 이상한 오류 출력이 나옵니다.

./installations.sh: line 11: ID_1=c8:2a:14:25:b7:f8: command not found 
./installations.sh: line 12: Name_1=Jonass-MBP: command not found 
./installations.sh: line 14: installationID_2=00:1f:d0:db:b7:48: command not found 
./installations.sh: line 15: installationMame_2=JonasKirkPeders: command not found 
No attached system on port 
No attached system on port 

테스트 용으로 PC를 연결 했으므로 원격 스크립트에서 올바른 데이터가 전송되고 있음을 알 수 있습니다. 그러나 왜이 경우 변수가 설정 변수가 아닌 명령으로 잘못 취급됩니까?

echo $1 $2 $3 >> test.log 
ID=$1 
NAME=$2 
LANPORT=$3 

    if [ -z $1 ] || [ -z $2 ]; then 
    echo "No attached system on port $LANPORT" 
    else 
    case $LANPORT in 
    1) installationID$LANPORT=$ID 
     installationName_${LANPORT}=$NAME 
     ;; 
    2) installationID_$LANPORT=$ID 
     installationName_${LANPORT}=$NAME 
     ;; 
    3) installationID_$LANPORT=$ID 
     installationName_${LANPORT}=$NAME 
     ;; 
    4) installationID_$LANPORT=$ID 
     installationName_${LANPORT}=$NAME 
     ;; 
    *) echo "ERROR - NO such port!! ${LANPORT}" >> test.log 
     ;; 
esac 

echo "LAN $LANPORT - $installationID_$LANPORT" >> test.log 
fi 

나는 동적 변수 사용 declare를 만들려면 아이디어

+0

'이름 _ $ {LANPORT가}'잘못된 것입니다. 마찬가지로 몇 가지 과제가 잘못되었습니다. – devnull

+0

l-value error from C : D 정확히는 아니지만 bash 당신이 할 수있는 일은 파일에서 이들을 반향하고 그 파일을 소트 할 수 있습니다 :) 그렇지 않으면 "lvalue required"를 제거 할 수 없습니다. – PradyJord

답변

0

여기에 복잡한 변수 이름이 필요하지 않습니다. 그냥 installationID="$ID" 등을 사용하십시오.

[ -z "$1" ] 

아니 오류,하지만 당신은 case 문을 병합 할 수 있습니다 :

Use More Quotes

는, 그렇지 않으면 [ 문이 실패합니다

case 1|2|3|4) 
0
<string>_$<var>=<value> is wrong syntax to create dynamic variable. 

부족했다. 예 :

declare "ID_$LANPORT=$ID" 

내가 언급 한 오류가이 변수 할당에만 도움이 될 것이라고 생각합니다. =의 부품 왼쪽이 유효한 변수 이름 때

+0

위대한 내가 그것을 시도합니다 – Sifungurux

0

변수 할당 만 인식됩니다. $은 변수 이름에 허용되지 않으므로 토큰은 명령으로 처리됩니다. 과제를 평가해야합니다 :

eval "installationID$LANPORT=$ID" 
+0

잘 평가를 할 필요는 없지만, 귀하의 변수 선언에서 보았고 대시와 밑줄을 제거했습니다. Yoola, 작동하는 것 같습니다. – Sifungurux

관련 문제