Unexpected `then' bash script

前端 未结 4 507
执念已碎
执念已碎 2020-12-11 12:53

How do I fix this error? I can\'t see anything wrong with my syntax.

ipcheck() {
  echolog \"[INFO] Enabling IP Forwarding...\"
  sudo echo 1 > /proc/sys/         


        
4条回答
  •  执念已碎
    2020-12-11 13:10

    Slightly refactored (improved) version that is not bash dependant:

    #!/bin/sh
    
    ipcheck() {
        echolog "[INFO] Enabling IP Forwarding..."
        sudo echo 1 > /proc/sys/net/ipv4/ip_forward || {
            echolog "[CRITICAL] Error echoing to ip_forward file"
            exit 1
        }
    
        # Paranoia check :)
        status=$(cat /proc/sys/net/ipv4/ip_forward)
        [ "$status" = "1" ] || {
            echolog "[CRITICAL] Could not enable IP Forwarding!"
            exit 1  # 1 is error in scripts, 0 is success
        }
        echolog "[INFO] IP Forwarding successfully enabled!"
    }
    

    Preferably make an error() function, perhaps something like:

    # Call it like: error "[INFO] Could not ..."
    error() {
        echolog "$*"
        exit 1
    }
    

    Oh and yeah, as everyone else pointed out, don't forget the spaces :)

提交回复
热议问题