alfredclough Posted January 10, 2019 Posted January 10, 2019 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.
deanishe Posted January 10, 2019 Posted January 10, 2019 (edited) 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 January 10, 2019 by deanishe
alfredclough Posted January 10, 2019 Author Posted January 10, 2019 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.
deanishe Posted January 10, 2019 Posted January 10, 2019 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.
nikivi Posted January 10, 2019 Posted January 10, 2019 @alfredclough WiFi tools workflow I wrote maybe of interest to you. It can check if you are online as well as stop/start wifi or restart it.
alfredclough Posted January 10, 2019 Author Posted January 10, 2019 4 minutes ago, nikivi said: @alfredclough WiFi tools workflow I wrote maybe of interest to you. It can check if you are online as well as stop/start wifi or restart it. I will look at it--thank you
vitor Posted January 11, 2019 Posted January 11, 2019 Since your main issue seems to be solved, I’ll offer some script suggestions. Instead of 2>&1, use &>. Same thing, way less confusing. So instead of > /dev/null 2>&1 you do &> /dev/null. 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now