`

shell编程学习笔记二

阅读更多



流程控制


退出状态


有四种退出状态
最后命令退出状态$?
控制次序命令$$ ||
处理shell脚步本退出或shell退出及相就退出状态或函数返回码


退出当前进程
exit n
n为数字


流控制


if then else
格式为:
if 条件1
then 命令1
elif 条件2
then 命令2
else 命令3
fi




简单的if 语句
if条件
then 命令
fi


示例代码:测试1是否小于2
#!/bin/sh
#test
#测试1是否小于2
if [ "1" -lt "2" ]
then
#1小于2
echo "yes,1 is less than 2"
fi

修改文件权限
chmod u+x test
执行角本
[retacn@localhost tmp]$ ./test
yes,1 is less than 2


变量值测试


示例代码如下:用户键入return键后变量name是否包含信息
#!/bin/sh
#test2
#测试用户键入return键后name中是否包含信息
echo "Enter your name:"
read NAME
#
if [ "$NAME" = "" ]
then
echo "You must enter some information"
fi


运行角本:
[retacn@localhost tmp]$ ./test2
Enter your name:


You must enter some information




文件考贝输出检查
#!/bin/sh
#test3
#测试文件拷贝是否正常
if cp myfile myfile.bak
then
echo "copy success"
else
echo "`basename $0`:error could not copy the files" >&2
fi


执行结果:
[retacn@localhost tmp]$ ls
myfile1 test test2 test3
[retacn@localhost tmp]$ ./test3
cp: 无法 stat “myfile”: 没有那个文件或目录
test3:error could not copy the files

不输出系统错误信息
if cp myfile myfile.bak >/dev/null 2>
then ...




当前目录测试
示例代码如下:检查当前目录是否为根目录
#!/bin/sh
#test4
#检查当前目录是否是根目录
DIRECTORY=`pwd`
#
if [ "$DIRECTORY" != "/" ];then
#it is not the root directory,show the error message
echo "You need to be in the root directory not $DIRECTORY to run this
script" >&2
#退出返回错误值1
exit 1
fi


执行结果:
[retacn@localhost tmp]$ chmod u+x test4
[retacn@localhost tmp]$ ./test4
You need to be in the root directory not /home/retacn/tmp to run this
script


文件权限测试
示例代码如下:测试文件 test.txt是否被设置到LOGNAME
#!/bin/sh
#test5
#
LOGFILE=test.txt
echo $LOGFILE
if [ ! -w "#LOGFILE" ];then
echo "you can not write to $LOGFILE" >&2
fi


[retacn@localhost tmp]$ ./test5
test.txt
you can not write to test.txt




测试传递到角本中的参数


位置参数
$0 是角本的名称
$1 第一个参数
$2 第二个参数
以此类推
在$9之后的参数要用括号括起来,如${10}
$# 参数的个数
$* 和$@ 表示所有参数


示例代码如下
#!/bin/sh
#test5
#测试传入角本的参数个数
if [ $# -lt 3 ]; then
#小于三个参数
echo "usage: `basename $0`arg1 arg2 arg3" >&2
exit 1
fi
echo "arg1: $1"
echo "arg2: $2"
echo "arg3: $3"


执行结果
输入一个参数
[retacn@localhost tmp]$ ./test6 cup yue
usage: test6arg1 arg2 arg3


输入三个参数
[retacn@localhost tmp]$ ./test6 cup yue zhen hua
arg1: cup
arg2: yue
arg3: zhen


决定角本是否为交互模式
角本的运行
交互模式(终端模式)
非交互模式(cron或at)
示例代码如下:
#!/bin/sh
#test7
#检查角本是否是交互模式,返回1为交互
if [ -t ]; then
echo "we are interactive with terminal"
else
echo "we must be running from some background process probably
cron or at"
fi
执行结果:
[retacn@localhost tmp]$ ./test7
we are interactive with terminal






检测运行角本的用户
示例代码如下:
#!/bin/sh
#test8
#检测执行角本的用户
if [ "$LOGNAME" != "root" ]
then
echo "you need to root to run this script" >&2
exit 1
else
echo "yes indeed you are $LOGNAME proceed"
fi


执行结果
[retacn@localhost tmp]$ chmod u+x test8
[retacn@localhost tmp]$ ./test8
you need to root to run this script
[retacn@localhost tmp]$ su - root
口令:
[root@localhost ~]# cd /home/retacn/tmp/
[root@localhost tmp]# ./test8
yes indeed you are root proceed




将角本参数传入系统命令
#!/bin/sh
#test9
#传入系统命令
DIRECTORY=$1
if [ "`ls -A $DIRECTORY`" = "" ]; then
echo "$DIRECTORY is indeed empty"
else
echo "$DIRECTORY is not empty"
fi
执行结果:
[root@localhost tmp]# ls
myfile1 test2 test4 test6 test8 test.txt
test test3 test5 test7 test9
[root@localhost tmp]# ./test9 test.txt
test.txt is not empty


null:命令用法


示例代码
#!/bin/sh
#test9
#传入系统命令
DIRECTORY=$1
if [ "`ls -A $DIRECTORY`" = "" ]; then
echo "$DIRECTORY is indeed empty"
else :
fi




测试目录创建结果
示例代码如下:
#!/bin/sh
#test10
#测试目录创建目结果
DIRECTORY=$1
#检查字符串是否为空
if [ "$DIRECTORY" = "" ]
then
echo "usage:`basename $0` directory to create " >&2
fi
#如果目录已存在OB
if [ -d $DIRECTORY ]
then : #不做操作
else
#提示用户是否创建目录
echo "the directory does exist"
echo -n "create it now? [y..n]:"
read ANS
if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]
then
echo "creating now"
#创建目录
mkdir $DIRECTORY >/dev/null 2>&1
exit 1
#检测是否创建成功
if [ $? != 0]; then
echo "error creating the directory $DIRECTORY" >&2
exit 1
fi
else :
fi
fi




执行结果:


[root@localhost tmp]# chmod u+x test10
[root@localhost tmp]# ./test10
usage:test10 directory to create
[root@localhost tmp]# ./test10 temp
the directory does exist
create it now? [y..n]:y
creating now
[root@localhost tmp]# ls
myfile1 test test2 test4 test6 test8 test.txt
temp test10 test3 test5 test7 test9

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics