[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

bash if condition, then continue

Posted on 2014-08-21
6
Medium Priority
?
181 Views
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
0
Comment
Question by:projects
  • 3
  • 3
6 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40277026
while :
   do
      if [[ $(date "+%Y") -ge 2000 ]]
          then
            break
         else
           sleep 1
      fi
   done
# Script continues here if year >= 2000
0
 

Author Comment

by:projects
ID: 40277132
So, just put that at the very top of my script, even before all of my variables, just underneath
#!/bin/bash

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

I am assuming this covers all 20xx

Thanks!
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 40277156
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 ]] ;
 do
   sleep 1
  done

Behind the scenes this contains the same logic, but I think it looks a bit more "elegant".
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

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

Thanks very much.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40277193
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.
0
 

Author Comment

by:projects
ID: 40277211
Works like a charm, thanks very much.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

831 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