天天看點

[原創]一個shell小案例

 今天看到一個shell題目,正好拿來練練手

需要在多個目錄中 (如:beijing  shanghai tianjin guangzhou 等等) 建立子目錄(以年份命名),然後進入子目錄,建立目錄并以當天的日期命名。

最終的效果是這樣的:

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 "PLEASE input country:" cou

        read -p "PLEASE input city:"  city

        read -p "PLEASE input name:"    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 "the dir is exit"

        else

                mkdir -p  ./$cou/$city/$name/$date/$date2

        fi

效果:

[root@localhost hbshell]# test.sh   

PLEASE input country:china

PLEASE input city:hangzhou

PLEASE input name:hb

#[root@localhost hbshell]# ll china/hangzhou/hb/2011/0321/

總計 0

      本文轉自hb_fukua  51CTO部落格,原文連結:http://blog.51cto.com/2804976/527032,如需轉載請自行聯系原作者