繁体   English   中英

Bash脚本回显,无需按Enter

[英]Bash script echo and no need to press ENTER

编辑:当前代码

#!/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即可继续使用终端。

例如...如果我回显“我们正在工作”,光标将移至末尾并等待我按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

使用回显“ ^ M”。

引号内的字符是通过按crtl + v + m形成的。

它应该工作。

该角色将完成输入工作。

更新:

我只是在外壳上尝试过。 我能够得到额外的输入。 您可能需要在此之后放置另一个echo命令。

为了进行检查,我们使用“ od -c”(如下所示)来确定字符的格式是否正确。

使用适当的字符,我们在od -c输出中得到“ \\ r \\ n”。 在不合适的情况下,我们得到^M。

正确:

Wed Oct 05|19:03:45|app/ga> echo "^M"|od -c
0000000  \r  \n
0000002

不当:

Wed Oct 05|19:03:50|app/ga> echo "^M"|od -c
0000000   ^   M  \n
0000003
Wed Oct 05|19:03:57|app/ga>

验证后,请尝试使用此行,而不是脚本中的行。

echo -ne "\nWaiting for client to come online" ; echo "^M"

通过按键盘上的ctrl + v + m可以正确形成“ ^ M”字符。

干杯,高拉夫

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM