将上述行删除,并在条件为真后用其他行替换它。

huangapple go评论59阅读模式
英文:

Delete the above line and replace it with other line after the condition become true

问题

read -p "请输入IP地址: " ip
echo -e "\e[5m正在进行ping测试...\e[25m"

ping -c 1 $ip &> /dev/null
if [ $? -eq 0 ]
then
    sleep 3
    echo -e "\e[1;93m$ip\e[1;39m 可达"
else
    echo -e "\e[1;91m$ip\e[1;39m 不可达"
fi
英文:

Please go through the below coding and clarify.

read -p "Enter the ip address: " ip
echo -e "\e[5mpinging...\e[25m"

ping -c 1 $ip &> /dev/null>1
        if [ $? -eq 0 ]
        then
        echo -e "\e[1;93m$ip\e[1;39m is reachable"
        else
        echo -e "\e[1;91m$ip\e[1;39m is not reachable"
        fi

When the above coding is executes, I need the program to wait 3 seconds, then say "IP_ADDR is reachable" also I need to replace the text "pinging..." to "completed" upon execution.

current output:when the above script is executed

needed output:should replace the word "pinging..." with "completed"

答案1

得分: 1

我建议使用\r转义序列,它表示将光标移到行的第一个字符。

这样将覆盖pinging...并且它不会显示。

以下是使用这种策略的脚本。

#!/bin/bash
read -p "输入IP地址: " ip
echo -ne "\e[5mpinging...\e[25m\r" # 注意这里的 \r
sleep 1
ping -c 1 $ip &> /dev/null >1
if [ $? -eq 0 ]
then
  echo -e "\e[1;93m$ip\e[1;39m 可达"
else
  echo -e "\e[1;91m$ip\e[1;39m 不可达"
fi

希望对你有所帮助。

英文:

I would suggest using \r escape sequence that means, cursor go back to first character of the line.

So this will overwrite pinging... and it will not appear.

Here is how looks the script with this stategy.


#!/bin/bash
read -p "Enter the ip address: " ip
echo -ne "\e[5mpinging...\e[25m\r" # Note here \r
sleep 1
ping -c 1 $ip &> /dev/null>1
if [ $? -eq 0 ]
then
  echo -e "\e[1;93m$ip\e[1;39m is reachable"
else
  echo -e "\e[1;91m$ip\e[1;39m is not reachable"
fi

Hope it is helpful.

huangapple
  • 本文由 发表于 2023年7月17日 16:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定