gpt4 book ai didi

linux - Bash 脚本回显,无需按 ENTER

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:41:36 24 4
gpt4 key购买 nike

编辑:当前代码

#!/bin/bash
check=0

while [ $check -ne 1 ];
do
ping -c 1 192.168.150.1 >> /dev/null
if [ $? -eq 0 ];then
echo -ne "\nClient is up! We will start the web server"
touch /etc/apache2/httpd.conf
echo "ServerName 127.0.0.1" > /etc/apache2/httpd.conf
/etc/init.d/apache2 start
if [ $? -eq 0 ];then
exit 0
fi
else
echo -ne "\nWaiting for client to come online"
sleep 5;
fi
done

我目前正在学习一些 Bash 脚本,我希望能够将它回显到终端,而无需按 enter 键继续使用终端。

例如...如果我回显“We are working”,光标将移至末尾并等待我按 Enter 键,然后返回到我可以键入新命令的位置。

server:#
Waiting for client to come online
Waiting for client to come online
Waiting for client to come online
Waiting for client to come online
Waiting for client to come online
Waiting for client to come online
Waiting for client to come online
Waiting for client to come online
Waiting for client to come online <---- Press Enter here
server:#

最佳答案

我怀疑您的问题与使用 echo -ne '\n...' 而不是简单地 echo '...' 有关。但是,您的整个代码可以大大简化:

#!/bin/bash

while :; do
until ping -c 1 192.168.150.1 > /dev/null; do
echo "Waiting for client to come online"
done
echo "Client is up! We will start the web server"
echo "ServerName 127.0.0.1" > /etc/apache2/httpd.conf
/etc/init.d/apache2 start && break
done

关于linux - Bash 脚本回显,无需按 ENTER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39874700/

24 4 0