英文:
Setting the date of a file to that of the previous month - KornShell
问题
我试图设置一个KornShell脚本,以便在每个月的第一天将一个文件FTP传输。
当文件创建时,它应该读取上个月的日期 - 如果作业在4月1日运行,文件将是文件名_MAR2023。这是否包括在脚本正文中,还是需要在开始时设置一个变量?
提前感谢您!
我尝试过使用"DATE=date +%d%m%y
"作为一个变量,但这对我来说似乎只会获取当前日期并将其应用为数值。
英文:
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.
答案1
得分: 0
似乎printf
的%T
指令可以解析日期时间字符串,就像GNU的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
名称
printf - 写入格式化输出
用法
printf [选项] 格式 [字符串 ...]
...
%T 将字符串视为日期/时间字符串并进行格式化。 T 可以前面带有 (dformat),其中 dformat 是日期格式,如 date 命令定义的。
这是:
$ print -v .sh.version
版本 AJM 93u+ 2012-08-01
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论