您的位置:首页 > 运维架构 > Shell

LinuxC/C++编程基础(13) shell脚本简单实例

2012-12-02 18:09 1296 查看
一.createDir.sh的编写,如下:

#! /bin/bash

isDirectory(){

DIR_NAME=$1

if [ ! -d $DIR_NAME ];then

return 1

else

return 0

fi

}

for DIR in "$@";do

if isDirectory "$DIR"

then :

else

echo "$DIR does not exist.Creating it now..."

mkdir $DIR > directory.log 2>&1

if [ $? -ne 0 ];then

echo "Cannot create directory $DIR"

exit 1

fi

fi

done

说明:(1)这个脚本可以一次创建多个目录,通过命令行参数传入各目录名,脚本先逐个测试各目录是否存在,如果目录不存在,首先打印信息然后试着创建该目录。

(2)该脚本包含了一些shell最基本的语法,如下:

a)控制语句:if/else/fi

b)测试语句:[

c)循环语句:for/do/done

d)函数使用:isDirectory

转载请注明出处:山水间博客:/article/2317646.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: