shell中的 expr命令的一些小用法

2021-08-11 20:24:05 字數 1000 閱讀 5744

用途:求表示式變數的值

語法: expr [expression]

描述: expr 命令讀入[expression]引數,計算它的值,然後將結果寫入到標準輸出。

用例:

1.expr substr string1 startposition length

返回字串』hello world』中從位置3開始的5個字串長的字串

expr substr

'hello world'

35

返回:hello

2.expr index string1 string2

返回字串』ab』中的任何乙個字元在』aabcdeabcc』中出現的第乙個位置

expr index

'aabcdeabcc'

'ab'

返回:1

3.expr length string1

返回字串』hello』的長度

expr length

'hello'

4.expr match 『string1』 『string2』

如果string1和string2相等(忽略空格,出去空格後的字串),則返回string的長度

如果string1和string2不等,則返回0

expr match

'hello'

'hello'

返回:5

expr match

'hello '

'hello'

返回:5 (忽略空格)

expr match

'hello'

'hello world'

返回:0

shell中的expr命令

expr 可以進行的操作如下 邏輯操作 arg1 arg2 邏輯或操作,真則返回arg1,否則返回arg2 以null或者0來判斷引數的真假,有短路功能 arg1 arg2 邏輯與操作,真則返回arg1,否則返回arg2 以null或者0來判斷引數真假,有短路功能 關係操作 arg1 arg2 或者...

shell 關於expr的用法

關於expr的用法 2004 04 23 15 18 pm 來自 linux文件 現載 www.8s8s.com 我執行了一下,沒有錯啊 你用的是什麼平台?什麼shell?我執行了一下,也沒有錯啊 hp ux ksh 請問 sum和 的值必須是整數嗎?hp ux上提供的ksh不支援陣列,所以sale...

bash shell中expr命令下幾種的使用

expr在linux中是乙個功能非常強大的命令。通過學習做乙個小小的總結。1 計算字串的長度。我們可以用awk中的length s 進行計算。我們也可以用echo中的echo 進行計算,當然也可以expr中的expr length string 求出字串的長度。舉例 root localhost s...