每月归档的Linux Bash

时间:2017-02-08 07:56:06

标签: linux bash shell

我需要帮助我的脚本在第一部分工作但是当添加更多它显示未知变量lastyear,month

#!/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行时,它正在工作

1 个答案:

答案 0 :(得分:2)

在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"