shell登录方式,shell算术运算,shell逻辑运算,shell条件测试,shell选择语句
shell编程在首行指明需执行的shell类型的路径
e.g #/bin/bash
1. shell登录方式
交互式登录:/etc/profile -- /etc/profile.d/*.sh -- /~/.bahsh_profile -- ~/.bashrc -- /etc/bashrc
非交互式登录: ~/.bashrc -- /etc/bashrc -- /etc/profile.d/*.sh
2. shell算术运算
(1) 运算符号:
+ add
- sub
* mul
/ div
% remainder
(2) 实现算术运算的四种方法
a. let varName=expression
let varName=2+3, let varName=2*3
b. varName=$[expression]
varName=$[5*6]
c. varName=$((expression))
varName=$((2+3))
d. varName=$(expr arg1 arg2 arg3 ...)
varName=$(expr 2 + 3)
3. 逻辑运算
&& logical and
|| logical or
! logical not
4. 条件测试
判断表达式是否为真,由测试语句完成
测试命令
test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]
测试比较符号
数值
-gt great than
-lt less than
-eq equal
-ne not equal
-ge great than or equal
-le less than or equal
e.g test 2 -eq 2, [ 4 -ge 3 ]
字符串
== strings are equal
> strings are great
< strings are less
!= strings are not equal
-z "STRING": 测试字符串是否为空,空位真
-n "STRING": 测试字符串是否不为空,不空为真
文件测试
test -e FILE 测试文件是否存在
5. 选择语句
if-then
if COMMAND
then
COMMAND
if-then-else
if COMMAND
then
COMMAND
if-then-elif-then-else
if COMMAND
then
COMMAND
elif
then
COMMAND
...
else
COMAND