【问题标题】:Pinging server in Bash script,在 Bash 脚本中 Ping 服务器,
【发布时间】:2020-04-13 01:42:25
【问题描述】:

谁能帮我弄清楚这个脚本有什么问题? 我是初学者

echo "whats the server address ?"
read server
ping -c 4 $server > /dev/null
result=$?
if [ $result = 0 ]
echo " ping succ"
else echo "ping unsuccessful"                                                                                                                                                fi

【问题讨论】:

  • 它怎么不适合你?如果您不告诉我们您有什么错误或问题,我们将无法解决您的问题。但对于初学者来说,if 语法是错误的。搜索“bash if else”。
  • 请描述错误是什么。

标签: bash if-statement syntax


【解决方案1】:

您的 if 语法错误。应该如下。注意then 和结束fi

if ...; then
    #code
else
    #code
fi

您可以通过在if 内部执行 ping 操作来缩短您的代码

if ping -c 4 $server > /dev/null; then 
    echo "ping succ"
else
    echo "ping unsuccessful"
fi

【讨论】:

    【解决方案2】:

    你只是错过了关闭 if 条件

    echo "whats the server address ?"
    read server
    ping -c 4 $server > /dev/null
    result=$?
    if [ $result = 0 ]
    echo "ping successful"
    else 
    echo "ping unsuccessful"
    fi 
    

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2020-10-17
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多