文章目录
六、字符串
- 之前可以利用${}引用变量,然而${}还有一个重要的功能,就是文本处理,单行文本基本上
可以满足所有需求。
6.1、获取字符串长度 ${#str}
${#字符串}
#!/bin/bash
str="hello world"
echo "${str}"
echo "${#str}"
[hadoop1@hadoop1 test]$ ./hello.sh
hello world
11
6.2、字符串切片截取 ${str:offset:length}
\${字符串:offset:length}截取从 索引为offset (字符索引从0开始)的字符开始,向后 length 个字符;不带length输出空白
#!/bin/bash
str="hello world"
echo "$str的索引为2开始,向后取6位字符为:${str:2:6}"
[hadoop1@hadoop1 test]$ ./hello.sh
hello world的索引为2开始,向后取6位字符为:llo wo
6.3、按照指定字符切割字符串,截取
${字符串#word} :左边最短匹配模式,取右边字符串
${字符串##word} :左边最长匹配模式,取右边字符串
${字符串%word} :右边最短匹配模式,取左边字符串
${字符串%%word}:右边最长匹配模式,取左边字符串
#!/bin/bash
url = "hadoop1/test/hello.sh"
echo "{$url#*/}:${url#*/}"
echo "{$url##*/}:${url##*/}"
echo "{$url%/*}:${url%/*}"
echo "{$url%%/*}:${url%%/*}"
[hadoop1@hadoop1 test]$ ./hello.sh
{hadoop1/test/hello.sh#*/}:test/hello.sh # "hadoop1/test/hello.sh"按照左短匹配将其分割为hadoop1和test/hello.sh ,取右边字符串test/hello.sh 以下同理
{hadoop1/test/hello.sh##*/}:hello.sh
{hadoop1/test/hello.sh%/*}:hadoop1/test
{hadoop1/test/hello.sh%%/*}:hadoop1
6.4、字符串替换 ${str/old/new}
${字符串/old_substring/new_substring}:将字符串中的第一次出现的old_substring替换为new_substring;
${字符串//old_substring/new_substring}:将字符串中的所有old_substring都替换为new_substring;
#!/bin/bash
str="hello world hello"
echo "{$str/hello/你好}:${str/hello/你好}"
echo "{$str//hello/你好}:${str//hello/你好}"
6.5、变量状态赋值
- ${变量:-string} 如果变量为空则返回 string
- ${变量:+string} 如果变量不为空则返回 string
- ${变量:=string} 如果变量为空则重新赋值变量为 string
- ${变量:?string} 如果变量为空则将 string 输出到控制台
#!/bin/bash
s=""
s2=""
echo ${s:-为空}
echo ${s:+不为空}
echo ${s:=我是空值}
echo ${s:+不为空}
echo ${s2:?我是空值}
[hadoop1@hadoop1 test]$ ./hello.sh
为空 # s="" 为空,满足条件则输出“为空”
# s="" 为空,不满足条件,输出空白
我是空值 # s=""为空,赋值“我是空值”,现在s="我是空值"
不为空 # s="我是空值",满足条件则输出"不为空"
./hello.sh:行8: s2: 我是空值 # s2="" 空值 输出"我是空值"
6.6、字符串颜色
-
有时候关键地方需要醒目,颜色是最好的方式:
-
\033[1;31;40m # 1 是显示方式,可选。31 是字体颜色。40m 是字体背景颜色。
-
\033[0m # 恢复终端默认颜色,即取消颜色设置。
#!/bin/bash
echo "============显示方式============"
for i in {1..8}; do
echo -e "\033[$i;31;40m Hello world! \033[0m"
done
echo "============字体颜色============"
for i in {31..37}; do
echo -e "\033[0;$i;40m Hello world! \033[0m"
done
echo "============颜色背景============"
for i in {41..47}; do
echo -e "\033[0;47;${i}m Hello world! \033[0m"
done
[hadoop1@hadoop1 test]$ ./hello.sh
七、数组
- 数组是相同类型的元素按一定顺序排列的集合。
- 定义方法 1:初始化数组 array=(a b c)
- 定义方法 2:新建数组并添加元素 array[下标]=元素
- 定义方法 3:将命令输出作为数组元素 array=($(command))
7.1、获取所有元素 ${array[*]} : *或@都可以
#!/bin/bash
[hadoop1@hadoop1 test]$ array=(1 7 9 5 3 2)
[hadoop1@hadoop1 test]$ echo ${array[*]}
1 7 9 5 3 2
7.2、获取元素下标 ${!array[@]}
[hadoop1@hadoop1 test]$ array=(1 7 9 5 3 2)
[hadoop1@hadoop1 test]$ echo ${!array[@]}
0 1 2 3 4 5
7.3、获取数组长度 ${#array[*]}
[hadoop1@hadoop1 test]$ array=(1 7 9 5 3 2)
[hadoop1@hadoop1 test]$ echo ${#array[@]}
6
7.4、获取元素 ${array[下标]}
[hadoop1@hadoop1 test]$ array=(1 7 9 5 3 2)
[hadoop1@hadoop1 test]$ echo ${array[2]}
9
7.5、添加与修改元素 array[i]=d array+=(e f g)
[hadoop1@hadoop1 test]$ array=(1 7 9 5 3 2)
[hadoop1@hadoop1 test]$ array[6]=10
[hadoop1@hadoop1 test]$ echo ${array[@]}
1 7 9 5 3 2 10
[hadoop1@hadoop1 test]$ array[0]=0
[hadoop1@hadoop1 test]$ echo ${array[@]}
0 7 9 5 3 2 10
[hadoop1@hadoop1 test]$ array+=(9 0 7)
[hadoop1@hadoop1 test]$ echo ${array[@]}
0 7 9 5 3 2 10 9 0 7
7.6、删除元素 unset array[0]
[hadoop1@hadoop1 test]$ unset array[3]
[hadoop1@hadoop1 test]$ echo ${array[@]}
0 7 9 3 2 10 9 0 7
7.7、删除数组 unset array
[hadoop1@hadoop1 test]$ unset array
[hadoop1@hadoop1 test]$ echo ${#array[@]}
0
附加:shell 的括号区分
括号 | 用途 |
( ) | 在运算中,先计算小括号里面的内容 数组 匹配分组 |
(( )) | 表达式,不支持-eq 这类的运算符。不支持-a 和-o,支持<=、>=、<、>这类 比较符和&&、|| C 语言风格的 for(())表达式 |
$( ) | 执行 Shell 命令,与反撇号(` `)等效 |
$(( )) | 简单算数运算 支持三目运算符 $(( 表达式?数字:数字 )) |
[ ] | 条件表达式,里面不支持逻辑判断符 |
[[ ]] | 条件表达式,里面不支持-a 和-o,不支持<=和>=比较符,支持-eq、<、>这类比较 符。支持=~模式匹配,也可以不用双引号也不会影响原意,比[]更加通用 |
$[ ] | 简单算数运算 |
{ } | 对逗号(,)和点点(...)起作用,比如 touch {1,2}创建 1 和 2 文件,touch {1..3}创建 1、2 和 3 文件 |
${ } | 引用变量 字符串处理 |