Shell基础

变量定义

定义变量时等号两边不能有空格,定义shell脚本时开头一定要以#!/bin/bash开头

1
2
3
4
5
#!/bin/bash
# 注意:"="号两边不能有空格
demo="hello world"
# 在终端输出变量demo,也就是hello world
echo $demo

数组

使用括号()来定义数组,且数组元素通过空格来分割

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
# 一对括号表示是数组,数组元素用“空格”符号分割开。
a=(1 2 3 4 5)
# 用${#数组名[@或*]} 可以得到数组长度
echo ${#a[@]}
echo ${#a[*]}
# 用${数组名[@或*]}可以得到整个数组内容
echo ${a[@]}
echo ${a[*]}
# 用${数组名[下标]} 可以得到指定下标的值,下标是从0开始
echo ${a[2]}
赋值

直接通过数组名[下标],就可以对其进行引用赋值,若下标不存在,则自动追加数组元素末尾

1
2
3
4
5
a=(1 2 3 4 5)
# 直接通过数组名[下标],就可以对其进行引用赋值
a[1]=100
# 若下标不存在,自动添加新一个数组元素
a[1000]=1000
删除

通过unset 数组[下标]即可清除数组相应下标的元素unset 数组清楚整个数组

1
2
3
4
5
a=(1 2 3 4 5)
# unset 数组[下标] 可以清除相应的元素
unset a[1]
# unset 数组[下标] 不带下标,清除整个数据。
unset a
截取

如下从下标0开始,截取长度为3,切片原先数组返回中间用空格分割的字符串,若将结果加上括号()即可产生新的数组

1
2
3
4
5
a=(1 2 3 4 5)
# 截取数组 ${数组名[@或*]:起始位置:长度},从下标0开始,截取长度为3,切片原先数组,返回是字符串,中间用“空格”分开
echo ${a[@]:0:3}
# 如果加上”()”,将得到切片数组,c 就是一个新数组
c=(${a[@]:1:4})
替换

该操作不会改变原先数组内容

1
2
3
4
5
a=(1 2 3 4 5)
# ${数组名[@或*]/查找字符/替换字符} 该操作不会改变原先数组内容
echo ${a[@]/3/100}
# 如果需要修改,重新赋值给变量a
a=(${a[@]/3/100})

条件

ifelse
1
2
3
4
5
6
7
8
9
10
11
# 打印终端命令行的所有参数
echo $*;
# 打印终端命令行的所有参数的个数
echo $#;
# 如果终端命令行的所有参数的个数小于3,就输出所有参数
if [ $# -lt 3 ]; then
echo $*;
else
echo $0;
echo "参数过多不在控制台显示";
fi
case
1
2
3
4
5
6
7
8
9
10
11
case $1 in
start | begin)
echo "start something"
;;
stop | end)
echo "stop something"
;;
*)
echo "Ignorant"
;;
esac
select
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
select ch in "begin" "end" "exit"; do
case $ch in
"begin")
echo "start something"
;;
"end")
echo "stop something"
;;
"exit")
echo "exit"
break;
;;
*)
echo "Ignorant"
;;
esac
done;

循环

while循环
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
i=10;
while [[ $i -gt 5 ]]; do
echo $i;
((i--));
done;

# 循环读取/etc/hosts文件内容
while read line; do
echo $line;
done < /etc/hosts;
for循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for((i=1;i<=10;i++)); do
echo $i;
done;

# seq 10 产生 1 2 3 。。。。10空格分隔字符串
for i in $(seq 10); do
echo $i;
done;

# 根据终端输入的文件名来检查当前目录该文件是否存在
for file in $*; do
if [ -f "$file" ]; then
echo "INFO: $file exists"
else
echo "ERROR: $file not exists"
fi
done;
until循环
1
2
3
4
5
a=10;
until [[ $a -lt 0 ]]; do
echo $a;
((a--));
done;

函数

所有函数在使用前必须定义,必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可,通过$?来获得函数返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function hello() {
echo "Hello!";
}
function hello_param() {
echo "Hello $1 !";
}
# 函数调用
hello;

# 函数传参调用
hello_param ben;

# 函数文件调用,调用(包含)函数文件,点和demo_call之间有个空格
. demo_call.sh;
# 调用函数
callFunction ben;

# 用unset functionname 取消载入
# unset callFunction;
# 因为已经取消载入,所以会出错
# callFunction ben;

# 参数读取的方式和终端读取参数的方式一样
# 注意:$10不能获取第10个参数,因为当n>=10时候,要用$(n)来获取参数。
funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $10 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !"
echo "The string of the parameters is $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

funWithReturn(){
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
read aNum
echo -n "Input another number: "
read anotherNum
echo "The two numbers are $aNum and $anotherNum !"
return $(($aNum+$anotherNum))
}
funWithReturn
# 函数返回值在调用该函数后通过$?来获得
echo "The sum of two numbers is $? !"

demo_call.sh文件内容

1
2
3
4
5
#!/bin/bash
function callFunction() {
echo "callFunction $1 !";
return 1;
}