Line 1 > Line 2 > Line 3 > Line 4 > Line 5 > EOF Line 1 Line 2 Line 3 Line 4 Line 5 還可以使用制表符讓 shell 腳本中的內(nèi)容更整潔一點(diǎn),這只需要在 # cat >Line 1 >Line 2。對話 UNIX: !$#@*%( 四 )。" />

日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

對話 UNIX: !$#@*%( 四 )


# cat << EOF
> Line 1
> Line 2
> Line 3
> Line 4
> Line 5
> EOF
Line 1
Line 2
Line 3
Line 4
Line 5
還可以使用制表符讓 shell 腳本中的內(nèi)容更整潔一點(diǎn),這只需要在 << 和終止標(biāo)識(shí)符之間放上一個(gè)連字符(-):
# cat <<- ATC
>Line 1
>Line 2
>Line 3
>Line 4
>Line 5
> ATC
Line 1
Line 2
Line 3
Line 4
Line 5
清單 7 給出的示例演示如何結(jié)合使用本文到目前為止討論的東西 。
清單 7. 組合 CLI
# cat redirect_example
#!/usr/bin/ksh
cat <<- ATC | sed "s/^/Redirect Example => /g" >> atc.out
This is an example of how to redirect
stdout to a file as well as pipe stdout into stdin
of another command (i.e. sed), all done inside
a here-document.
Cool eh?
ATC
現(xiàn)在,看看關(guān)于重定向和管道的腳本 。
# ./redirect_example
# cat atc.out
Redirect Example => This is an example of how to redirect
Redirect Example => stdout to a file as well as pipe stdout into stdin
Redirect Example => of another command (i.e. sed), all done inside
Redirect Example => a here-document.
Redirect Example =>
Redirect Example => Cool eh?
子 shell
有時(shí)候,需要一起執(zhí)行幾個(gè)命令 。例如,如果希望在另一個(gè)目錄中執(zhí)行某一操作,可以使用 清單 8 中的代碼 。
清單 8. 同時(shí)執(zhí)行幾個(gè)命令
# pwd
/home/cormany
# cd testdir
# tar –cf ls_output.tar ls.out?
# pwd
/home/cormany/testdir
這是有效的,但是要注意,在執(zhí)行這些步驟之后,您就不再位于原來的目錄中了 。通過把這些命令放在它們自己的子 shell 中,它們會(huì)作為子 shell 的實(shí)例執(zhí)行 。清單 9 演示如何使用子 shell 執(zhí)行相同的代碼 。
清單 9. 使用子 shell 同時(shí)執(zhí)行幾個(gè)命令
# pwd
/home/cormany
# (cd testdir ; tar -cf ls_output.tar ls.out?)
# pwd
/home/cormany
test 命令、[ ] 和 [[ ]]
在編寫 shell 腳本或用任何現(xiàn)代語言編寫程序時(shí),運(yùn)算表達(dá)式或值的能力都很重要 。Unix 一直通過 test 命令提供這一功能 。正如 test 的手冊頁指出的,test 命令運(yùn)算表達(dá)式參數(shù)的值,如果表達(dá)式的值是 True,就返回零(True)退出值 。關(guān)于 test 的定義和所有可用條件的更多信息,請參見 test 手冊頁 。
要想使用 test 命令,只需給這個(gè)命令提供適當(dāng)?shù)臉?biāo)志和文件名 。當(dāng) test 運(yùn)算完表達(dá)式時(shí),返回到命令提示,可以在這里檢查返回碼,見 清單 10 。
清單 10. 檢查返回碼
# ls –l
-rwxr-xr-x1 cormany atc 786 Feb 22 16:11 check_file
-rw-r--r--1 cormany atc0 Aug 04 20:57 emptyfile
# test -f emptyfile
# echo $?
0
# test -f badfilename
# echo $?
1
根據(jù)定義,如果表達(dá)式值是 True,那么 test 返回零退出值,否則返回非零退出值(即 1) 。在 清單 10 中,找到了文件 emptyfile,所以 test 返回 0;但是沒有找到文件 badfilename,所以返回 1 。
使用 test 的另一種方法是把要運(yùn)算的表達(dá)式放在單層方括號(hào)([ ])中 。使用 test 命令或把它替換為 [ ] 會(huì)返回相同的值:
# [ -f emptyfile ]
# echo $?
0
# [ -f badfilename ]
# echo $?
1
使用單層方括號(hào)([ ])還是雙層方括號(hào)([[ ]])是個(gè)人習(xí)慣問題,實(shí)際上取決于您如何學(xué)習(xí)命令和 shell 腳本編程 。但是請記住,這兩者之間有一些差異 。盡管 [ ] 和 [[ ]] 在運(yùn)算期間使用相同的測試操作符,但是它們使用不同的邏輯操作符 。
操作符
在 ksh(AIX 中使用的默認(rèn) shell)中,以及 Unix 和 Linux 使用的其他 shell 中,一定要知道如何使用測試、邏輯和替換操作符 。

推薦閱讀