• Date Variable in Linux

    From Jeff Smith@1:2320/100 to Janis on Wed Jan 11 00:02:16 2017
    Hello Janis,

    I know that I can specify a DOY (Day of Year) variable in a bash
    file as "DOY=$(date +%j)". But, I would only want the variable to
    express the last two digits of DOY. So... 010 would be expressed as
    10 and 165 would be expressed as 65, etc.

    I looking at grabbing a zipped file with a filename of filename.znn
    where "nn" is the DOY minus the first digit.

    Any thoughts? If I am making any sense. <g>


    Jeff
    --- SBBSecho 3.00-Linux
    # Origin: The OuijaBoard Too - Anoka, MN (1:282/1032)
    * Origin: LiveWire BBS - Synchronet - LiveWireBBS.com (1:2320/100)
  • From Janis Kracht@1:2320/100 to Jeff Smith on Wed Jan 11 01:23:50 2017
    Hi Jeff,

    I know that I can specify a DOY (Day of Year) variable in a bash
    file as "DOY=$(date +%j)". But, I would only want the variable to
    express the last two digits of DOY. So... 010 would be expressed as
    10 and 165 would be expressed as 65, etc.

    I looking at grabbing a zipped file with a filename of filename.znn
    where "nn" is the DOY minus the first digit.

    Any thoughts? If I am making any sense. <g>

    Yes, I think I understand...

    Here's how I get the two-digits you are talking about when I need them in my scripts:

    #set variables
    num=`date +%j --date=Friday`
    pick=`expr substr $num 2 3`
    year=`date +%Y`
    echo $pick
    13
    echo $num
    013
    echo $year
    2017

    So I think it's the `expr substr $num 2 3` you want to use for those two digits. My variable 'list' above is looking for info for Friday's nodelist date, that's why I'm looking for --date=Friday :)

    Take care,
    Janis

    --- BBBS/Li6 v4.10 Toy-3
    # Origin: Prism bbs (1:261/38)
    * Origin: LiveWire BBS - Synchronet - LiveWireBBS.com (1:2320/100)
  • From Sean Dennis@1:2320/100 to Jeff Smith on Wed Jan 11 02:24:06 2017
    Jeff Smith wrote to Janis:
    I know that I can specify a DOY (Day of Year) variable in a bash
    file as "DOY=$(date +%j)". But, I would only want the variable to
    express the last two digits of DOY. So... 010 would be expressed as
    10 and 165 would be expressed as 65, etc.

    I looking at grabbing a zipped file with a filename of filename.znn
    where "nn" is the DOY minus the first digit.

    Any thoughts? If I am making any sense. <g>

    I think I have what you need. For the "othernet" that I run, I wrote a
    script that does the automated creation and hatching of the nodelists and infopacks every Friday morning.

    Here you go:

    ## Figure out correct day and copy proper
    ## archive to hatch directory
    DOY=$(date +%j)

    ## This line will get the last two digits of the current
    ## day of the year since this script always runs on Friday
    ## right after MakeNL does.
    DOY1=${DOY:${#DOY}<3?0:-2}

    Hope this helps!

    --Sean


    --- MBSE BBS v1.0.6.10 (GNU/Linux-i386)
    # Origin: Outpost BBS * Limestone, TN, USA (1:18/200)
    * Origin: LiveWire BBS - Synchronet - LiveWireBBS.com (1:2320/100)
  • From Sean Dennis@1:2320/100 to Mark Lewis on Wed Jan 11 17:02:18 2017
    Hello mark,

    11 Jan 17 12:24 at you wrote to me:

    DOY1=${DOY:${#DOY}<3?0:-2}

    ha! that looks really familiar! i just got the same thing in some searching and posted it in my previous message :lol: :) :) :)

    The joys of store n' forward mail. :D

    Yes, I spent a good bit of time figuring that out. I'm glad I did. Nice to see your example too.

    Later,
    Sean

    --- GoldED+/LNX 1.1.5-b20160322
    # Origin: Outpost BBS * Limestone, TN, USA (1:18/200)
    * Origin: LiveWire BBS - Synchronet - LiveWireBBS.com (1:2320/100)
  • From Mark Lewis@1:2320/100 to Jeff Smith on Wed Jan 11 11:26:36 2017

    On 2017 Jan 10 23:56:16, you wrote to Janis:

    I know that I can specify a DOY (Day of Year) variable in a bash file
    as "DOY=$(date +%j)". But, I would only want the variable to express
    the last two digits of DOY. So... 010 would be expressed as 10 and 165 would be expressed as 65, etc.

    I looking at grabbing a zipped file with a filename of filename.znn
    where "nn" is the DOY minus the first digit.

    i'm guessing that you're looking for a specific file of that naming format? i went another way and simply extract (after making sure there's only one that fits the name format) foo.z*...

    but see how this fits... we're going to use the "${parameter:offset:length}" substring method with a little twist...


    ==== Begin "doytest" ====
    #!/bin/bash

    DOY=$(date +%j)
    DOYS=$(printf "%02d" ${DOY:${#DOY}<2?0:-2})
    printf "DOY=\"$DOY\" DOYS=\"$DOYS\"\n"

    DOY=1
    DOYS=$(printf "%02d" ${DOY:${#DOY}<2?0:-2})
    printf "DOY=\"$DOY\" DOYS=\"$DOYS\"\n"

    DOY=01
    DOYS=$(printf "%02d" ${DOY:${#DOY}<2?0:-2})
    printf "DOY=\"$DOY\" DOYS=\"$DOYS\"\n"

    DOY=100
    DOYS=$(printf "%02d" ${DOY:${#DOY}<2?0:-2})
    printf "DOY=\"$DOY\" DOYS=\"$DOYS\"\n"

    ==== End "doytest" ====


    since we want only the last two characters, if DOY has a length less than 2, ${DOY: -2} would expand to the empty string. in that situation we want all of DOY so we use this tricky little beast...

    ${DOY:${#DOY}<2?0:-2}

    what we're doing here is replacing the offset value with another parameter expansion formula... that formula is

    ${#DOY}<2?0:

    this uses the "?:" ternary "if" operator that can be used in Shell Arithmetic... we can do this because the offset value is an arithmetic expression... so if DOY contains less than two characters, "${#DOY}<2", we want all of them... "the "?0:" is the check if the result is zero length... now that we've got the last up to two characters of DOY, we use printf to pad leading zeros if the result has only one character...


    sure "$(date +%j)" returns three characters but the reason i added the rest is because you might find a use for this in other things where you may put data in manually... i did that in the above test script to see what happens with one, two and three character data strings... i can see a situation where you might want to add or subtract 1 or maybe 7 to the DOY result if you wanted a file up to a week newer or older... in fact, this is a pretty good start to the old DOS DAYNBR tool used in fidonet for so long...


    ===== snip =====

    Please refer to the Shell Parameter Expansion in the reference manual[1]:

    ${parameter:offset}
    ${parameter:offset:length}

    Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions (see Shell Arithmetic). This is referred to as Substring Expansion.

    If offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter. If length evaluates to a number less than zero, and parameter is not '@' and not an indexed or associative array, it is interpreted as an offset from the end of the value of parameter rather than a number of characters, and the expansion is the characters between the two offsets. If parameter is '@', the result is length positional parameters beginning at offset. If parameter is an indexed array name subscripted by '@' or '*', the result is the length members of the array beginning with ${parameter[offset]}. A negative offset is taken relative to one greater than the maximum index of the specified array. Substring expansion applied to an associative array produces undefined results.

    Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the ':-' expansion. Substring indexing is zero-based unless the positional parameters are used, in which case the indexing starts at 1 by default. If offset is 0, and the positional parameters are used, $@ is prefixed to the list.

    ===== snip =====

    [1] http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion



    full disclosure:
    i got the tricky looking parameter expansion formula from this page... i've also copied some of the relevent text as well as trying to word the explanation in my own way and make it easier to understand... http://stackoverflow.com/questions/19858600/bash-accessing-last-x-characters-of -string


    )\/(ark

    Always Mount a Scratch Monkey
    Do you manage your own servers? If you are not running an IDS/IPS yer doin' it wrong...
    ... Although the exit polls say otherwise.
    ---
    # Origin: (1:3634/12.73)
    * Origin: LiveWire BBS - Synchronet - LiveWireBBS.com (1:2320/100)
  • From Mark Lewis@1:2320/100 to Sean Dennis on Wed Jan 11 12:29:08 2017

    On 2017 Jan 11 02:19:06, you wrote to Jeff Smith:

    DOY1=${DOY:${#DOY}<3?0:-2}

    ha! that looks really familiar! i just got the same thing in some searching and posted it in my previous message :lol: :) :) :)

    )\/(ark

    Always Mount a Scratch Monkey
    Do you manage your own servers? If you are not running an IDS/IPS yer doin' it wrong...
    ... It really bothers me when people cut me o...
    ---
    # Origin: (1:3634/12.73)
    * Origin: LiveWire BBS - Synchronet - LiveWireBBS.com (1:2320/100)
  • From Jeff Smith@1:2320/100 to Janis Kracht on Wed Jan 11 00:57:20 2017
    Hello Janis,


    Yes, I think I understand...

    Here's how I get the two-digits you are talking about when I need them in my scripts:

    [...]

    num=`date +%j --date=Friday`
    pick=`expr substr $num 2 3`

    Yup. I used the above minus the "--date=Friday" and use

    num=`date +%j`
    DOY=`expr substr $num 2 3`

    Then todays (After Midnight <g>) DOY is expressed as 11 instead of 011.

    Thanks alot.

    Jeff
    --- SBBSecho 3.00-Linux
    # Origin: The OuijaBoard Too - Anoka, MN (1:282/1032)
    * Origin: LiveWire BBS - Synchronet - LiveWireBBS.com (1:2320/100)