aix...day of the week command


 
Thread Tools Search this Thread
Operating Systems AIX aix...day of the week command
# 1  
Old 01-20-2011
aix...day of the week command

I have a ksh script that runs fine on Linux box but not on an AIX box. I am trying to determine what awk to use, using an "if" statement that says 'if today is Monday, do this else run the other awk statement'. here is my code...
Code:
if [ $(date '+%w') -eq 1 ]
  then
      awk -vD=$(date -d '3 days ago' '+%Y%m%d1700') '{if(substr($0,35,12)<=D) print $0}' $ExceptionFile > ${ExceptionFile%.txt}.tmp
  else
      awk -vD=$(date -d '1 days ago' '+%Y%m%d1700') '{if(substr($0,35,12)<=D) print $0}' $ExceptionFile > ${ExceptionFile%.txt}.tmp
fi

i dont think the date command supports this function on AIX...any ideas?

Last edited by Scott; 01-20-2011 at 03:11 PM.. Reason: Please use code tags
# 2  
Old 01-20-2011
There is no 'base' (-d) option in AIX date. So, since you're talking about 72 hours, cheat by using a temporary TZ.

Code:
if [ $(date '+%w') -eq 1 ];then
        awk -vD=$(TZ=72 date '+%Y%m%d1700') '{d=substr($0,35,12);if(d<=D) print d "<=" D}' ex-file
else
        awk -vD=$(TZ=24 date '+%Y%m%d1700') '{d=substr($0,35,12);if(d<=D) print d "<=" D}' ex-file 
fi

(make edits to what I presented here... it was done so I could see what was happening)
# 3  
Old 01-21-2011
I've seen many posts about date manipulation. The shell and AIX seem to lack the functionality of other environments. I have this code. I didn't write it. But it seems to work. Perhaps it might help people on AIX...



Code:
ms:/home/unxsa/functions>today -m 3
18Jan2011
ms:/home/unxsa/functions>cat today
# Name: today
#
# Purpose: Prints today's date, plus or minus the given number of days.
#          Default is today's date.
#
# Date: 2006/06/08
#
# Version: 1.0
#
# Required Parameters:
# -------------------------------------------------------------------------
#
# Optional Parameters:
#   -f [format]                 - format of date to use
#   -p #                                - number of days to add
#   -m #                                - number of days to subtract
#   -c c                                - character to use in the date string
#   -s                                  - use short year string
# -------------------------------------------------------------------------
#
# Change History:
#
# Date          Name            Comments
# _________________________________________________________________________
# 2006/06/08    selbyp                  Created.



###################################################################################
# function
###################################################################################
function today {

   USAGE=":f:p:m:c:sh"

   FORMAT=0
   PLUS=0
   MINUS=0
   SEPCHAR=""
   SHORT=0

   if [[ $# -gt 0 ]]
   then
      while getopts "${USAGE}" OPT
      do
         case ${OPT} in
            f) FORMAT=${OPTARG};;
            p) PLUS=${OPTARG};;
            m) MINUS=${OPTARG};;
            c) SEPCHAR=${OPTARG};;
            s) SHORT=1;;
            h) cat <<EOF
        Syntax:  today { [-p n] OR [-m n] } [-f FORMAT] [-c CHAR] [-s] [-h]
        Parameters:
                [-p n]          -       Print today's date, plus n days
                [-m n]          -       Print today's date, minus n days
                [-f FMT]        -       Print date using specified format (DMY/MDY/YMD), default looks like Jun092006
                [-c c]          -       Separate dates using the character c
                [-s]            -       Use a short year in the date
                [-h]            -       View this help
        Default:  Print today's date in the default format.
EOF
            return 0
            ;;
            ?) echo "today(): unrecognized option: ${OPT}"
            return 1;;
         esac
      done

      shift $((${OPTIND} - 1))
      ARGUMENTS=$@

      if [[ ${PLUS} -ne 0 && ${MINUS} -ne 0 ]]
      then
         RETCODE=1
         if [[ ${DEBUG} -ne 0 ]]
         then
            print "today(): Attempted to add and subtract simultaneously."
         fi
         return $RETCODE
      fi

      if [[ ${FORMAT} -ne 0 && ${FORMAT} != "YMD" && ${FORMAT} != "MDY" && ${FORMAT} != "DMY" ]]
      then
         RETCODE=2
         if [[ ${DEBUG} -ne 0 ]]
         then
            print "today(): Unrecognized format: ${FORMAT}"
         fi
         return $RETCODE
      fi

      if [[ ${#SEPCHAR} -gt 1 ]]
      then
         RETCODE=3
         if [[ ${DEBUG} -ne 0 ]]
         then
            print "today(): Bad separating character: ${SEPCHAR}"
         fi
         return $RETCODE
      fi
   fi

   eval `date "+DAY=%d; MONTH=%m; YEAR=%Y`

        if [[ $PLUS -gt 0 ]]; then
                DAY=$((DAY + PLUS))
        elif [[ $MINUS -gt 0 ]]; then
                DAY=$((DAY - MINUS))
        fi

        if (( DAY <= 0 )); then
                while (( DAY <= 0 )); do
                        MONTH=$((MONTH - 1))
                        if (( MONTH == 0 )); then
                                YEAR=$(( YEAR - 1))
                                MONTH=12
                        fi
                        set -A DAYS $(cal $MONTH $YEAR)
                        XDAY=${DAYS[$((${#DAYS[*]} - 1 ))]}
                        DAY=$(( XDAY + DAY ))
                done
        else
                set -A DAYS $(cal $MONTH $YEAR)
                MAXDAY=${DAYS[$((${#DAYS[*]} - 1 ))]}
                if (( DAY > MAXDAY )); then
                        while (( DAY > MAXDAY )); do
                                DAY=$(( DAY - MAXDAY ))
                                MONTH=$((MONTH + 1))
                                if (( MONTH > 12 )); then
                                        YEAR=$(( YEAR + 1 ))
                                        MONTH=1
                                fi
                                set -A DAYS $(cal $MONTH $YEAR)
                                MAXDAY=${DAYS[$((${#DAYS[*]} - 1 ))]}
                        done
                fi
        fi

        if [[ $SHORT -ne 0 ]]; then
                if [[ ${#YEAR} -eq 4 ]]; then
                        OLD_YEAR=${YEAR}
                        typeset -Z2 YEAR
                        YEAR=$( echo ${OLD_YEAR} | cut -c 3-4 )
                elif [[ ${#YEAR} -ne 2 ]]; then
                        if [[ ! -z $DEBUG ]] && [[ $DEBUG -gt 0 ]]; then
                                echo "today(): what kind of year is this: ${YEAR}??"
                                return 1
                        fi
                fi
        fi

        typeset -Z2 F_DAY F_MONTH
        F_DAY=$DAY
        F_MONTH=$MONTH

   if [[ $FORMAT == "YMD" ]]; then
      print "${YEAR}${SEPCHAR}${F_MONTH}${SEPCHAR}${F_DAY}"
   elif [[ $FORMAT == "DMY" ]]; then
      print "${F_DAY}${SEPCHAR}${F_MONTH}${SEPCHAR}${YEAR}"
   elif [[ $FORMAT == "MDY" ]]; then
      print "${F_MONTH}${SEPCHAR}${F_DAY}${SEPCHAR}${YEAR}"
   else
      case $F_MONTH in
                  01) MONTH_STR=Jan;;
                  02) MONTH_STR=Feb;;
                  03) MONTH_STR=Mar;;
                  04) MONTH_STR=Apr;;
                  05) MONTH_STR=May;;
                  06) MONTH_STR=Jun;;
                  07) MONTH_STR=Jul;;
                  08) MONTH_STR=Aug;;
                  09) MONTH_STR=Sep;;
                  10) MONTH_STR=Oct;;
                  11) MONTH_STR=Nov;;
                  12) MONTH_STR=Dec;;
      esac
   print ${F_DAY}${MONTH_STR}${YEAR}
fi
}

# 4  
Old 01-28-2011
Date manipulation almost deserves a sticky on the main page of this forum. It seems like there is always a question about it.

Once I asked the head IBM AIX developer (Jay Krum--something) if they were ever going to integrate the epoch date into the date command like the various flavors of Linux have. His response was they don't want to know how Linux does it because they want to avoid "copying" any code from Linux. He then continued to say "don't hold your breath."
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the week's day

Hi All, I have the below requirement , if i give the week number for ex 41 i need to get the date for Monday and thursday for this given week. my expected output is 13/10/2014 (Monday's date) and 16/10/2014 (Thursday's date) I am using GNU LINUX . Pls help me with your thoughts. Thanks in... (7 Replies)
Discussion started by: mohanalakshmi
7 Replies

2. UNIX for Dummies Questions & Answers

Sudoers for one day per week?

I have been volunteered by my boss to be the sysadmin for our production redhat server. He asked me to tighten the security to avoid mishaps like "rm -f *" that occured not long ago. Right now, we have 53 users sudo-ing into the machine and it is an audit nightmare. I am wondering if it... (15 Replies)
Discussion started by: alan
15 Replies

3. HP-UX

Find Day of Week

In HP-UX the date command does not have the "-d" switch like some other *nixes do. I'm working a simple script to tell me, given the day, month and year what day of the week that falls on. Assuming valid day, month and year input (I'd perform quality checks on the input separately, but not... (5 Replies)
Discussion started by: rwuerth
5 Replies

4. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

5. UNIX for Dummies Questions & Answers

Day of the week from a string

Hi All, I need to know how to derive the day of the week by passing the value in following format: Feb 28 2010 The output I'm expecting is Sunday or Sun. I know, I can use the following code to get the day of the week. date +%a But I want to pass the value as a string. Please help... (11 Replies)
Discussion started by: shash
11 Replies

6. Shell Programming and Scripting

Get day of week from cal

Hi all, I am trying to get dow from cal using below script #! /bin/bash YEAR=`echo $1 | cut -c 1-4` MONTH=`echo $1 | cut -c 5-6` DAY=`echo $1 | cut -c 7-8` for i in 1 2 3 4 5 6 7 do dayofweek=`cal $MONTH $YEAR | awk '$i == $DAY {printf("%s","$i")}'` echo $dayofweek... (4 Replies)
Discussion started by: bzylg
4 Replies

7. HP-UX

Get Day of Week from date

Hi All, I have date in string format 'YYYY-MM-DD'. I want to know day of the week for this date. Example. For '2005-08-21' my script should return '0' or Sunday For '2005-08-22' it should return '1' or Monday I want piece of code for HP-UX korn shell. Appreciate reply on this. (5 Replies)
Discussion started by: vpapaiya
5 Replies

8. UNIX for Dummies Questions & Answers

Changing First Day Of The Week?

Hi All, Our system is running on Solaris 8 and we are using US locale. By default the First Day Of Week is Sunday, is it possible for us to change it to Monday? I have googled it but found very little of use. THanks in advance. (2 Replies)
Discussion started by: fowlerleftfoot
2 Replies

9. Shell Programming and Scripting

Yesterday's Day of week

I need o get yesterday's day of week but im not exactly sure. the actual name is what i want. I can do it with numbers but im not sure with words. (3 Replies)
Discussion started by: rcunn87
3 Replies

10. UNIX for Dummies Questions & Answers

Calculating the day of the week

Hi all, I would like to calculate the day of the week using a supplied date. i.e. 20011012 = Day 5. Any ideas? Many thanks, ligs (4 Replies)
Discussion started by: ligs
4 Replies
Login or Register to Ask a Question