Menu

How to check if a string is empty

Help
2009-05-11
2022-03-14
  • Wolfgang Klein

    Wolfgang Klein - 2009-05-11

    Hello!

    How can I check if a program returns an empty string?

    This line:

    program="if [ -z `ping -c1 -w3 www.whatismyip.com | grep 'unknown host'`  ]; then ...; else... fi"

    doesn't work. What would be the correct way in this example?

     
    • Steven Friedrich

      I believe your really trying to determine if a host exists.

      If that is the case then just use ping and then check the return code. There is also a option to pinf to tell it you only want a return code, none of the ping responses or any other text output.

      In Bourne shell or bash, test the return code by looking at the shell variable $?.

      Be sure to check the man pages for sh and ping.

      Happy hunting.

       
    • Steven Friedrich

      OOpps, I should have said "you're", not "your".  And I only speak English, so I have no excuse.

      And I should have said that ping also allows you to specify a count, rather than ping indefinitely.

      That's probably why you program doesn't work the way you're expecting, becuase ping isn't exiting.

      try ping -c 5 and the option for return code only.  Check the man page for ping, I'm not sure count is -c.

       
    • Wolfgang Klein

      Wolfgang Klein - 2009-05-12

      Steven: I am already using "-c" and "-w" for pinging. The command above

            ping -c1 -w3 http://www.whatismyip.com | grep 'unknown host'

      does exactly what you mentioned and what I want: the "-c1" tells ping to send only one ping and "-w3" tells ping to wait no longer than three seconds for a response. And if the network is down or the host is not reachable, the result will contain the words "'unknown host", which are caught by grep.

      Now I need to know how to check for an empty string within a theme-file. The option "-z" for the test doesn't work. It works in a shell script, but not in a theme file.

       
    • Wolfgang Klein

      Wolfgang Klein - 2009-05-12

      One more thing:

      I don't know how the "http://" got in front of the machine I try to ping. I must have gotten there either by the forum software or by taking the wrong line when I did cut&paste. Here is the correct line that I am trying to get to work:

              if [ ! -z `ping -c1 -w3 www.whatismyip.com | grep unknown host` ]; then

      While composing this message, I don't see "http://" in front of the machine's name, and I hope it will not be there after I press "submit".

       
    • Wolfgang Klein

      Wolfgang Klein - 2009-05-12

      This is frustating! The "http://" got there again. It must be put there by the forum software. Anyway: it is not there in the line inside the theme file!

       
  • Troy  Artois

    Troy Artois - 2021-01-29

    Great Work . If you like to find your Ip address or location in one clickMy Locationfrom any place in the World.

     
  • mabelgoodrich

    mabelgoodrich - 2022-03-14
    Post awaiting moderation.
  • mabelgoodrich

    mabelgoodrich - 2022-03-14

    Thanks for sharing! Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. app para modelos webcam

     

    Last edit: mabelgoodrich 2022-03-14

Log in to post a comment.