Shell控制流结构编程之测试目录创建结果
2013-01-20 13:42
253 查看
#接受一个参数,并用之创建目录,然后参数被传入命令行,重设给变量DIRECTORY,最后测试变量是否为空。
#!/bin/sh
DIRECTORY=$1
if [ "$DIRECTORY" = "" ]
then
echo "Usage:`basename $0` directory to create" >&2
exit 1
fi
if [ -d $DIRECTORY ]
then :
else
echo "The directory not exist"
echo -n "Create it now? [y/n]:"
read ANS
if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]
then
echo "creating......"
mkdir $DIRECTORY >/dev/null 2>&1
if [ $? != 0 ];
then
echo "Errors creating the directory $DIRECTORY" >&2
exit 1
fi
else :
fi
fi
这里使用最简单的嵌套的if else语句。
if 条件
then
命令1
else
命令2
fi
记住,如果没有命令1或2,需在then或else后面加上空格+:,否则shell程序将报错。
#!/bin/sh
DIRECTORY=$1
if [ "$DIRECTORY" = "" ]
then
echo "Usage:`basename $0` directory to create" >&2
exit 1
fi
if [ -d $DIRECTORY ]
then :
else
echo "The directory not exist"
echo -n "Create it now? [y/n]:"
read ANS
if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]
then
echo "creating......"
mkdir $DIRECTORY >/dev/null 2>&1
if [ $? != 0 ];
then
echo "Errors creating the directory $DIRECTORY" >&2
exit 1
fi
else :
fi
fi
这里使用最简单的嵌套的if else语句。
if 条件
then
命令1
else
命令2
fi
记住,如果没有命令1或2,需在then或else后面加上空格+:,否则shell程序将报错。
相关文章推荐
- 【安装oracle——准备工作】配置内核参数+添加用户组及用户+为oracle用户配置shell限制+创建目录结构+设置oracle环境
- 第一部分 Shell基础编程——第四章 控制流结构
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课
- linux之shell编程if-then-else结构
- java-web应用-创建,发布,目录结构,WEB-INF,META-INF,web.xml,build.xml
- lucene3.0+版本中文分词测试+搜索结果+创建索引测试
- Eclipse创建 分离单同等 的目录结构
- 令用EclipseJ2EE创建的Dynamic Web project目录结构与用MyEclipse创建的Web project一样
- shell编程之如何遍历进入当前目录下的所有子目录
- 使用shell编程,创建10000个目录,结构类似于9/9/99
- Shell编程面试题2_批量创建随机名称的文件
- 一个创建工程目录的shell脚本
- 亿能测试大讲堂 - YY在线课程[ 测试人员需要掌握的Shell脚本编程 ]
- linux shell编程指南第十八章------控制流结构1
- 文件编程:创建目录mkdir()函数
- linux——Shell的控制结构(附shell编写代码和运行结果)
- shell 编程语法总结(I/O 逻辑控制结构)
- mac系统下用zend framework 自动创建目录结构
- Node.js 文件夹目录结构创建实例代码
- Node.js 文件夹目录结构创建实例代码