Bash脚本回显,无需按ENTER键

时间:2016-10-05 13:01:02

标签: linux bash

编辑:当前代码

#!/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:#

2 个答案:

答案 0 :(得分:2)

我怀疑您的问题与使用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

答案 1 :(得分:-3)

使用echo&#34; ^ M&#34;。

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

它应该有用。

这个角色将完成输入。

更新

我刚试了一下我的shell。我能够获得额外的进入。你可能需要在那之后添加另一个echo命令。

要检查,我们使用&#34; od -c&#34; (如下所示),角色是否正确形成。

如果有正确的性格,我们会得到&#34; \ r \ n&#34;在od -c输出。在不正确的一个,我们得到^ 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"

哪里&#34; ^ M&#34;通过在键盘上按ctrl + v + m正确形成字符。

干杯, 拉夫

相关问题