欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

1.shell编程-变量的高级用法

程序员文章站 2022-05-15 14:24:11
1.1.变量替换 变量替换的六种形式 实例:非贪婪和贪婪的区别 从头部删除 从尾部删除 字符串替换,把bin替换成大写的BIN,单斜线和双斜线的区别 1.2.字符串处理 计算字符串长度 方法一 方法二 string有空格,则必须加双引号 实例 获取子串在字符串中的索引位置 实例 会把子串分割成一个一 ......

 

 

 

1.1.变量替换

变量替换的六种形式

1.shell编程-变量的高级用法

 

实例:非贪婪和贪婪的区别

从头部删除

[root@vm_0_9_centos shell_learn]# var_1="i love you,do you love me"
[root@vm_0_9_centos shell_learn]# echo $var_1
i love you,do you love me
[root@vm_0_9_centos shell_learn]# var1=${var_1#*ov}
[root@vm_0_9_centos shell_learn]# echo $var1
e you,do you love me
[root@vm_0_9_centos shell_learn]# var2=${var_1##*ov}
[root@vm_0_9_centos shell_learn]# echo $var2
e me
[root@vm_0_9_centos shell_learn]# 

 从尾部删除

[root@vm_0_9_centos shell_learn]# var_1="i love you,do you love me"
[root@vm_0_9_centos shell_learn]# echo $var_1
i love you,do you love me
[root@vm_0_9_centos shell_learn]# var3=${var_1%ov*}
[root@vm_0_9_centos shell_learn]# echo $var3
i love you,do you l
[root@vm_0_9_centos shell_learn]# var4=${var_1%%ov*}
[root@vm_0_9_centos shell_learn]# echo $var4
i l
[root@vm_0_9_centos shell_learn]# 

 字符串替换,把bin替换成大写的bin,单斜线和双斜线的区别

[root@vm_0_9_centos shell_learn]# echo $path
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@vm_0_9_centos shell_learn]# 
[root@vm_0_9_centos shell_learn]# var5=${path/bin/bin}
[root@vm_0_9_centos shell_learn]# echo $var5
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@vm_0_9_centos shell_learn]# 
[root@vm_0_9_centos shell_learn]# var6=${path//bin//bin}
[root@vm_0_9_centos shell_learn]# echo $var6
/usr/local/s/bin:/usr/local//bin:/usr/s/bin:/usr//bin:/root//bin
[root@vm_0_9_centos shell_learn]#

1.2.字符串处理

计算字符串长度

方法一

${#string}

方法二

string有空格,则必须加双引号

expr length "$string"    

实例

[root@vm_0_9_centos shell_learn]# var1="hello world"
[root@vm_0_9_centos shell_learn]# len=${#var1}
[root@vm_0_9_centos shell_learn]# echo $len
11
[root@vm_0_9_centos shell_learn]# len2=`expr length "$var1"`
[root@vm_0_9_centos shell_learn]# echo $len2
11
[root@vm_0_9_centos shell_learn]# 

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

expr index $string $substring

实例

[root@vm_0_9_centos shell_learn]# var1="quickstart is a app"
[root@vm_0_9_centos shell_learn]# index=`expr index "$var1" start`
[root@vm_0_9_centos shell_learn]# echo $index
6
[root@vm_0_9_centos shell_learn]# index2=`expr index "$var1" uniq`
[root@vm_0_9_centos shell_learn]# echo $index2
1
[root@vm_0_9_centos shell_learn]# index3=`expr index "$var1" cnk`
[root@vm_0_9_centos shell_learn]# echo $index3
4
[root@vm_0_9_centos shell_learn]# 

会把子串分割成一个一个字符,index是最先找到的那个字符的位置。

计算子串长度

expr match $string substr

 实例

[root@vm_0_9_centos shell_learn]# var1="quickstart is a app"
[root@vm_0_9_centos shell_learn]# len=`expr match "$var1" quic`
[root@vm_0_9_centos shell_learn]# echo $len
4
[root@vm_0_9_centos shell_learn]# len=`expr match "$var1" app`
[root@vm_0_9_centos shell_learn]# echo $len
0
[root@vm_0_9_centos shell_learn]# len=`expr match "$var1" quic.*`
[root@vm_0_9_centos shell_learn]# echo $len
19
[root@vm_0_9_centos shell_learn]# 

必须从开头匹配才可以

抽取子串

1.shell编程-变量的高级用法

 实例

[root@vm_0_9_centos shell_learn]# var1="kafka hadoop yarn mapreduce"
[root@vm_0_9_centos shell_learn]# sub1=${var1:10}
[root@vm_0_9_centos shell_learn]# echo $sub1
op yarn mapreduce
[root@vm_0_9_centos shell_learn]# sub2=${var1:10:5}
[root@vm_0_9_centos shell_learn]# echo $sub2
op ya
[root@vm_0_9_centos shell_learn]# sub3=${var1: -5}
[root@vm_0_9_centos shell_learn]# echo $sub3
educe
[root@vm_0_9_centos shell_learn]# sub4=${var1:(-6)}
[root@vm_0_9_centos shell_learn]# echo $sub4
reduce
[root@vm_0_9_centos shell_learn]# sub5=${var1: -5:3}
[root@vm_0_9_centos shell_learn]# echo $sub5
edu
[root@vm_0_9_centos shell_learn]# sub6=`expr substr "$var1" 10 5`
[root@vm_0_9_centos shell_learn]# echo $sub6
oop y
[root@vm_0_9_centos shell_learn]# 

 注意:使用expr索引是从1开始计算,使用${string:position},索引从0开始计算。

1.3.字符串处理完整脚本

1.shell编程-变量的高级用法

思路分析

1.将不同的功能模块划分,并编写函数
    function print_tips
    function len_of_string
    function del_hadoop
    function rep_hadoop_mapreduce_first
    function rep_hadoop_maapreduce_all

2.实现第一步所定义的功能函数

3.程序主流程的设计

vim example.sh

#!/bin/bash

string="bigdata process framework is hadoop,hadoop is an open source project"

function print_tips
{
    echo "******************************"
    echo "(1)打印string长度"
    echo "(2)删除字符串中所有的hadoop"
    echo "(3)替换第一个hadoop为mapreduce"
    echo "(4)替换全部hadoop为mapreduce"
    echo "*******************************"        
}

function len_of_string
{
    echo "${#string}"    
}

function del_hadoop
{
    echo "${string//hadoop/}"
}

function rep_hadoop_mapreduce_first
{
    echo "${string/hadoop/mapreduce}"
}

function rep_hadoop_mapreduce_all
{
        echo "${string//hadoop/mapreduce}"
}

while true
do
    echo "[string=$string]"
    echo
    print_tips
    read -p "pls input your choice(1|2|3|4|q|q): " choice
    
    case $choice in
        1)
            len_of_string
            ;;
        2)
            del_hadoop
            ;;
        3)
            rep_hadoop_mapreduce_first
            ;;
        4)
            rep_hadoop_mapreduce_all
            ;;
        q|q)
            exit
            ;;
        *)
            echo "error,input only in {1|2|3|4|q|q|}"
            ;;
    esac
done

sh example.sh

1.shell编程-变量的高级用法

1.4.命令替换

语法格式

方法一:
`command`

方法二:
$(command)

实例一:获取系统所有的用户并输出

cat /etc/passwd | cut -d ":" -f 1

vim example2.sh

#!/bin/bash
#

index=1
for user in `cat /etc/passwd | cut -d ":" -f 1`
do 
    echo "this is $index user: $user"
    index=$(($index+1))

done

结果

1.shell编程-变量的高级用法

 实例二:根据当前当前时间计算今年和明年

$(());两个括号主要用来进行整数运算

[root@vm_0_9_centos shell_learn]# date
wed jun 26 21:58:04 cst 2019
[root@vm_0_9_centos shell_learn]# date +%y
2019
[root@vm_0_9_centos shell_learn]# echo "this is $(date +%y) year"
this is 2019 year
[root@vm_0_9_centos shell_learn]# echo "this is $(($(date +%y) + 1)) year"
this is 2020 year
[root@vm_0_9_centos shell_learn]# 

实例三:根据当前时间获取今年还剩下多少星期和已经过了多少星期

[root@vm_0_9_centos shell_learn]# date +%j
177
[root@vm_0_9_centos shell_learn]# echo "this yaer have passed $(date +%j) days"
this yaer have passed 177 days
[root@vm_0_9_centos shell_learn]# echo "this yaer have passed $(($(date +%j)/7)) weeks"
this yaer have passed 25 weeks
[root@vm_0_9_centos shell_learn]# echo "今年还剩下$(((365 - $(date +%j))/7))星期"
今年还剩下26星期
[root@vm_0_9_centos shell_learn]#

 实例四:判断nginx进程是否存在,若不存在则自动拉起该进程

[root@vm_0_9_centos shell_learn]# ps -ef |grep nginx
root      6658     1  0 22:33 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     6659  6658  0 22:33 ?        00:00:00 nginx: worker process
root      6891   501  0 22:35 pts/0    00:00:00 grep --color=auto nginx
[root@vm_0_9_centos shell_learn]# ps -ef |grep nginx |grep -v grep |wc -l
2
[root@vm_0_9_centos shell_learn]# systemctl stop nginx
[root@vm_0_9_centos shell_learn]# 
[root@vm_0_9_centos shell_learn]# ps -ef |grep nginx |grep -v grep |wc -l
0
[root@vm_0_9_centos shell_learn]# sh example3.sh 
[root@vm_0_9_centos shell_learn]# 
[root@vm_0_9_centos shell_learn]# ps -ef |grep nginx |grep -v grep |wc -l
2
[root@vm_0_9_centos shell_learn]#

vim example3.sh

如果nginx的进程个数为0,则拉起该进程

#!/bin.bash
#

nginx_process_num=$(ps -ef | grep nginx | grep -v grep | wc -l)

if [ $nginx_process_num -eq 0 ];then
    systemctl start nginx
fi

1.5.有类型变量

declare和typeset命令

  • declare和typeset命令两者等价
  • declare和typeset命令都是用来定义变量类型的

1.shell编程-变量的高级用法

取消申明的变量

declare +r
declare +i
declare +a
declare +x

实例一:-r 将变量设为只读

[root@vm_0_9_centos shell_learn]# var1="hello world"
[root@vm_0_9_centos shell_learn]# var1="hello python"
[root@vm_0_9_centos shell_learn]# echo $var1
hello python
[root@vm_0_9_centos shell_learn]# declare -r var1
[root@vm_0_9_centos shell_learn]# var1="hello go"
-bash: var1: readonly variable
[root@vm_0_9_centos shell_learn]# 

实例二:-i 将变量设为整数

shell中如果不声明,默认当做字符串处理

[root@vm_0_9_centos shell_learn]# num1=10
[root@vm_0_9_centos shell_learn]# num2=$num1+20
[root@vm_0_9_centos shell_learn]# echo $num2
10+20
[root@vm_0_9_centos shell_learn]# declare -i num2
[root@vm_0_9_centos shell_learn]# num2=$num1+20
[root@vm_0_9_centos shell_learn]# echo $num2
30
[root@vm_0_9_centos shell_learn]#

实例三:-a 将变量定义为数组

定义数组

[root@vm_0_9_centos shell_learn]# declare -a array
[root@vm_0_9_centos shell_learn]# array=("jones" "mike" "kobe" "jordan")

输出数组所有的内容

[root@vm_0_9_centos shell_learn]# echo ${array[@]}
jones mike kobe jordan

第一个元素

[root@vm_0_9_centos shell_learn]# echo ${array[0]}
jones

数组长度

[root@vm_0_9_centos shell_learn]# echo ${#array[@]}
4

删除元素

[root@vm_0_9_centos shell_learn]# unset array

 1.6.bash数学运算之expr

语法格式

1.shell编程-变量的高级用法

1.shell编程-变量的高级用法

 1.shell编程-变量的高级用法

需要加转义字符“\”

[root@vm_0_9_centos shell_learn]# num1=30
[root@vm_0_9_centos shell_learn]# num2=40
[root@vm_0_9_centos shell_learn]# expr $num1 \> $num2
0
[root@vm_0_9_centos shell_learn]# expr $num1 \< $num2
1
[root@vm_0_9_centos shell_learn]# expr $num1 + $num2
70
[root@vm_0_9_centos shell_learn]# expr $num1 - $num2
-10
[root@vm_0_9_centos shell_learn]# expr $num1 * $num2
expr: syntax error
[root@vm_0_9_centos shell_learn]# expr $num1 \* $num2
1200

实例:输入一个正整数num,然后计算1+2+3+....+num的值,必须判断num是否为正整数,不符合重新输入

 vim num.sh