当前位置:编程学习 > 网站相关 >>

shell编程的一些常用方法

下面是我在工作中编写的shelldemo,里面包含了常用方法,包括当前事件的获得和格式化,字符串的分割,文件和目录的判断,字符串前缀的对比等等,我还会继续扩展这个demo,希望对大家有用:)
 
[cpp] 
currenttime=`date '+%Y-%m-%d %H:%M:%S'`  
var=`date`  
echo `date`  
echo $var  
#得到当前事件  
echo "$currenttime"    
  
  
#!/bin/bash  
echo `pwd`  
  
#字符串的分割  
# var=`echo "aaa,bbb,ccc"|awk -F ',' '{print $1}' `  
# echo $var   
  
  
#遍历“config”目录  
for config in `ls config`; do  
       echo "$config"       
  
        #字符串根据“#”来分隔,print打印,计数器从0开始  
        # var=`echo "$config"|awk -F '#' '{print $1}' `  
        # echo $var   
  
        #! [[ ...,这里的求反必须有空格,否则报错   
        # no_*为前缀,不可以加引号  
        if ! [[ "$config" = _* ]]; then  
             echo "----------"  
             continue;  
        fi  
  
  
       for con in `ls config/$config`; do  
        echo "config/$config/$con"  
  
        # if [[ -d $con ]]; then  
        #这里必须是"config/$config/$con",否则会出现文件能识别,但文件夹不能识别的错误  
        #递归拷贝所有的config文件到主目录中  
        # --判断con是文件和还是目录--  
        if [[ -d "config/$config/$con" ]]; then  
            echo "$con is a directory ..."  
            cp -r "config/$config/$con" .  
        elif [[ -f "config/$config/$con" ]]; then  
            echo "$con is a file ..."  
            cp "config/$config/$con" .  
        else  
            echo "$con is not valid ..."  
            exit 1  
        fi  
       done  
done  
 
输出结果如下:
[cpp]  
victormatoimac:shelltest ericyang$ sh ./autotest.sh   
2013年 1月 8日 星期二 16时35分51秒 CST  
2013年 1月 8日 星期二 16时35分51秒 CST  
2013-01-08 16:35:51  
/Users/ericyang/shelltest  
_con1  
config/_con1/a.txt  
a.txt is a file ...  
config/_con1/c1  
c1 is a directory ...  
con2  
----------  
 
附上demo中config的文件目录:
 
 
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,