1. 程式人生 > >1.shell程式設計之變數的高階用法

1.shell程式設計之變數的高階用法

1.1.變數替換

變數替換的六種形式

 

例項:非貪婪和貪婪的區別

從頭部刪除

[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]# 

必須從開頭匹配才可以

抽取子串

 例項

[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.將不同的功能模組劃分,並編寫函式
    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

&n