编辑:当前代码

#!/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继续使用终端。
例如。。。如果我回显“我们正在工作”,光标将转到末尾,等待我按回车键,然后返回到可以键入新命令的位置。
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/

10-12 22:28