Message Area
Casually read the BBS message area using an easy to use interface. Messages are categorized exactly like they are on the BBS. You may post new messages or reply to existing messages! You are not logged in. Login here for full access privileges. |
Previous Message | Next Message | Back to PRISM Chat Echo <-- <--- | Return to Home Page |
|
||||||
From | To | Subject | Date/Time | |||
mark lewis | Jeff Smith | Date Variable in Linux |
January 11, 2017 11:21 AM * |
|||
On 2017 Jan 10 23:56:16, you wrote to Janis: JS> I know that I can specify a DOY (Day of Year) variable in a bash file JS> as "DOY=$(date +%j)". But, I would only want the variable to express JS> the last two digits of DOY. So... 010 would be expressed as 10 and 165 JS> would be expressed as 65, etc. JS> I looking at grabbing a zipped file with a filename of filename.znn JS> 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/bashr... 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/b... -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) |
||||||
|
Previous Message | Next Message | Back to PRISM Chat Echo <-- <--- | Return to Home Page |
Execution Time: 0.0651 seconds If you experience any problems with this website or need help, contact the webmaster. VADV-PHP Copyright © 2002-2024 Steve Winn, Aspect Technologies. All Rights Reserved. Virtual Advanced Copyright © 1995-1997 Roland De Graaf. |