#####the fist line should be the following#####
#!/bin/bash
#####return value#####
exit n
#####Usage of read command,读键盘的输入#####
read [-pt] variable
-p: 后面可以接提示信息
-t: 后面可以接等待的秒数,这样脚本不用一直等下去
例子:
read -p "Input your name: " name;
echo $name;
#3秒内输入名字
read -p "input your name: " -t 3 name;
#####date command#####
#2 years/days/minutes/seconds ago
date1=`date --date='2 years/days/minutes/seconds ago' +%Y%m%d`
echo $date1
#2 years/days/minutes/seconds next
date2=`date --date='-2 /days/minutes/seconds ago' +%Y%m%d`
或者date2=`date --date='-2 /days/minutes/seconds next' +%Y%m%d`
echo $date2
#####数值运算#####
total=$((运算表达式))
e.g. total=$((2+3))
#####判断表达式#####
判断表达式有test指令和[]。
##test指令
当我们要检测系统上某个文件是否存在或者属性时,可以使用test指令。用&&判断真值,||判断假值。例如:
test -e ch04.sh && echo "exist" || echo "no exist"
意思就是当文件ch04.sh存在时打印"exist",不存在时打印"no exist"。
#判断字符串长度是否为0
test [-zn] string
-z: 当string为空时,返回true
-n: 当string不为空时,返回true
test -z "" && echo "empty" || echo "not empty"
#empty
test -z " " && echo "empty" || echo "not empty"
#not empty
test -n "" && echo "not empty" || echo "empty"
#empty
test -n " " && echo "not empty" || echo "empty"
#not empty
#判断字符串是否相等,注意=和!=两边要留一个空格
test str1 = str2
test str1 != str2
#逻辑与,或,非
-a(and)
-o(or)
!
##判断符号[]
注意点:
1. 在中括号内的各个元素必须有空格隔开
2. 在中括号内的变量,最好用双引号括起来
3. 在中括号内的常数,最好用单引号或者双引号括起来
例子:
yn='y'
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "equal" || echo "not equal"
#equal