1

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:#
Nathan K
  • 15
  • 6
  • Can you show us the code? – Arkadiusz Drabczyk Oct 05 '16 at 13:08
  • @ArkadiuszDrabczyk I have editted the post with the current code. – Nathan K Oct 05 '16 at 13:11
  • Why are you `appending (>>)` to the NULL object? – Inian Oct 05 '16 at 13:13
  • @123 I am not using read, I want it to print and then continue with the terminal input, At the minute its just echoing into the command line. – Nathan K Oct 05 '16 at 13:13
  • @Inian I don't want the ping to output to the terminal, Is that not the correct method? – Nathan K Oct 05 '16 at 13:14
  • @NathanK: What do you mean by `press Enter`? Do you want a newline to show up after the last `Waiting for client to come online`? – Arkadiusz Drabczyk Oct 05 '16 at 13:15
  • @NathanK, just the re-direction is sufficient (`>`), it is not an issue though. – Inian Oct 05 '16 at 13:15
  • Please look at http://stackoverflow.com/a/5297780/6138942 This might very well be what you are looking for. – Gijs de Jong Oct 05 '16 at 13:15
  • @ArkadiuszDrabczyk Yeah, I want the Waiting for client to come online to show and then immediately go back to the status of me being able to add another command in to terminal. If I was to echo "ls" and press Enter it will run the command, This is not what I want, If that makes sense? – Nathan K Oct 05 '16 at 13:18
  • @Inian Thank you, I have changed that part. – Nathan K Oct 05 '16 at 13:20
  • Just print a newline before exiting? – 123 Oct 05 '16 at 13:23
  • @123 The example I gave the exit is in the else statement so the exit has nothing to do with this, I want it so it prints to the terminal and that's it... So print to the terminal and then if I was to type another command in the terminal the printed statement will be above where I am typing. – Nathan K Oct 05 '16 at 13:26
  • 1
    How are you running the code? I suspect you are running it in the background and the shell is already accepting a new command; the script is simply writing output to your terminal until it exits. – chepner Oct 05 '16 at 13:30
  • I suppose you want to put the script in background when you press Enter ? Also your code is a bit scary : you wipe the httpd.conf when the distant machine is up... – Goufalite Oct 05 '16 at 13:35
  • @Goufalite I am using Netkit so currently the httpd.conf is blank, I will amend to the httpd.conf as I progress further. I do want it to run in the background but when I do sh script.sh & it still has the same issue. – Nathan K Oct 05 '16 at 13:36

2 Answers2

2

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Awesome! The issue still happens whilst we are waiting for the client to come up. It's because the while loop is running, I would like it to echo to terminal and then that's it, As if the script was running in the background. – Nathan K Oct 05 '16 at 13:34
  • When you run the script add a `&` after... So lets say the script is called `script.sh`, then just type this in terminal: `script.sh &`.. That will run it in the background. – Fadi Oct 05 '16 at 14:49
-3

Use echo "^M".

The character inside quoted is formed by pressing 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.

To check, we use "od -c" (as shown below), whether the character has correctly formed or not.

With proper character, we get "\r \n" in od -c output. And in improper one, we get ^ 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.

Cheers , Gaurav

User9102d82
  • 1,172
  • 9
  • 19
  • This hasn't solved the problem. I now get "Waiting for client to come online ^M" and the same issue still :( – Nathan K Oct 05 '16 at 13:28
  • Just need to understand how and where the enter is to be supplied.. I think I am not clear on that. Can you help in That? – User9102d82 Oct 05 '16 at 13:31
  • Hi Nathan, please check my solution again, i have added some comments on how to check whether character is proper and a small change in your scirpt. Thanks. – User9102d82 Oct 05 '16 at 13:46