Bash syntax errors unexpected token else with mult

2019-08-04 04:35发布

问题:

I keep getting errors when debugging my script and I'm at a loss for what it is the cause. Error I'm getting are below thanks in advance for your help line 29: syntax error near unexpected token else' line 29:else if [ ["$VERSION" = "5" ] && ["$ARCH" = "64" ] ];'

    #!/bin/bash
    if [ $USER != 'root' ]
    then
    echo "REQUIRES ROOT"
    exit 0
    fi

    ###Determine OS Version and Architecture
    read -p "What version of Centos are you Running? [5,6]?" VERSION
    echo $VERSION
    ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')


    IP=$(hostname -i)
    ###Installing OpenVpn Dependicies
    echo Installing OpenVpn Dependicies
    yum install gcc make wget rpm-build autoconf.noarch zlib-devel pam-devel        openssl-devel -y

    ###Download LZO RPM and Configure RPMForge Repo
    wget http://openvpn.net/release/lzo-1.08-4.rf.src.rpm
    echo Downloaded LZO RPM

    ##Download RPMForge Repo
    if [ ["$VERSION" = "5" ] && ["$ARCH" = "32" ] ];
    then
  wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
    fi

    else if [ ["$VERSION" = "5" ] && ["$ARCH" = "64" ] ];
    then
          wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
    fi

    else if [ ["$VERSION" = "6" ] && ["$ARCH" = "32" ] ];
    then
          wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-1.el6.rf.i686.rpm
    fi

    else if [ ["$VERSION" = "6" ] && ["$ARCH" = "64" ] ];
    then
          wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
    fi
    echo Downloaded Centos $VERSION $ARCH Rpmforge RPM

    ###Build the rpm packages
    rpmbuild --rebuild lzo-1.08-4.rf.src.rpm
    rpm -Uvh lzo-*.rpm
    rpm -Uvh rpmforge-release*
    echo rpm packages built

    ###Install OpenVPN
    yum install openvpn
    echo Openvpn installed

    ###Copy the easy-rsa folder to /etc/openvpn/
    cp -R /usr/share/doc/openvpn-2.2.2/easy-rsa/ /etc/openvpn/
    echo easy-rsa copied to /etc/openvpn/

    ###CentOS 6 patch for /etc/openvpn/easy-rsa/2.0/vars
    if [ ["$VERSION" = "6" ];
    then 
    sed -i bak -e s/export KEY_CONFIG=`$EASY_RSA/whichopensslcnf $EASY_RSA`/export KEY_CONFIG=/etc/openvpn/easy-rsa/2.0/openssl-1.0.0.cnf/g /etc/openvpn/easy-rsa/2.0/vars

    ###Now let’s create the certificate
    cd /etc/openvpn/easy-rsa/2.0
    chmod 755 *
    source ./vars
    ./vars
    ./clean-all

    ###Build CA
    ./build-ca
    echo certificate built

    ###Build key Server
    ./build-key-server server
    echo key Server built

    ###Build Diffie Hellman
    echo Build Diffie Hellman
    ./build-dh
    echo Diffie Hellman built

    ###Create OpenVPN server conf
    touch /etc/openvpn/server.conf
    echo OpenVPN Server conf created /etc/openvpn/server.conf

    echo "local 123.123.123.123 #- your_server_ip goes here
    port 1194 #- port
    proto udp #- protocol
    dev tun
    tun-mtu 1500
    tun-mtu-extra 32
    mssfix 1450
    ca /etc/openvpn/easy-rsa/2.0/keys/ca.crt
    cert /etc/openvpn/easy-rsa/2.0/keys/server.crt
    key /etc/openvpn/easy-rsa/2.0/keys/server.key
    dh /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem
    plugin /usr/share/openvpn/plugin/lib/openvpn-auth-pam.so /etc/pam.d/login
    client-cert-not-required
    username-as-common-name
    server 10.8.0.0 255.255.255.0
    push "redirect-gateway def1"
    push "dhcp-option DNS 8.8.8.8"
    push "dhcp-option DNS 8.8.4.4"
    keepalive 5 30
    comp-lzo
    persist-key
    persist-tun
    status 1194.log
    verb 3" > '/etc/openvpn/server.conf'
    sed -i bak -e s/123.123.123.123/$IP/g /etc/openvpn/server.conf
    echo default server copied to /etc/openvpn/server.conf please


    ###Save it and start OpenVPN
    service openvpn start

    ###enable IP forwarding
    sed -i bak -e s/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g /etc/sysctl.conf
    sysctl -p
    echo ip forwarding enabled

    ###Route Iptables
    echo iptables setup
    iptables -F
    read -p "What type of Virtualization are you using? [openvz,xen,kvm]?" VMVIRTTYPE
    echo $VMVIRTTYPE

    if [ "$VMVIRTTYPE" = "openvz" ];
    then
          iptables -t nat -A POSTROUTING -o venet0 -j SNAT --to-source $IP
    fi

    else [ "$VMVIRTTYPE" = "xen" ];
    then
          iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
    fi

    else [ "$VMVIRTTYPE" = "kvm" ];
    then
          iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
    fi
    service iptables save
    echo iptables configured and saved

    ###Create Server.opvn
    touch /etc/openvpn/server.ovpn
    echo "client
    dev tun
    proto udp
    remote 123.123.123.123 1194 # - Your server IP and OpenVPN Port
    resolv-retry infinite
    nobind
    tun-mtu 1500
    tun-mtu-extra 32
    mssfix 1450
    persist-key
    persist-tun
    ca ca.crt
    auth-user-pass
    comp-lzo
    verb 3" > '/etc/openvpn/server.ovpn'
    sed -i bak -e s/123.123.123.123/$IP/g /etc/openvpn/server.ovpn
    echo server.opvn saved to /etc/openvpn/server.ovpn
    exit 0
    fi

回答1:

You aren't using the appropriate if constructs (you're closing them too early). Here's what you're doing:

if {tests}
then
{code}
fi                     <== Problem #1
else if {more tests}   <== Problem #2
{code}
fi

And here's what it should be:

if {tests}
then
{code}
elif {more tests}
{code}
fi

In other words, there should only be one fi for a whole if block (this includes the else clauses). Note that you should also be using elif instead of else if.

You can read more about if statements in the Bash Guide for Beginners.

Update: So here's a modification of your block that should (hopefully) work. Note that I've also simplified your test blocks, which had some unnecessary square brackets in them.

if [[ "$VERSION" = "5" && "$ARCH" = "32" ]]
then
    wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
elif [[ "$VERSION" = "5" && "$ARCH" = "64" ]]
then
    wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
elif [[ "$VERSION" = "6" && "$ARCH" = "32" ]]
then
    wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-1.el6.rf.i686.rpm
elif [[ "$VERSION" = "6" && "$ARCH" = "64" ]]
then
    wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
fi


标签: bash openvpn