shell-变量

shell-变量

shell简介

shell是一个用C语言编写的程序,是用户与linux沟通的桥梁。

如下代码,指定路径,告诉系统解释此shell脚本的shell程序。

#!/bin/bash

 

运行shell脚本

1)首先使脚本具有执行权限,chmod +x test.sh

2)运行shell脚本,./test.sh

 

变量赋值

变量名只能使用字母,数字和下划线,并且首字符不能以数字开头,使用变量时需在变量名前加上$符合。

#!/bin/bash
a=1
b="hello"
echo ${a}
echo ${b}
echo $a

 

花括号可加可不加,加的主要目的是帮助解释器识别变量的边界。比如下面这种情况就必须加花括号,不然$skill_score会被当成变量

#!/bin/bash
for score in A B C D;do
    echo "I got ${score}_score"
done

 

定义变量

1 #!/bin/bash
2 a=1
3 b="123"
4 baidu_url="www.baidu.com"  
5 readonly baidu_url #定义只读变量

 

全局变量和局部变量

1)在不做说明的情况下,shell中的变量都是全局变量

2)定义局部变量时,用关键字local

3)对于函数内的全局变量,一次调用函数后,这个全局变量就可以到处使用

#!/bin/bash
a=1
function test(){
    local b=2
    c=3
    echo "test," ${b}
}

echo ${a} ${b} ${c}
echo "--------"

test
echo ${a} ${b} ${c}

输出:

1
--------
test, 2
1 3

 

单引号与双引号

1 #!/bin/bash
2 a=1
3 b='123,${a}'
4 c="123,${a}"
5 echo ${b}
6 echo ${c}

注意

1)单引号字符串中的变量是无效的,双引号字符串中的变量是有效的

2)单引号字符串中不能出现单独的单引号,使用转义字符也不行,但是可成对出现作为字符串拼接

 

变量替换

1)${变量名#匹配规则}

从变量开头进行规则匹配,将符合最短的数据删除

 

2)${变量名##匹配规则}

从变量开头进行规则匹配,将符合最长的数据删除

 

3)${变量名%匹配规则}

从变量尾部进行规则匹配,将符合最短的数据删除

 

4)${变量名%%匹配规则}

从变量尾部进行规则匹配,将符合最长的数据删除

 

5)${变量名/旧字符串/新字符串}

变量内容符合旧字符串规则,则第一个旧字符串会被新字符串取代

 

6)${变量名//旧字符串/新字符串}

变量内容符合旧字符串规则,则全部的旧字符串会被新字符串取代

 

数组

定义数组

1 #!/bin/bash
2 arr1=(1 2 3)
3 arr2[0]="a"
4 arr2[1]="b"
5 echo ${arr1[0]} ${arr1[1]}
6 echo ${arr2[0]} ${arr2[1]}

 

获取数组中的所有元素

1 #!/bin/bash
2 arr1=(1 2 3)
3 echo ${arr1[@]}

 

获取数组的长度

1 #!/bin/bash
2 arr1=(21 2 3)
3 echo ${#arr1[@]} #获取数组元素的个数
4 echo ${#arr1[*]} #获取数组元素的个数
5 echo ${#arr1[0]} #获取数组单个元素的长度

 

字符串操作

1)计算字符串长度

方法一

${#string}

方法二

expr length "$string"

注意:string有空格则必须加双引号

1 #!/bin/bash
2 str1="hello"
3 len1=${#str1}
4 len2=`expr length "${str1}"`
5 echo ${len1}
6 echo ${len2}

 

2)获取子串在字符串中的索引位置

语法:expr index ${string} ${substring}

1 #!/bin/bash
2 #将子串进行拆分成字符,在字符串中从左往右查找,返回最先找到个子串中的字符的索引
3 str1="tom is a dog"
4 idx1=`expr index "${str1}" is`
5 echo ${idx1} #5
6 idx2=`expr index "${str1}" aot`
7 echo ${idx2} #1

 

3)计算子串长度

语法:expr match ${string} substr

1 #!/bin/bash
2 #子串必须从头到尾开始匹配,若从中间匹配则找不到
3 str1="tom is a dog"
4 len1=`expr match "${str1}" dog`
5 len2=`expr match "${str1}" tom`
6 len3=`expr match "${str1}" tom.*`
7 echo ${len1} #0
8 echo ${len2} #3
9 echo ${len3} #12

 

4)抽取子字符串

${string:position}

从string中的position开始

 

${string:position:length}

从position开始,匹配长度为length

 

${string: -position}

从右边开始匹配,注意:与-之间有空格,并且等价于${string:(-position)}

 

${string:(position)}

从左边开始匹配

 

expr substr $string $position $length

从position开始,匹配长度为length

 1 #!/bin/bash
 2 #子串必须从头到尾开始匹配,若从中间匹配则找不到
 3 str1="tom is a dog"
 4 sub_str1=${str1:5}
 5 sub_str2=${str1:5:3}
 6 sub_str3=${str1: -2}
 7 sub_str4=${str1:(-2):1}
 8 sub_str5=`expr substr "${str1}" 6 10`
 9 echo ${sub_str1} #s a dog
10 echo ${sub_str2} #s a
11 echo ${sub_str3} #og
12 echo ${sub_str4} #o
13 echo ${sub_str5} #s a dog

 

注意:expr索引是从1开始,${string:position}索引从0开始

posted @ 2019-11-27 21:46  可口可乐嗨  阅读(227)  评论(0编辑  收藏  举报
levels of contents