簡體   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