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
本文介绍了Shell脚本的基础知识,包括如何使用判断条件进行逻辑控制、理解不同的符号含义以及默认变量的格式。通过示例展示了如何结合这些元素来构建实用的脚本。
158

被折叠的 条评论
为什么被折叠?



