bash if condition, then continue

Posted on 2014-08-21
Last Modified: 2014-08-21
Need a quick bash solution to the following problem...

A bash script is starting before the system has it's dhcp address and therefore it is failing.
It is further failing because it is taking a while for the system to get it's updated time from it's default of 1970.

I need to run a one time test at the very start of my script which will check to see what year it is.
If the year is not 20xx then it implies that full network connectivity has not been achieved yet so repeat the test.
Once the system year is 20xx, it means that the network has started and that the system has internet access so time to end this function and continue the script.

-script starts up
-checks to see what year it is on the system
-if the year is 1070, then loop and try trying again one second later
-Once the year is 20xx, this function dies for good and the script continues on to it's main functions
Question by:projects
    LVL 68

    Expert Comment

    while :
          if [[ $(date "+%Y") -ge 2000 ]]
               sleep 1
    # Script continues here if year >= 2000

    Author Comment

    So, just put that at the very top of my script, even before all of my variables, just underneath

    That's what I did and this seems to work perfectly.

    I am assuming this covers all 20xx

    LVL 68

    Accepted Solution

    Yes, you can put it just below the "shebang" line, it doesn't require any preliminary setup.
    And yes, it covers all 20xx, but also all 21xx, 22xx, ..., 30xx, 31xx, 32xx, ... and so on. If this is not desired, please let me know,

    I wrote the first solution a bit "for demonstration purposes".
    Of course we can make it shorter:

    while [[ $(date "+%Y") -lt 2000 ]] ;
       sleep 1

    Behind the scenes this contains the same logic, but I think it looks a bit more "elegant".

    Author Comment

    So long as you tested the second one and it does the same thing, perfect! :)

    Thanks very much.
    LVL 68

    Expert Comment

    Yes, it's tested - well , not with the actual system date (a thing I don't like to mess around with), but with a freehand variable.

    Author Comment

    Works like a charm, thanks very much.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Ldapsearch all expiring passwords in OID 1 42
    korn shell example requested 7 35
    PERL - Find newest folder 12 87
    exchange 2007 3 44
    This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
    Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now