1.命名规则shell
- 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头
- 中间不能有空格,能够使用下划线(_)
- 不能使用标点符号。
- 不能使用bash里的关键字
2.定义变量:name=value
3.使用变量:$name
4.只读变量:readonly name
5.删除变量:uset name
6.变量类型数组
- 局部变量:当前shell有效
- 环境变量:全部的应用程序有效
name="hello" # 使用双引号拼接 greeting="hello, "$name" !" greeting_1="hello, $name} !" echo $greeting $greeting_1 # 使用单引号拼接 greeting_2='hello, '$name' !' greeting_3='hello, ${name} !' echo $greeting_2 $greeting_3
输出结果为:bash
hello, hello ! hello, hello} ! hello, hello ! hello, ${name} !
string="abcd" echo ${#string} #输出 4
string="hello world" echo ${string:1:4}#输出 ello # 字符串下标从0开始
string="hello world" echo `expr index $string e`#输出2
7.反引号和$()同样,里面的语句看成命令执行markdown
- $0:执行的文件名
- $1:为第一个参数
- $2:为第二个参数 以此类推
- $#:传递到脚本的参数个数
- $$:脚本运行的当前进程ID
- $!:后台运行的最后一个进程ID
- $?:回传码,显示最后命令的退出状态
- $@,$*:显示全部传递的参数
假设变量a=10,b=20函数
运算符 | 说明 | 举例 |
---|---|---|
+ | 加法 | expr $a + $b 结果为 30。 |
- | 减法 | expr $a - $b 结果为 -10。 |
* | 乘法 | expr $a \* $b 结果为 200。 |
/ | 除法 | expr $b / $a 结果为 2。 |
% | 取余 | expr $b % $a 结果为 0。 |
= | 赋值 | a=$b 将把变量 b 的值赋给 a。 |
== | 相等 | 用于比较两个数字,相同则返回 true。 [ $a == $b ] 返回 false。 |
!= | 不相等 | 用于比较两个数字,不相同则返回 true。 [ $a != $b ] 返回 true。 |
运算符 | 说明 | 举例 |
---|---|---|
-eq | 检测两个数是否相等,相等返回 true。 | [ $a -eq $b ] 返回 false。 |
-ne | 检测两个数是否不相等,不相等返回 true。 | [ $a -ne $b ] 返回 true。 |
-gt | 检测左边的数是否大于右边的,若是是,则返回 true。 | [ $a -gt $b ] 返回 false。 |
-lt | 检测左边的数是否小于右边的,若是是,则返回 true。 | [ $a -lt $b ] 返回 true。 |
-ge | 检测左边的数是否大于等于右边的,若是是,则返回 true。 | [ $a -ge $b ] 返回 false。 |
-le | 检测左边的数是否小于等于右边的,若是是,则返回 true。 | [ $a -le $b ] 返回 true。 |
运算符 | 说明 | 举例 |
---|---|---|
&& | 逻辑的 AND | [[ $a -lt 100 && $b -gt 100 ]] 返回 false |
|| | 逻辑的 OR | [[ $a -lt 100 || $b -gt 100 ]] 返回 true |
运算符 | 说明 | 举例 |
---|---|---|
! | 非运算,表达式为 true 则返回 false,不然返回 true。 | [ ! false ] 返回 true。 |
-o | 或运算,有一个表达式为 true 则返回 true。 | [ $a -lt 20 -o $b -gt 100 ] 返回 true。 |
-a | 与运算,两个表达式都为 true 才返回 true。 | [ $a -lt 20 -a $b -gt 100 ] 返回 false |
运算符 | 说明 | 举例 |
---|---|---|
= | 检测两个字符串是否相等,相等返回 true。 | [ $a = $b ] 返回 false。 |
!= | 检测两个字符串是否相等,不相等返回 true。 | [ $a != $b ] 返回 true。 |
-z | 检测字符串长度是否为0,为0返回 true。 | [ -z $a ] 返回 false。 |
-n | 检测字符串长度是否不为 0,不为 0 返回 true。 | [ -n "$a" ] 返回 true。 |
$ | 检测字符串是否为空,不为空返回 true。 | [ $a ] 返回 true |
操做符 | 说明 | 举例 |
---|---|---|
-b file | 检测文件是不是块设备文件,若是是,则返回 true。 | [ -b $file ] 返回 false。 |
-c file | 检测文件是不是字符设备文件,若是是,则返回 true。 | [ -c $file ] 返回 false。 |
-d file | 检测文件是不是目录,若是是,则返回 true。 | [ -d $file ] 返回 false。 |
-f file | 检测文件是不是普通文件(既不是目录,也不是设备文件),若是是,则返回 true。 | [ -f $file ] 返回 true。 |
-g file | 检测文件是否设置了 SGID 位,若是是,则返回 true。 | [ -g $file ] 返回 false。 |
-k file | 检测文件是否设置了粘着位(Sticky Bit),若是是,则返回 true。 | [ -k $file ] 返回 false。 |
-p file | 检测文件是不是有管道,若是是,则返回 true。 | [ -p $file ] 返回 false。 |
-u file | 检测文件是否设置了 SUID 位,若是是,则返回 true。 | [ -u $file ] 返回 false。 |
-r file | 检测文件是否可读,若是是,则返回 true。 | [ -r $file ] 返回 true。 |
-w file | 检测文件是否可写,若是是,则返回 true。 | [ -w $file ] 返回 true。 |
-x file | 检测文件是否可执行,若是是,则返回 true。 | [ -x $file ] 返回 true。 |
-s file | 检测文件是否为空(文件大小是否大于0),不为空返回 true。 | [ -s $file ] 返回 true。 |
-e file | 检测文件(包括目录)是否存在,若是是,则返回 true。 | [ -e $file ] 返回 true |
if condition then command1 command2 ... commandN fi
if condition then command1 command2 ... commandN else command fi
if condition1 then command1 elif condition2 then command2 else commandN fi
for循环通常格式为:测试
for var in item1 item2 ... itemN do command1 command2 ... commandN done
while condition do command done
until condition do command done
case 值 in 模式1) command1 command2 ... commandN ;; 模式2) command1 command2 ... commandN ;; esac
- break跳出全部循环
- continue跳出当前循环
- 标准输入 0
- 标准输出 1
- 标准错误输出 2