Bash 스크립트의 구문 오류: 예상치 못한 토큰 'else' 근처

Bash 스크립트의 구문 오류: 예상치 못한 토큰 'else' 근처
#!/bin/bash

input=""

echo "Does a wall needs to be sent?"
read input

if [ $input="yes" ]; then
   echo "Sending message to all users"
   echo ""
else if [ $input="no"]; then
    exit
    fi
fi
echo "Is this a reboot or shutdown?"
      read input
if [ $input="reboot" ]; then
   reboot
elif [ $input="shutdown" ]; then
else
echo ""
echo "Goodbye"

답변1

이 스크립트에는 많은 문제가 있습니다. 정리된 버전은 다음과 같습니다.

#!/usr/bin/env bash

input=""

echo "Does a wall needs to be sent?"
read input

if [ "$input" = "yes" ]; then
    echo "Sending message to all users\n"
elif [ "$input" = "no" ]; then
    exit
fi

echo "Is this a reboot or shutdown?"
read input

if [ "$input" = "reboot" ]; then
    reboot
elif [ "$input" = "shutdown" ]; then
    shutdown -h now
fi

echo "\nGoodbye"

그러나 솔직히 말해서 여전히 매우 열악했습니다. case입력을 읽는 대신 문을 사용하여 매개변수를 구문 분석하는 것이 좋습니다 .

관련 정보