Jump to content

Run Script Outputting an extra New Line How to remove?

Recommended Posts

I have a strange problem I am trying to solve. I must be missing something because this should be quite simple. I have a script to check and see if the computer is online. Here is the script:


echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1

if [ $? -eq 0 ]; then
    echo "online"
    echo "offline"

After this I have two filters. One filter checks if the value is online and the other checks if the value is offline. The problem is when I run the workflow it always stops at the filter and does not continue. I ran Alfred in debut and here is what I see:

[2019-01-10 07:38:49][action.script] Processing output of 'utility.filter' with arg 'online
[2019-01-10 07:38:49][action.script] Processing output of 'utility.filter' with arg 'online

It appears to me that there is an extra new line after "online" which would explain why the filter is not successfully completing. However, I cannot see any reason why an extra new line would be inserted? This seems like it should be something very simple so I must be missing something but I cannot see what it is.

Link to comment

That's caused by echo. It adds a newline to its output so the next echo is on the next line.


Use echo -n "online" to suppress the additional newline.


Out of interest, is there a particular reason you're using netcat instead of cURL or ping to tell if you're online?

Edited by deanishe
Link to comment

That's a good way to test if a website is online. ping is a better choice for checking your own connection, imo.

ping -c 1 google.com should succeed much more quickly than loading a webpage. If you're offline, I suppose the speed would be determined by the command's timeout.

Link to comment

Since your main issue seems to be solved, I’ll offer some script suggestions.

  1. Instead of 2>&1, use &>. Same thing, way less confusing. So instead of > /dev/null 2>&1 you do &> /dev/null.
  2. Instead of $? use if command. So instead of
if [ $? -eq 0 ]; then


if my_command; then

Check shellcheck (and possibly add it to your editor). It points out a lot of good shell scripting rules from your mistakes, and explains their reasoning.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...