您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Shell字符串相關(guān)操作方法是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
Shell中針對(duì)字符串的操作有很多。結(jié)合代碼示例會(huì)比較容易理解
通過(guò)單引號(hào)拼接字符串
################### 使用單引號(hào)拼接字符串 ################### name1='white' str1='hello, '${name1}'' str2='hello, ${name1}' echo ${str1}_${str2} # Output: # hello, white_hello, ${name1} name3='green' str5='hello,'${name3}'' str6='hello, ${name3}' echo ${str5}_${str6}
運(yùn)行結(jié)果如下:
hello, white_hello, ${name1} hello,green_hello, ${name3}
通過(guò)雙引號(hào)拼接字符串
################### 使用雙引號(hào)拼接字符串 ################### name2="black" str3="hello, "${name2}"" str4="hello, ${name2}" echo ${str3}_${str4} # Output: # hello, black_hello, black name4='pink' str7="hello, "${name4}"" str8="hello, ${name4}" echo ${str7}_${str8}
運(yùn)行結(jié)果如下:
hello, black_hello, black hello, pink_hello, pink
獲取字符串長(zhǎng)度信息
################### 獲取字符串長(zhǎng)度 ################### text="12345" echo "${text} length is: ${#text}" # Output: # 12345 length is: 5 text1="qwert" echo "${text1} length is: ${#text1}" # 獲取子字符串 text="12345" echo ${text:2:2} # Output: # 34 text="asdfghjkl" echo ${text:3:4} #fghj
運(yùn)行結(jié)果如下:
12345 length is: 5 qwert length is: 5 34 fghj
查找字符串中對(duì)應(yīng)的子串的索引,索引值從1開(kāi)始計(jì)數(shù)。
################### 查找子字符串對(duì)應(yīng)的索引 ################### text="hello" echo `expr index "${text}" ll` # Output: # 3 # 索引從 1 開(kāi)始記錄 string_test="linux world" echo `expr index "${string_test}" w` # 7
運(yùn)行結(jié)果如下:
3 7
邏輯判斷字符串中是否包含子字符串
################### 判斷字符串中是否包含子字符串 ################### str="new_feature/" result=$(echo "${str}" | grep "feature/") if [[ "$result" != "" ]]; then echo "feature/ 是 ${str} 的子字符串" else echo "feature/ 不是 ${str} 的子字符串" fi
運(yùn)行結(jié)果如下:
feature/ 是 new_feature/ 的子字符串
shell獲取字符串特定模式匹配右邊的內(nèi)容
################### 截取關(guān)鍵字右邊內(nèi)容 ################### full_branch="feature/1.0.0" branch=`echo ${full_branch#feature/}` echo "branch is ${branch}" full_name="aaa_bbb" right_half=`echo ${full_name#aaa_}` echo "right half of ${full_name} is ${right_half}"
運(yùn)行結(jié)果如下:
branch is 1.0.0 right half of aaa_bbb is bbb
shell獲取字符串特定模式匹配左邊的內(nèi)容
################### 截取關(guān)鍵字左邊內(nèi)容 ################### full_version="0.0.1-SNAPSHOT" version=`echo ${full_version%-SNAPSHOT}` echo "version is ${version}" full_address="california-kk" left_address=`echo ${full_address%-kk}` echo "left_address is ${left_address}"
運(yùn)行結(jié)果如下:
version is 0.0.1 left_address is california
將字符串分割成數(shù)組
################### 字符串分割成數(shù)組 ################### str="0.0.0.1" OLD_IFS="$IFS" IFS="." array=( ${str} ) IFS="$OLD_IFS" size=${#array[*]} lastIndex=`expr ${size} - 1` echo "數(shù)組長(zhǎng)度:${size}" echo "最后一個(gè)數(shù)組元素:${array[${lastIndex}]}" for item in ${array[@]} do echo "$item" done ip_address="192.168.1.1" OLD_IFS="$IFS" IFS="." array=( ${ip_address} ) IFS="$OLD_IFS" ip_size=${#array[*]} lastIndex=`expr ${ip_size} - 1` firstIndex=`expr ${ip_size} - 4` echo "IP地址對(duì)應(yīng)的數(shù)組長(zhǎng)度:${ip_size}" echo "IP地址的第一段對(duì)應(yīng)是:${array[${firstIndex}]}" for element in ${array[@]} do echo ${element} done
運(yùn)行結(jié)果如下:
數(shù)組長(zhǎng)度:4 最后一個(gè)數(shù)組元素:1 0 0 0 1 IP地址對(duì)應(yīng)的數(shù)組長(zhǎng)度:4 IP地址的第一段對(duì)應(yīng)是:192 192 168 1 1
邏輯判斷字符串是否為空
################### 判斷字符串是否為空 ################### #-n 判斷長(zhǎng)度是否非零 #-z 判斷長(zhǎng)度是否為零 str=testing str2='' if [[ -n "$str" ]] then echo "The string $str is not empty" else echo "The string $str is empty" fi if [[ -n "$str2" ]] then echo "The string $str2 is not empty" else echo "The string $str2 is empty" fi if [[ -z $str2 ]] then echo "==The string $str2 is empty==" else echo "The string $str2 is not empty" fi # Output: # The string testing is not empty # The string is empty
運(yùn)行結(jié)果如下:
The string testing is not empty The string is empty ==The string is empty==
字符串比較邏輯
################### 字符串比較 ################### str=hello str2=world if [[ $str = "hello" ]]; then echo "str equals hello" else echo "str not equals hello" fi if [[ $str2 = "hello" ]]; then echo "str2 equals hello" else echo "str2 not equals hello" fi str3=linux if [[ $str3 = "linux" ]];then echo "str3 equals linux" else echo "str3 not equal linux" fi
運(yùn)行結(jié)果如下:
str equals hello str2 not equals hello str3 equals linux
“Shell字符串相關(guān)操作方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。