Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, im rather new to linux, trying to get my head around scripting. i have made a shell script. i am trying to make a script that take in a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2012
    Posts
    2

    shell script help to a newbie


    Hi,

    im rather new to linux, trying to get my head around scripting.

    i have made a shell script. i am trying to make a script that take in a date (DATE_TO_RUN) and find what day of week the date is.

    what i need help with is to get the $DAYOFWEEK variable assigned with the value from printf("%d\n", (NF == 7 || FNR!=3) ? i-1 : i+(6-NF))

    How to do this? Thanx, all help appriciated.

    Code:
    #!/bin/bash
    
    DATE_TO_RUN='2012-11-05'
    
    eval $(echo "${DATE_TO_RUN}" | nawk -F- '{printf("year=%s month=%s day=%s\n", $1, $2, $3)}')
    echo "year->[${year}] month->[${month}] day->[${day}]"
    
    DAYOFWEEK=-1
    
    cal "${month}" "${year}" | nawk -v day="${day}" '
      FNR > 2 {
        for(i=1; i <= NF; i++)
          if ( $i == day) {
    
            printf("%d\n", (NF == 7 || FNR!=3) ? i-1 : i+(6-NF))
            exit
          }
      }
    '
    
    echo "The date: " $DATE_TO_RUN " is day of week " $DAYOFWEEK
    By running this script i get this output:
    Code:
    year->[2012] month->[11] day->[05]
    1
    The date:  2012-11-05  is day of week  -1
    Want to have been:
    Code:
    year->[2012] month->[11] day->[05]
    1
    The date:  2012-11-05  is day of week  1

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    238
    hi,

    couldn't you rely on `date'?
    Code:
    d2run="2012-11-05"
    date -d "$d2run" +%u
    1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •