문제

Im writing a piece of a script that takes 3 variables from a ssh inbound connection. A ID A Name And a switch port number

All is fin until the case is handled. And to me get a strange error output.

./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

Have to PC connected for testing purpose and I can see the correct data is being sent from remote script. But why does the variables in the case mistakenly handle as commands and not just variables to set?

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

I have ran out of ideas

도움이 되었습니까?

해결책

You don't need complex variable names here; just use installationID="$ID" and the like.

Use More Quotes, otherwise the [ statements will fail:

[ -z "$1" ]

Not an error, but you can merge the case statements:

case 1|2|3|4)

다른 팁

<string>_$<var>=<value> is wrong syntax to create dynamic variable.

To create dynamic variable use declare. e.g.

declare "ID_$LANPORT=$ID"

I think this will help as the error mentioned is only with this variable assignment.

Variable assignments are only recognized when the part left of the = is a valid variable name. Since $ is not allowed in a variable name, the token is treated as a command. You need to eval the assignments:

 eval "installationID$LANPORT=$ID"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top