shell指令碼處理字串常用方法

2021-06-16 16:10:36 字數 4244 閱讀 6933

一、構造字串

直接構造

str_zero=hello

str_first="i am a string"

str_second='success'

重複多次

#repeat the first parm($1) by $2 times

strrepeat()

舉例:str_repeat=`strrepeat "$user_name" 3`

echo "repeat = $str_repeat"

二、賦值與拷貝

直接賦值

與構造字串一樣

user_name=terry

從變數賦值

aliase_name=$user_name

三、聯接

直接聯接兩個字串

str_temp=`printf "%s%s" "$str_zero" "$user_name"`

使用printf可以進行更複雜的聯接

四、求長

獲取字串變數的長度:$

求字元數(char)

count_char=`echo "$str_first" | wc -m`

echo $count_char

求位元組數(byte)

count_byte=`echo "$str_first" | wc -c`

echo $count_byte

求字數(word)

count_word=`echo "$str_first" | wc -w`

echo $count_word

五、比較

相等比較 str1 = str2

不等比較 str1 != str2

舉例:if [ "$user_name" = "terry" ]; then

echo "i am terry"

fi小於比較

#return 0 if the two string is equal, return 1 if $1 < $2, else 2strcompare()

六、測試

判空   -z str

判非空  -n str

是否為數字

# return 0 if the string is num, otherwise 1

strisnum()

舉例:if [ -n "$user_name" ]; then

echo "my name is not empty"

fiecho `strisnum "9980"`

七、分割

以符號+為準,將字元分割為左右兩部分

使用sed

舉例:命令 date --rfc-3339 seconds 的輸出為

2007-04-14 15:09:47+08:00

取其+左邊的部分

date --rfc-3339 seconds | sed 's/+[0-9][0-9]:[0-9][0-9]//g'

輸出為2007-04-14 15:09:47

取+右邊的部分

date --rfc-3339 seconds | sed 's/.*+//g'

輸出為08:00

以空格為分割符的字串分割

使用awk

舉例:str_fruit="banana 0.89 100"

取第3欄位

echo $str_fruit | awk ''

八、子字串

字串1是否為字串2的子字串

# return 0 is $1 is substring of $2, otherwise 1

strissubstring()

一、

linux

shell 擷取字元變數的前8位,有方法如下:

1.expr substr 「$a」 1 8

2.echo $a|awk 『』

3.echo $a|cut -c1-8

4.echo $

5.expr $a : 『\(.\\).*』

6.echo $a|dd bs=1 count=8 2>/dev/null

二、按指定的字串擷取

1、第一種方法:

「*」只是乙個萬用字元可以不要

例子:$ myvar=foodforthought.jpg

2、第二種方法:$:擷取變數varible從n1開始的n2個字元,組成乙個子字串。可以根據特定字元偏移和長度,使用另一種形式的變數擴充套件,來選擇特定子字串。試著在 bash 中輸入以下行:

$ exclaim=cowabunga

$ echo $

cow$ echo $

abunga

這種形式的字串截斷非常簡便,只需用冒號分開來指定起始字元和子字串長度。

三、按照指定要求分割:

比如獲取字尾名

ls -al | cut -d 「.」 -f2

運算子

描述

示例

檔案比較運算子

-efilename

如果filename存在,則為真

[ -e /var/log/syslog ]

-dfilename

如果filename為目錄,則為真

[ -d /tmp/mydir ]

-ffilename

如果filename為常規檔案,則為真

[ -f /usr/bin/grep ]

-lfilename

如果filename為符號鏈結,則為真

[ -l /usr/bin/grep ]

-rfilename

如果filename可讀,則為真

[ -r /var/log/syslog ]

-wfilename

如果filename可寫,則為真

[ -w /var/mytmp.txt ]

-xfilename

如果filename可執行,則為真

[ -l /usr/bin/grep ]

filename1-ntfilename2

如果filename1比filename2新,則為真

[ /tmp/install/etc/services -nt /etc/services ]

filename1-otfilename2

如果filename1比filename2舊,則為真

[ /boot/bzimage -ot arch/i386/boot/bzimage ]

字串比較運算子[size=-1]

(請注意引號的使用,這是防止空格擾亂**的好方法)

-zstring

如果string長度為零,則為真

[ -z "$myvar" ]

-nstring

如果string長度非零,則為真

[ -n "$myvar" ]

string1=string2

如果string1與string2相同,則為真

[ "$myvar" = "one two three" ]

string1!=string2

如果string1與string2不同,則為真

[ "$myvar" != "one two three" ]

算術比較運算子

num1-eqnum2

等於[ 3 -eq $mynum ]

num1-nenum2

不等於[ 3 -ne $mynum ]

num1-ltnum2

小於[ 3 -lt $mynum ]

num1-lenum2

小於或等於

[ 3 -le $mynum ]

num1-gtnum2

大於[ 3 -gt $mynum ]

num1-genum2

大於或等於

[ 3 -ge $mynum ]

shell指令碼處理字串常用方法

一 構造字串 直接構造 str zero hello str first i am a string str second success 重複多次 repeat the first parm 1 by 2 times strrepeat 舉例 str repeat strrepeat user n...

shell指令碼處理字串常用方法

一 構造字串 直接構造 str zero hello str first i am a string str second success 重複多次 repeat the first parm 1 by 2 times strrepeat 舉例 str repeat strrepeat user n...

shell常用的指令碼處理命令

1 cut echo path cut d f 5 d 以 為分隔字元 f fields 選取第幾段 export cut c 12 13 c 字元範圍,即以字元為單位在每一行中切出一部分 2 grep grep acinv color auto 查詢字串 filename c 計算查詢 字串 的個...