Shell程式設計

2021-06-28 22:09:32 字數 3349 閱讀 6638

shell程式設計

比較兩個字串是否相等:

if [ "$test"x = "test"x ]; then

關鍵點:

1 使用單個等號

2 等號兩邊各有乙個空格:這是unix shell的要求

3 "$test"x最後的x,是特意安排的,因為當$test為空的時候,上面的表示式就變成了x = testx,顯然是不相等的。而如果沒有這個x,表示式就會報錯:[: =: unary operator expected

二元比較操作符,比較變數或者比較數字.注意數字與字串的區別.

整數比較

-eq 等於,如:if [ "$a" -eq "$b" ]

-ne 不等於,如:if [ "$a" -ne "$b" ]

-gt 大於,如:if [ "$a" -gt "$b" ]

-ge 大於等於,如:if [ "$a" -ge "$b" ]

-lt 小於,如:if [ "$a" -lt "$b" ]

-le 小於等於,如:if [ "$a" -le "$b" ]

大於(需要雙括號),如:(("$a" > "$b"))

>= 大於等於(需要雙括號),如:(("$a" >= "$b"))

小資料比較可使用awk

字串比較

= 等於,如:if [ "$a" = "$b" ]

== 等於,如:if [ "$a" == "$b" ],與=等價

shell指令碼按行讀取檔案的方法有3種:

#!/bin/bash

echo "##### 方法 1 #####"

while

read line1  

doecho $line1  

done < $1  

echo "##### 方法 2 #####"

cat $1 | while

read line2  

doecho $line2  

done  

echo "##### 方法 3 #####"

forline3 in $(<$1)  

doecho $line3  

done  

執行結果

snail@ubuntu:5.read-line$ cat file.bin 

hello world

this is 1

this is 2

this is 3

snail@ubuntu:5.read-line$ ./read-line.sh file.bin 

##### 方法 1 #####

hello world

this is 1

this is 2

this is 3

##### 方法 2 #####

hello world

this is 1

this is 2

this is 3

##### 方法 3 #####

hello

world

thisis1

thisis2

thisis3

《以上例程**來自網路:

此處》

請注意方法1和方法2均可以按行讀取內容,而不需特別注意其他特定字元,而第三種方法除可以按行讀取內容外,還會斷開每行中所含有的空格,即以空格斷開內容,使用時應留意,這也是在寫指令碼的過程中發現的哦,哈哈~

同時,也可以通過設定ifs值來實現方法3的按行讀取,如:

ifs=$

'\n'

echo "##### 方法 3 #####"

forline3 in $(<$1)  

doecho $line3  

done 

ifs的具體含義是shell常識,後面也會具體介紹.

c>.shell sed字串操作

a追加內容 用法:sed 『/匹配詞/a\要加入的內容』 example.file(將內容追加到匹配的目標行的下一行位置)

i 插入內容 用法:

sed 『/匹配詞/i\要加入的內容』 example.file 將內容插入到匹配的行目標的上一行位置)

示例:

1mname=yangyangyang

mline=pipipi

#行前加

#在匹配[

mline內容

]的行前插入一行,內容為tab縮排的

mline的值+換行+mname的值,相當於在匹配行前插入了兩行內容;

sed -i "/

$mline

/i \    $

mline

\n\\$

mname

" $m1line.txt

2

3#在匹配[mline內容]的行後插入一行,內容為mname的值;

sed -i "/$mline/a\\$mname" $mfile.txt

4

d>.shell中的ifs 介紹

shell 指令碼中有個變數叫ifs(internal field seprator) ,內部域分隔符。完整定義是the shell uses the value stored in ifs, which is thespace, tab, and newline characters by default, to delimit words for the read and set commands, when parsing output from command substitution, and when performing variable substitution.

shell 的環境變數分為 set, env 兩種,其中 set 變數可以通過 export 工具匯入到 env 變數中。其中,set 是顯示設定shell變數,僅在本 shell 中有效;env 是顯示設定使用者環境變數 ,僅在當前會話中有效。換句話說,set 變數裡包含了 env 變數,但 set 變數不一定都是 env 變數。這兩種變數不同之處在於變數的作用域不同。顯然,env 變數的作用域要大些,它可以在 subshell 中使用。

而ifs 是一種 set 變數,當 shell 處理"命令替換"和"引數替換"時,shell 根據 ifs 的值,預設是 space, tab, newline 來拆解讀入的變數,然後對特殊字元進行處理,最後重新組合賦值給該變數。

檢視變數 ifs 的值:

$ echo $ifs  

$ echo "$ifs" | od -b  

0000000 040 011 012 012  

0000004 

直接輸出ifs是看不到的,把它轉化為二進位制就可以看到了,"040"是空格,"011"是tab,"012"是換行符"\n" 。最後乙個 012 是因為 echo 預設是會換行的。

未完,不斷更新中.....

Shell程式設計 shell特性

linux會預設記錄1000條歷史記錄,可通過 echo histsize 檢視,如果講histsize更改為2000,那麼會預設儲存2000條。1000條記錄儲存在家目錄的 bash history 中,僅當使用者正常退出當前shell時,當前shell中執行的命令才會儲存到 bash histo...

Shell程式設計 Shell函式

shell函式 1.將命令序列按格寫在一起 2.可方便重複使用命令序列 3.shell函式定義 function 函式名 4.呼叫函式的方法 函式名 引數1 引數2 5.shell函式應用示例 1 兩個數字求和 要求 通過sum 定義函式 兩個數求和 方法一 root localhost vim d...

Shell程式設計

1 建立指令碼 vi emacs等即可 bin sh 2 shell變數 對shell來講,所有的變數的取值都是乙個字串 shell是一種解釋性語言,變數無需事先定義 shell中的系統變數 程式命令列引數的數目 儲存前乙個命令的返回值 0 當前程式名 以 1 2 形式儲存所有輸入的命令列引數 以 ...