简体   繁体   English

Bash脚本回显,无需按Enter

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

Edit : Current code 编辑:当前代码

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

I am currently learning some Bash scripting and I want to be able to echo it to the terminal and without having to press enter to then continue with the terminal. 我目前正在学习一些Bash脚本,我希望能够将其回显到终端,而不必按Enter即可继续使用终端。

For example... If I echo "We are working" the cursor will go to the end and wait for me to press Enter to then return me back to where I could type a new command. 例如...如果我回显“我们正在工作”,光标将移至末尾并等待我按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:#

I suspect your issue is related to using echo -ne '\\n...' instead of simply echo '...' . 我怀疑您的问题与使用echo -ne '\\n...'而不是简单地echo '...' However, your whole code can be simplified greatly: 但是,您的整个代码可以大大简化:

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

Use echo "^M". 使用回显“ ^ M”。

The character inside quoted is formed by pressing crtl+v+m. 引号内的字符是通过按crtl + v + m形成的。

It should work. 它应该工作。

This character will do the job of enter. 该角色将完成输入工作。

Update: 更新:

I just tried it on my shell. 我只是在外壳上尝试过。 I am able to get the extra enter. 我能够得到额外的输入。 you may need to put another echo command after that. 您可能需要在此之后放置另一个echo命令。

To check, we use "od -c" (as shown below), whether the character has correctly formed or not. 为了进行检查,我们使用“ od -c”(如下所示)来确定字符的格式是否正确。

With proper character, we get "\\r \\n" in od -c output. 使用适当的字符,我们在od -c输出中得到“ \\ r \\ n”。 And in improper one, we get ^ M. 在不合适的情况下,我们得到^M。

Proper: 正确:

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

Improper: 不当:

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

After having verified that, please give another try with this line, instead of your line in the script. 验证后,请尝试使用此行,而不是脚本中的行。

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

Where "^M" character is properly formed by pressing ctrl+v+m on keyboard. 通过按键盘上的ctrl + v + m可以正确形成“ ^ M”字符。

Cheers , Gaurav 干杯,高拉夫

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

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