如何在 bash 脚本中将日期名称转换为小写?
How to convert day name to lowercase in bash script?
我通常使用以下 bash 脚本将文件重命名为星期几(例如,星期一、星期二、星期三、星期四、星期五、星期六和星期日):
#get date
export LANG=id_ID
export TZ=Asia/Jakarta
DAY=$(date --date='0 days' '+%A')
TODAY=$(date --date='0 days' '+%Y%m%d')
#get each page 1 till 9
PAGE=1
until [ $PAGE -gt 9 ]; do
mv "0$PAGE".jpg banjarmasinpost"$TODAY"-"$DAY"_"0$PAGE".jpg
let PAGE+=1
done
有没有办法将星期一、星期二、星期三、星期四、星期五、星期六和星期日等所有日期的名称都变成小写?
谢谢大家。
Bash 参数扩展来拯救! ${DAY,,}
将扩展为 $DAY
的 lower-cased 值。
(此外,shell 脚本中的变量名称应为小写——请参阅 this unix.stackexchange 问题。)
参见 bash
reference manual:
${parameter^pattern}
${parameter^^pattern}
${parameter,pattern}
${parameter,,pattern}
Case modification. This expansion modifies the case of alphabetic characters in parameter. The pattern is expanded to produce a pattern just as in pathname expansion. Each character in the expanded value of parameter is tested against pattern, and, if it matches the pattern, its case is converted. The pattern should not attempt to match more than one character. The ^
operator
converts lowercase letters matching pattern to uppercase; the ,
operator converts matching uppercase letters to lowercase.The ^^
and ,,
expansions convert each matched character in the expanded value; the ^
and ,
expansions match and convert only the first character in the expanded value. If pattern is omitted, it is treated like a ‘?’, which matches every character. [...]
所以如果$DAY
包含Wednesday
,可以写${DAY,,}
得到wednesday
:
$ DAY=$(date --date='0 days' '+%A')
$ echo $DAY
Wednesday
$ echo ${DAY,,}
wednesday
(在这种情况下您也可以使用 ${DAY,}
,因为您只需要担心一个字母。)
我通常使用以下 bash 脚本将文件重命名为星期几(例如,星期一、星期二、星期三、星期四、星期五、星期六和星期日):
#get date
export LANG=id_ID
export TZ=Asia/Jakarta
DAY=$(date --date='0 days' '+%A')
TODAY=$(date --date='0 days' '+%Y%m%d')
#get each page 1 till 9
PAGE=1
until [ $PAGE -gt 9 ]; do
mv "0$PAGE".jpg banjarmasinpost"$TODAY"-"$DAY"_"0$PAGE".jpg
let PAGE+=1
done
有没有办法将星期一、星期二、星期三、星期四、星期五、星期六和星期日等所有日期的名称都变成小写? 谢谢大家。
Bash 参数扩展来拯救! ${DAY,,}
将扩展为 $DAY
的 lower-cased 值。
(此外,shell 脚本中的变量名称应为小写——请参阅 this unix.stackexchange 问题。)
参见 bash
reference manual:
${parameter^pattern}
${parameter^^pattern}
${parameter,pattern}
${parameter,,pattern}
Case modification. This expansion modifies the case of alphabetic characters in parameter. The pattern is expanded to produce a pattern just as in pathname expansion. Each character in the expanded value of parameter is tested against pattern, and, if it matches the pattern, its case is converted. The pattern should not attempt to match more than one character. The
^
operator converts lowercase letters matching pattern to uppercase; the,
operator converts matching uppercase letters to lowercase.The^^
and,,
expansions convert each matched character in the expanded value; the^
and,
expansions match and convert only the first character in the expanded value. If pattern is omitted, it is treated like a ‘?’, which matches every character. [...]
所以如果$DAY
包含Wednesday
,可以写${DAY,,}
得到wednesday
:
$ DAY=$(date --date='0 days' '+%A')
$ echo $DAY
Wednesday
$ echo ${DAY,,}
wednesday
(在这种情况下您也可以使用 ${DAY,}
,因为您只需要担心一个字母。)