Skip to content

Commit e8c3474

Browse files
committed
linux shell
linux shell
1 parent b78fdf6 commit e8c3474

8 files changed

Lines changed: 391 additions & 0 deletions

File tree

linux/Shell函数.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Shell函数
2+
```python
3+
function func {
4+
statements
5+
[return value]
6+
}
7+
8+
# 调用 Shell 函数时可以给它传递参数,也可以不传递。如果不传递参数,直接给出函数名字
9+
func
10+
# 如果传递参数,那么多个参数之间以空格分隔
11+
name param1 param2 param3
12+
```
13+
14+
```python
15+
#!/bin/bash
16+
17+
function getsum(){
18+
local sum=0
19+
for var in $*
20+
do
21+
((sum+=var))
22+
done
23+
return $sum
24+
}
25+
26+
getsum 1 2 3
27+
echo $?
28+
29+
```

linux/Shell数学计算.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Shell数学计算
2+
## 1.shell数学计算命令
3+
* (( )):用于整数运算,推荐使用
4+
* let:用于整数运算,与(())类似
5+
* $[]: 用于整数运算
6+
* expr: 可用于整数运算,不推荐
7+
* bc: linux下的计算器程序,可处理整数和小数,shell本身只支持整数运算,要计算小数得使用bc计算器
8+
* declare -i: 将变量定义为整数,然后进行数学运算的时候就不会被当做字符串
9+
10+
## 2.shell(())
11+
```python
12+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# ((a=1+3))
13+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# echo $a
14+
4
15+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# a=$((2*4))
16+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# echo $a
17+
8
18+
# 1表示真,0表示假
19+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# echo $((a==8))
20+
1
21+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# echo $((a+10))
22+
18
23+
```
24+
25+
## 3.let "表达式"
26+
```python
27+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# let "a=1+1"
28+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# echo $a
29+
2
30+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# let b=3*4
31+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# echo $b
32+
12
33+
```
34+
35+
## 4.declare
36+
```python
37+
# 除了将a,b定义为整数,还必须将c定义为整数,如果不这样做,在执行c=$a+$b时,Shell依然会将a、b视为字符串。
38+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# declare -i a b c
39+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# a=11
40+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# b=12
41+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# c=$a+$b
42+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# echo $c
43+
23
44+
45+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# d=$a+$b
46+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# echo $d
47+
11+12
48+
```

linux/casein语句.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## case in语句
2+
```python
3+
case expression in
4+
pattern1)
5+
...
6+
;;
7+
pattern2)
8+
...
9+
;;
10+
pattern3)
11+
...
12+
;;
13+
*)
14+
statementn
15+
esac
16+
```
17+
18+
```python
19+
#!/bin/bash
20+
21+
printf "输入一个整数:"
22+
read num
23+
24+
case $num in
25+
1)
26+
echo "Monday"
27+
;;
28+
2)
29+
echo "Tuesday"
30+
;;
31+
3)
32+
echo "Wednesday"
33+
;;
34+
4)
35+
echo "Thursday"
36+
;;
37+
5)
38+
echo "Friday"
39+
;;
40+
6)
41+
echo "Saturday"
42+
;;
43+
7)
44+
echo "Sunday"
45+
;;
46+
*)
47+
echo "error"
48+
esac
49+
50+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
51+
输入一个整数:1
52+
Monday
53+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
54+
输入一个整数:2
55+
Tuesday
56+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
57+
输入一个整数:9
58+
error
59+
```

linux/for循环.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## for循环
2+
```python
3+
for(( 初始化语句; 判断条件; 自增或自减 ))
4+
do
5+
...
6+
done
7+
```
8+
9+
```python
10+
#!/bin/bash
11+
12+
sum=0
13+
for ((i=0;i<=100;i++))
14+
do
15+
((sum+=i))
16+
done
17+
18+
echo $sum
19+
```
20+
21+
22+
## python风格的for循环
23+
```python
24+
items=("item1" "item2" "item3")
25+
for s in ${items[*]}
26+
do
27+
echo "$s"
28+
done
29+
30+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
31+
item1
32+
item2
33+
item3
34+
```

linux/if语句.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# if语句
2+
3+
### if
4+
```python
5+
if condition
6+
then
7+
....
8+
fi
9+
```
10+
11+
```python
12+
# 示例
13+
#!/bin/bash
14+
15+
a=3
16+
if (($a>2))
17+
then
18+
echo "a大于2"
19+
fi
20+
21+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
22+
a大于2
23+
```
24+
25+
26+
### if else
27+
```python
28+
if condition
29+
then
30+
....
31+
else
32+
....
33+
fi
34+
```
35+
36+
```python
37+
#!/bin/bash
38+
39+
read -p "请输入a的值: " a
40+
if (($a>=10))
41+
then
42+
echo "a大于等于10"
43+
else
44+
echo "a小于10"
45+
fi
46+
47+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
48+
请输入a的值: 5
49+
a小于10
50+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
51+
请输入a的值: 12
52+
a大于等于10
53+
```
54+
55+
### if elif else
56+
```python
57+
if condition1
58+
then
59+
...
60+
elif condition2
61+
then
62+
...
63+
elif condition3
64+
then
65+
...
66+
else
67+
...
68+
fi
69+
```
70+
71+
```python
72+
#!/bin/bash
73+
74+
read -p "请输入a的值: " a
75+
if (($a>=10))
76+
then
77+
echo "a>=10"
78+
elif (($a>=5))
79+
then
80+
echo "5<=a<10"
81+
else
82+
echo "a<5"
83+
fi
84+
85+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
86+
请输入a的值: 3
87+
a<5
88+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
89+
请输入a的值: 5
90+
5<=a<10
91+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
92+
请输入a的值: 12
93+
a>=10
94+
```

linux/main.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
2. [Shell位置参数与特殊变量](Shell位置参数与特殊变量.md)
2121
3. [Shell字符串](Shell字符串.md)
2222
4. [Shell数组](Shell数组.md)
23+
5. [Shell数学计算](Shell数学计算.md)
24+
6. [Shell if else语句](if语句.md)
25+
7. [Shell test命令和\[\[\]\]](test命令和[[]].md)
26+
8. [Shell case in语句](casein语句.md)
27+
9. [Shell while语句](while语句.md)
28+
10. [Shell for循环](for循环.md)
29+
11. [Shell函数](Shell函数.md)
2330

2431

2532
## 常用命令

linux/test命令和[[]].md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## test命令
2+
test是shell内置命令,用来检测条件是否成立,通常和if一起用
3+
```python
4+
test 表达式
5+
# 或者简写,表达式和[] 之间必须有空格
6+
[ 表达式 ]
7+
```
8+
9+
### test文件检测
10+
* test -e filename: 判断文件是否存在
11+
* test -r filename: 判断文件是否存在,并且是否拥有读权限
12+
* -w filename: 判断文件是否存在,并且是否拥有写权限
13+
* -x filename: 判断文件是否存在,并且是否拥有执行权限
14+
* filename1 -nt filename2: 判断 filename1 的修改时间是否比 filename2 的新
15+
* filename -ot filename2: 判断 filename1 的修改时间是否比 filename2 的旧
16+
* filename1 -ef filename2: 判断 filename1 是否和 filename2 的 inode 号一致,可以理解为两个文件是否为同一个文件,这个判断用于判断硬链接是很好的方法
17+
18+
### test数值比较
19+
* num1 -eq num2:判断 num1 是否和 num2 相等
20+
* num1 -ne num2:判断 num1 是否和 num2 不相等
21+
* num1 -gt num2:判断 num1 是否大于 num2
22+
* num1 -lt num2:判断 num1 是否小于 num2
23+
* num1 -ge num2:判断 num1 是否大于等于 num2
24+
* num1 -le num2:判断 num1 是否小于等于 num2
25+
26+
### test字符串判断
27+
* -z str:判断字符串 str 是否为空
28+
* -n str:判断宇符串 str 是否为非空
29+
* str1 = str2,str1 == str2:=和==是等价的,都用来判断 str1 是否和 str2 相等
30+
* str1 != str2:判断 str1 是否和 str2 不相等
31+
32+
### 逻辑运算
33+
* expression1 -a expression:逻辑与,表达式 expression1 和 expression2 都成立,最终的结果才是成立的
34+
* expression1 -o expression2 : 逻辑或,表达式 expression1 和 expression2 有一个成立,最终的结果就成立
35+
* !expression:逻辑非,对 expression 进行取反。
36+
37+
38+
## [[]]用法
39+
[[]]是shell内置关键字,也是用来检测条件是否成立,可以认为是test的升级版
40+
```python
41+
# 表达式成立时退出状态为 0,否则为非 0 值
42+
[[ 表达式 ]]
43+
```
44+
45+
```python
46+
#!/bin/bash
47+
48+
read -p "请输入s1: " s1
49+
read -p "请输入s2: " s2
50+
51+
if [[ -z $s1 ]] || [[ -z $s2 ]]
52+
then
53+
echo "字符串不能为空"
54+
else
55+
echo "s1:$s1 s2:$s2"
56+
fi
57+
58+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
59+
请输入s1: aaa
60+
请输入s2:
61+
字符串不能为空
62+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
63+
请输入s1: aaa
64+
请输入s2: bbb
65+
s1:aaa s2:bbb
66+
```
67+
68+
#### [[]]支持正则表达式
69+
```python
70+
[[ str =~ regex ]]
71+
```
72+
73+
```python
74+
#!/bin/bash
75+
76+
read tel
77+
if [[ $tel =~ ^1[0-9]{10}$ ]]
78+
then
79+
echo "手机号:$tel"
80+
else
81+
echo "手机号格式不正确"
82+
fi
83+
84+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
85+
13000010002
86+
手机号:13000010002
87+
[root@iZ8vb6ughzbdqkfd58dowoZ bin]# shell_1.sh
88+
123456qwer
89+
手机号格式不正确
90+
```
91+
92+
**使用if条件时,用(())来判断数值,用[[]]来处理字符串或者文件**

0 commit comments

Comments
 (0)