bash shell笔记(4)

shell脚本的编写

1、善用判断条件  例

test -e /jason && echo "exist" || echo "not exist"

2、另一个判断符号[]

中括号内的每个组件应用空格隔开,中括号内的常量用双引号或者单引号设置,例子

格式:   [ "&HOME" == "$MAIL" ] 

例2: read -p "please input (Y/N): " yn

[ "$yn" == "Y" -o "$yn" == "y" ] && echo "ok,continue" && exit 0

[ "$yn" == "N" -o "$yn" == "n" ] && echo "oh,interrupt" && exit 0

echo "sorry,I dont know what is your choise" && exit 0

3、shell脚本的默认变量

格式如下:

/path/to/srcriptsname opt1 opt2 opt3 ........

 $0                                $1     $2     $3

例子:

#!bin/bash
echo "the script name is $0"
[ -n "$1" ] && echo "the 1st parameter is $1" || exit 0
[ -n "$2" ] && echo "the 1st parameter is $2" || exit 0
[ -n "$3" ] && echo "the 1st parameter is $3" || exit 0


结果:

jason@jason:~/scripts$ sh sh04.sh afa beta thta
the script name is sh04.sh
the 1st parameter is afa
the 1st parameter is beta
the 1st parameter is thta


猜你喜欢

转载自blog.csdn.net/stormjason/article/details/79914730