一个shell小案例(创建日期目录)
今天看到一个shell题目,正好拿来练练手
需要在多个目录中(如:beijingshanghaitianjinguangzhou等等)创建子目录(以年份命名),然后进入子目录,新建目录并以当天的日期命名。
最终的效果是这样的:
china/guangdong/ china/guangdong/shenzhen/2010/1206 china/guangdong/shenzhen/2010/1207 china/guangdong/shenzhen/baoan/2010/1206 china/guangdong/shenzhen/baoan/2010/1207 china/guangdong/shenzhen/baoan/guangming/2010/1206 china/guangdong/shenzhen/baoan/guangming/2010/1207
我的实现:
#!/bin/bash read-p"PLEASEinputcountry:"cou read-p"PLEASEinputcity:"city read-p"PLEASEinputname:"name #echo"$cou,$city,$name" date=$(date+%Y) #echo$date date2=$(date+%m%d) #echo$date2 if[-d"./$cou/$city/$name/$date/$date2"];then echo"thedirisexit" else mkdir-p./$cou/$city/$name/$date/$date2 fi
效果:
[root@localhosthbshell]#test.sh
PLEASEinputcountry:china
PLEASEinputcity:hangzhou
PLEASEinputname:hb
#[root@localhosthbshell]#llchina/hangzhou/hb/2011/0321/
总计0