[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

Linux: Only if line begins with valid date

#!/bin/bash
NOW=$( date +"%s" )
while read MONTH DAY TIME DATA
do
    THEN=$( date -d "$MONTH $DAY $TIME" +"%s" )
        COUNT=$((COUNT+1))
done < $1
echo $COUNT

Open in new window

I want to count all the lines that begin with a valid date.  This code works if a line begins with a valid date but if it does not then it returns an error.
0
hankknight
Asked:
hankknight
1 Solution
 
woolmilkporcCommented:
You could check the returncode of the "date" command issued in line 5

#!/bin/bash
NOW=$( date +"%s" )
while read MONTH DAY TIME DATA
do
    THEN=$( date -d "$MONTH $DAY $TIME" +"%s" 2>/dev/null )
       [[ $? -ne 0 ]] && continue
        COUNT=$((COUNT+1))
done < $1
echo $COUNT

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now