![在这里插入图片描述](https://img-blog.csdnimg.cn/7c61bfc2aee34d0cbf1d7052b4a3755a.gif#pic_center)
1、Shell数组
Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小,数组元素的下标由 0 开始,下标可以是整数或算术表达式,其值应大于或等于 0。
1.1、定义数组
语法
数组名=(值1 值2 … 值n)
或者
数组名=(
值1
值2
…
值n)
或者
数组名[下标]=值 注:可以不使用连续的下标,而且下标的范围没有限制
在 Shell 中,用括号来表示数组,数组元素用"空格"符号分割开
案例
自定义一个数组
[root@root test02]# vim test03.sh
array_string=(a s c v bb)
echo "第一个元素为:${array_string[0]}"
[root@root test02]# ./test03.sh
第一个元素为:a
1.2、读取数组
语法
${数组名[下标]}
下标说明
- 下标必须大于或等于 0
- 下标不能超过数组的大小范围
- 可以使用
@
或者*
代表所有元素的下标
案例
取出array_string中的元素
[root@root test02]# vim test03.sh
array_string=(a s c v bb)
echo "第一个元素为:${array_string[0]}"
echo "取出数组中所有的元素:${array_string[*]}"
echo "取出数组中所有的元素:${array_string[@]}"
[root@root test02]# ./test03.sh
第一个元素为:a
取出数组中所有的元素:a s c v bb
取出数组中所有的元素:a s c v bb
1.3、获取数组长度
语法
获取单个元素的长度
${#数组名[下标]}
获取数组的长度
${#数组名[@]}或 ${#数组名[*]}
下标说明
-
当下标是具体的数字时,所获取的长度为当前元素大小
-
当下标为@
或*
时,代表的是数组的长度
-
${#数组名[@]} 或 ${#数组名[*]}
案例
获取数组的长度
[root@root test02]# vim test03.sh
array_string=(a s c v bb)
echo "第一个元素为:${array_string[0]}"
echo "取出数组中所有的元素:${array_string[*]}"
echo "取出数组中所有的元素:${array_string[@]}"
echo "获取数组的长度:${#array_string[@]}"
echo "获取数组中第五个元素的长度:${#array_string[4]}"
[root@root test02]# ./test03.sh
第一个元素为:a
取出数组中所有的元素:a s c v bb
取出数组中所有的元素:a s c v bb
获取数组的长度:5
获取数组中第五个元素的长度:2
2、Shell传递参数
Shell传递参数即可以在执行 Shell 脚本时,向脚本传递参数,类似于位置参数变量和预定义参数
参数 |
作用 |
$n |
n 为数字, $0 代表命令本身,$1-$9 代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如${10}
|
$* |
代表命令行中所有的参数,把所有的参数看成一个整体 |
$@ |
代表命令行中所有的参数,把每个参数区分对待 |
$# |
代表命令行中所有参数的个数 |
$$ |
当前进程的进程号(PID) |
$! |
后台运行的最后一个进程的进程号(PID) |
$? |
最后一次执行的命令的返回状态。 0表示正确执行;其他数字表示执行不正确。 |
注:$*与 $@ 区别
- 相同点:都是引用所有参数。
- 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,则
*
等价于 “1 2 3”(传递了一个参数),而 @
等价于 “1” “2” “3”(传递了三个参数)。
案例
[root@root test02]# vim test01.sh
#!/bin/bash
echo "$0 $1 $2"
echo "$*"
echo "$@"
echo "参数总个数为=$#"
[root@root test02]# ./test01.sh 10 11
./test01.sh 10 11
10 11
10 11
参数总个数为=2
#$0 表示本身
#$1 表示第一个参数
[root@root test02]# vim test01.sh
echo "当前进程的id=$$"
./test01.sh $
echo "后台最后一个进程的id=$!"
echo "执行的值=$?"
[root@root test02]# test01.sh
当前进程的id=25994
3、Shell echo
Shell echo主要用于字符串的输出,在shell中经常使用。
语法
echo [参数] [需要输出的内容]
参数
输出内容
- 普通文本
- 变量
- 转义字符
- 结果定向输出
- 输出执行结果,注意: 这里使用的是反引号 `, 而不是单引号 '
案例
1.输出普通字符串
[root@root test02]# echo "hello world"
hello world
[root@root test02]# echo hello world
hello world
[root@root test02]# echo 'hello world'
hello world
2.输出变量
[root@root test02]# echo $PATH
/usr/java/jdk1.8.0_221-amd64/bin:/usr/java/jdk1.8.0_221-amd64/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
3.转义输出
#普通输出
[root@root test02]# echo "It is a test"
It is a test
#需要带引号,此时可以转义
[root@root test02]# echo "\"It is a test\""
"It is a test"
#-e 开启转义
[root@root test02]# echo -e "OK! \c"
4.结果定向输出
[root@root test02]# echo "hello shell" > helloTwo.java
[root@root test02]# tac helloTwo.java
hello shell
5.输出执行结果
[root@root test02]# vim test01.sh
sum=0
for((i=0;i<=100;i++))
do
sum=$[ $sum + $i]
done
echo "1-100的和为:total=$sum"
[root@root test02]# ./test01.sh
1-100的和为:total=5050
4、Shell printf
Shell printf和echo类似,都主要用于字符串的输出,但是printf的可移植性更好。在printf 中可以使用格式化字符串,还可以定制字符串的宽度、左右对齐方式等。默认情况下 printf 不会自动添加换行符,需要手动添加 \n。
语法
printf 格式化字符串 [参数列表]
格式化字符串
格式化字符串 |
作用 |
空 |
%s |
输出一个字符串 |
NUll |
%d |
整型输出 |
0 |
%c |
输出一个字符 |
Null |
%f |
输出实数,以小数形式输出 |
0.0 |
%b |
相对应的参数被视为含有要被处理的转义序列之字符串。 |
|
格式化后的参数
-
- 表示左对齐,
- 没有 表示右对齐
-
n 数字,表示宽度或者精度,如果表示宽度时字符没有达到规定的宽度,则以空格表示,超过也会将内容全部显示出来。
案例
[root@root /]# printf "%d %s\n" 1 "abc"
1 abc
[root@root /]# printf '%d %s\n' 1 "abc"
1 abc
[root@root /]# printf %s abcdef
abcdef[root@root /]#
[root@root /]# printf %s\n abcdef
abcdefn[root@root /]#
[root@root /]# printf "%s\n" abc def
abc
def
[root@root /]# printf %s abc def
abcdef[root@root /]# printf "%s\n" abcdef
abcdef
[root@root /]# printf "%s %s %s\n" a b c d e f g h i j
a b c
d e f
g h i
j
[root@root /]# printf "%s and %d \n"
and 0
转义序列
序列 |
说明 |
\n |
换行(常用) |
\r |
回车 |
\t |
水平制表符 |
![在这里插入图片描述](https://img-blog.csdnimg.cn/ccdfc897c1b34cae8b75b48cd60fff65.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNTIwOTEy,size_16,color_FFFFFF,t_70#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/828c258818274ce2a4a68f9809ca1fb3.gif#pic_center)