如果条件,Bash语法错误意外令牌与其他多个else

时间:2012-08-24 02:02:34

标签: bash openvpn

我在调试脚本时一直遇到错误,但我不知道它是什么原因。 我得到的错误是提前感谢您的帮助 第29行:意外令牌else' line 29:附近的语法错误,如果[[“$ 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 个答案:

答案 0 :(得分:3)

您没有使用相应的if构造(过早关闭它们)。这是你正在做的事情:

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

这就是它应该是什么:

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

换句话说,整个fi块应该只有一个if(这包括else子句)。请注意,您还应该使用elif而不是else if

您可以在if statements中详细了解Bash Guide for Beginners

更新:所以这里是你的块的修改,应该(希望)有效。请注意,我还简化了测试块,其中包含一些不必要的方括号。

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