Bash - multiple test [[expressions]] in linux -


i asked question yesterday trying make sure date formatted correctly. more targeted i've tried write test myself , it's not acting have expected to. (i'm new linux, happens fair amount)

i need date inputted yyyymmdd. think start of if statment should checking [[ check if $tdate 0 string or if date not yyyymmdd ]].

when run code date there $lastcob (yesterday, last close of business), when input date (or don't change date), "invalid date. please enter correct format." response, meaning test doesn't believe format correct, though it's entered yyyymmdd want be.

tdate=$(lastcob) tdateokay=0  until [[ $tdateokay -eq 1 ]] ;         echo "$tdate"         echo "$tdateokay"         read -p "please enter date search.  use format: date (yyyymmdd): " -e -i  "$tdate" tdate         if [[ -z "$tdate" || "$(date --date=$tdate + '%y%m%d' 2>&1 )" != $tdate ]] ;                 echo "invalid date. please enter date in correct format."         elif [[ $tdate -gt $(today) || $tdate -eq $(today) ]] ;                 echo "date must in past.  please try again."         else                 tdateokay=1         fi done 

the first if statment should test format. elif statment should test making sure date in past, , not future, or today. if passes both tests, tdateokay should turn 1 0 , program should move on date input future data search. let me know if need add clarity.

i think have problem in line:

if [[ -z "$tdate" || "$(date --date=$tdate + '%y%m%d' 2>&1 )" != $tdate ]] ; 

it works me with

if [[ -z "$tdate" || "$(date --date=$tdate '+%y%m%d' 2>&1 )" != $tdate ]] ; 

that is, put + inside date format.

for future, recommend use 2>&1 when sure code fine. otherwise more complicated debug.

by way, stated answer deleted, if [[ || ]] not best posix way. in this comment recommended use if [ ] || [ ].


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -