1

Im trying to set up a KornShell script so a file is FTP'd on the first of each month.

When the file is created it should read the date of the previous month - If the job was ran on April 1st the file would be filename_MAR2023. Is this something that is included in the body of the script or would a variable need to be set up at the start?

Thank you in advance!

I have tried DATE=`date +%d%m%y` as a variable but this seems to me like it would just take the current date and apply that in a numerical value.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
STAKD
  • 15
  • 4
  • `date +%Y-%b --date='-1 month'` gives `2003-Feb`. Getting an all-caps abbreviation will be your challenge (`tr` or `sed` can do it). ALSO, I highly recommend using ` date +%Y-%m --date='-1 month'` which gives `2023-02` as these dates naturally sort. Once you have more that one year's data, any time you do an `ls /myDated/Dir` all the same months will group together over multiple years, which will make programming any processing more complicated. Good luck! – shellter Mar 03 '23 at 18:34
  • (This solution works in any shell, as long as the `date` command supports the `--date='....'` option. Some don't or you are required to install something like gnu-date.) – shellter Mar 03 '23 at 18:37
  • When you can't install a GNU like `date`, you can also think about the `find` option `-ctime` or `-mtime`, which might be useful in your case. – Walter A Mar 03 '23 at 22:38

1 Answers1

0

It seems that printf's %T directive can parse a datetime string much like GNU's date -d

fname="my_filename"
typeset -u date=$(printf '%(%b%Y)T' 'last month')
newname="${fname}_${date}"
print -v newname
# => my_filename_APR2023
$ printf --man
NAME
  printf - write formatted output

SYNOPSIS
  printf [ options ] format [string ...]
...
    %T    Treat string as a date/time string and format it. The T can be preceded by (dformat),
          where dformat is a date format as defined by the date command.

This is:

$ print -v .sh.version
Version AJM 93u+ 2012-08-01
glenn jackman
  • 238,783
  • 38
  • 220
  • 352