0

I have a little bash script here I'm trying to fix but I keep getting a syntax error stating "Unexpected end of file". It asks if i want to block or unblock and asks which type of port and then errors out.

Any help would be greatly appreciated.

#!/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
Zac1989
  • 53
  • 10

2 Answers2

1

Went about it a different way.

#!/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
Zac1989
  • 53
  • 10
1

If you're systematic in your indentation, you'll spot the problem:

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

Classically, port-type is not a valid variable name; underscore would be OK. Using $(ptype-text) is running a command ptype-text and capturing the output, which is a bit surprising; similarly with $(choice). For variable references, you use curly brackets: ${choice}. You have some rather noticeable repetitions in the code. The two pairs of 'echo/read' should be outside the if/else structure.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278