我需要第一部分脚本的帮助,但当添加更多时,它会显示去年、月的未知变量

#!/bin/bash

year=$(date +%Y)
lastyear=$(expr $year-1)
month=$(date +%m)
log=$lastyear$month

mkdir -p /root/temp/$(lastyear)
mkdir -p /root/temp/$(lastyear)/$(month)

mv -f *$log* $(archivefolder)/$(lastyear)/$(month)

错误提示是
./logdate.sh: line 8: lastyear: command not found
./logdate.sh: line 9: lastyear: command not found
./logdate.sh: line 9: month: command not found

但当我只包括到第6行的时候

最佳答案

不要将变量放在方括号中,此时shell正试图执行命令lastyear并将其放入变量中。以下应该是好的:

year=$(date +%Y)
lastyear=$(( year-1 ))
month=$(date +%m)
log="$lastyear$month"

mkdir -p "/root/temp/$lastyear"
mkdir -p "/root/temp/$lastyear/$month"

mv -f "*$log*" "$archivefolder/$lastyear/$month"

10-04 17:25