2014-12-19 5 views
0

여기에 약간의 bash 스크립트가 있는데 문제를 해결하려고하는데 "예기치 않은 파일 끝"이라는 구문 오류가 계속 나타납니다. 그것은 차단 또는 차단을 해제하고 포트 유형을 묻고 오류가 있는지 묻습니다.구문 오류 : 예기치 않은 파일 끝

도움을 주시면 감사하겠습니다.

#!/bin/bash 

PTYPET="What kind of port? [udp] or [tcp] or [both] :" 
PTEXTT="What port? [number] :" 

echo "Would you like to block or unblock? [b] or [u] :" 
read choice 

if [ $(choice) == "u" ]; then 
    echo $PTYPET 
    read port-type 
    echo $PTEXTT 
    read port 
    if [ $(ptype-text) == "both" ]; then 
     /sbin/iptables -A INPUT -p $port-type -m tcp --dport $port -j ACCEPT 
     /sbin/iptables -A INPUT -p $port-type -m udp --dport $port -j ACCEPT 
    else 
    /sbin/iptables -A INPUT -p $port-type -m $port-type --dport $port -j ACCEPT 
fi 

else 
    echo $PTYPET 
    read port-type 
    echo $PTEXTT 
    read port 
    if [ $(ptype-text) == "both" ]; then 
     /sbin/iptables -A INPUT -p $port-type -m tcp --dport $port -j DROP 
     /sbin/iptables -A INPUT -p $port-type -m udp --dport $port -j DROP 
    else 
    /sbin/iptables -A INPUT -p $port-type -m $port-type --dport $port -j DROP 
fi 
+3

사용 http://www.shellcheck.net/ – Cyrus

답변

1

다른 방식으로 변경되었습니다.

#!/bin/bash 

echo "Would you like to block or unblock? [ACCEPT] or [DROP] :" 
    read choice 
echo "What kind of port? [udp] or [tcp] or [both] :" 
    read porttype 
echo "What port? [number] :" 
    read port 

    if [[ $porttype == "both" ]]; then 
     /sbin/iptables -A INPUT -p tcp -m tcp --dport $port -j $choice 
     /sbin/iptables -A INPUT -p udp -m udp --dport $port -j $choice 
    else 
    /sbin/iptables -A INPUT -p $porttype -m $porttype --dport $port -j $choice 
fi 
1

당신이 당신의 들여 쓰기 체계적 경우, 당신은 문제를 발견합니다 :

if [ $(choice) == "u" ]; then 
    echo $PTYPET 
    read port-type 
    echo $PTEXTT 
    read port 
    if [ $(ptype-text) == "both" ]; then 
     /sbin/iptables -A INPUT -p $port-type -m tcp --dport $port -j ACCEPT 
     /sbin/iptables -A INPUT -p $port-type -m udp --dport $port -j ACCEPT 
    else # Indent next two lines 
     /sbin/iptables -A INPUT -p $port-type -m $port-type --dport $port -j ACCEPT 
    fi 
else 
    echo $PTYPET 
    read port-type 
    echo $PTEXTT 
    read port 
    if [ $(ptype-text) == "both" ]; then 
     /sbin/iptables -A INPUT -p $port-type -m tcp --dport $port -j DROP 
     /sbin/iptables -A INPUT -p $port-type -m udp --dport $port -j DROP 
    else # Indent the next two lines 
     /sbin/iptables -A INPUT -p $port-type -m $port-type --dport $port -j DROP 
    fi 
# And now it is clear that this fi was missing! 
fi 

고전적으로는, port-type 유효한 변수 이름이 아닙니다; 밑줄은 괜찮을 것입니다. $(ptype-text)을 사용하면 ptype-text 명령을 실행하고 약간의 놀라운 결과를 캡처 할 수 있습니다. $(choice)와 유사합니다. 변수 참조의 경우 중괄호 (${choice})를 사용합니다. 코드에서 약간의 눈에 띄는 반복이 있습니다. 두 쌍의 '에코/읽기'는 if/else 구조 외부에 있어야합니다.