shell之()、{}

來源:互聯網
上載者:User

標籤:shell

1 一串命令的執行

[[email protected] log]# (a=2;echo $a)

2

[[email protected] log]# { a=2;echo $a;}    #注意格式的不同,左邊有空格,最右邊以分號結尾

2


2 ()和{}中括弧裡面的某個命令的重新導向隻影響該命令,但括弧外的重新導向則影響到括弧裡的所有命令

[[email protected] log]# { a=10;b=100;echo $a>c;echo $b;}

100

[[email protected] log]# cat c

10


[[email protected] ~]# { a=10;b=100;echo $a;echo $b;}>c

[[email protected] ~]# cat c

10

100


3 ()只是對一串命令重新開一個子shell進行執行,{}對一串命令在當前shell執行

[[email protected] ~]# a=100

[[email protected] ~]# (a=10;echo $a)

10

[[email protected] ~]# echo $a                #值沒有被修改,說明()中命令在子shell中實現 

100


[[email protected] ~]# a=100

[[email protected] ~]# { a=10;echo $a;}

10

[[email protected] ~]# echo $a                #值已經被修改,說明{}中命令在當前shell中實現

10


4 $()和${}

命令替換:

[[email protected] ~]# echo $(ls)

anaconda-ks.cfg a.py a.sh c install.log install.log.syslog moban.sh secure test.txt

變數的實現:

[[email protected] ~]# a=1000

[[email protected] ~]# echo ${a}

1000


5 幾種特殊的替換結構

${var:-string},${var:+string},${var:=string},${var:?string}

1)若變數var為空白或者未定義,則用在命令列中用string來替換${var:-string},否則變數var不為空白時,則用變數var的值來替換${var:-string}

[[email protected] ~]# echo $var


[[email protected] ~]# echo ${var:-bcc}

bcc

[[email protected] ~]# var=1000

[[email protected] ~]# echo ${var:-bcc}

1000


2)對於${var:=string}的替換規則和${var:-string}是一樣的,所不同之處是${var:=string}若var為空白時,用string替換${var:=string}的同時,把string賦給變數var:

[[email protected] ~]# echo $var


[[email protected] ~]# echo ${var:=bbc}

bbc

[[email protected] ~]# echo $var

bbc


3)${var:+string}:只有當var不是空的時候才替換成string,若var為空白時則不替換或者說是替換成變數var的值,即空值


[[email protected] ~]# var=100

[[email protected] ~]# echo $var

100

[[email protected] ~]# echo ${var:+bbc}

bbc

[[email protected] ~]# echo $var

100

[[email protected] ~]# unset var

[[email protected] ~]# echo $var


[[email protected] ~]# echo ${var:+bbc}


4)${var:?string}:若變數var不為空白,則用變數var的值來替換${var:?string},若變數var為空白,則把string輸出到標準錯誤中,並從指令碼中退出。可利用此特性來檢查是否設定了變數的值

[[email protected] ~]# echo $var


[[email protected] ~]# echo ${var:?bbc}

-bash: var: bbc

[[email protected] ~]# var=100

[[email protected] ~]# echo ${var:?bbc}

100


6 匹配和替換:

*    萬用字元,任意數目的任一字元

#    去掉左邊

%    去掉右邊

單一符號是最小匹配;兩個符號是最大匹配


樣本:

[[email protected] ~]# var=/var/log/messages       

[[email protected] ~]# echo ${var:3:2}                 #取出從第3個字元開始,向後2個字元

r/

[[email protected] ~]# echo ${var:0:3}                 #取出前3個字元

/va


[[email protected] ~]# var=/var/log/messages

[[email protected] ~]# echo ${var/log/LOG}             #把log替換成LOG

/var/LOG/messages

[[email protected] ~]# echo ${var//s/$}                #兩個符號是最大匹配,全部替換

/var/log/me$$age$


[[email protected] ~]# var=/var/log/messages

[[email protected] ~]# echo ${var#*/}                  #從左邊開始,刪除第一個匹配*/及其左邊的

var/log/messages                                    字元

[[email protected] ~]# echo ${var#*m}

essages

[[email protected] ~]# echo ${var##*/}                #從左邊開始,刪除最後一個匹配*/及其左邊

messages                                           的字元

[[email protected] ~]# echo ${var%/*}                 #從右邊開始,刪除第一個匹配/*及其右邊的

/var/log                                           字元


本文出自 “一萬年太久,只爭朝夕” 部落格,請務必保留此出處http://zengwj1949.blog.51cto.com/10747365/1922976

shell之()、{}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.