Nathan K Nathan K - 1 month ago 9
Linux Question

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.

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.

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

Answer

I suspect your issue is related to using echo -ne '\n...' instead of simply 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
Comments