Jump to content
alfredclough

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"
else
    echo "offline"
fi

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.

Share this post


Link to post

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

Share this post


Link to post

Thank you!

10 minutes ago, deanishe said:

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

 

It's a solution I found that seemed to work reasonable quickly. If there's a better option I'm open to it.

 

Share this post


Link to post

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.

Share this post


Link to post

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
my_command
if [ $? -eq 0 ]; then

do

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.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...