shell 開發指南

2021-08-09 03:35:35 字數 3110 閱讀 2018

語法

釋義使用示例

bash_source[0]

取得當前執行的shell檔案所在的路徑

context="$(dirname "$")"

$?上一條指令執行完的退出狀態. 0為成功, 非0為失敗.

if [ $? -eq 0 ]; then

基本結構

if

[條件語句]

;then

執行語句1

else

執行語句2

fi注意條件語句外有 `

` `

(())

`兩種形式

常用的條件語句

# 字串型別比較

# 整型比較

# -eq 等於(equal)if[

"$a" -eq "$b"

]# -ne 不等於(not equal)if[

"$a" -ne "$b"

]# -gt 大於(greater than)if[

"$a" -gt "$b"]if

(("$a"

>

"$b"

))#(需要雙括號)

# -ge 大於等於if[

"$a" -ge "$b"]if

(("$a"

>=

"$b"

))# -lt 小於if[

"$a" -lt "$b"]if

(("$a"

<

"$b"

))# -le 小於等於if[

"$a" -le "$b"]if

(("$a"

<=

"$b"

))if

[$test_value

="true"];

thenif[

"$"==

"udp"];

thenif[

$? -eq 0 ]

;thenif[

$? -ne 0 ]

;thenif[

[$test_value -eq 1 ]];

thenif[

$test_result -ne 0 ]

;thenif[

[ -z "$"]]

;then

if[ -e "$/test"];

thenif[

! -f $];

then

if[ -n "$"];

thenif[

! -d $/test ]

;then

if中常用的判斷符含義

[ -a file ] 如果 file 存在則為真。

[ -b file ] 如果 file 存在且是乙個塊特殊檔案則為真。

[ -c file ] 如果 file 存在且是乙個字特殊檔案則為真。

[ -d file ] 如果 file 存在且是乙個目錄則為真。

[ -e file ] 如果 file 存在則為真。

[ -f file ] 如果 file 存在且是乙個普通檔案則為真。

[ -g file ] 如果 file 存在且已經設定了sgid則為真。

[ -h file ] 如果 file 存在且是乙個符號連線則為真。

[ -k file ] 如果 file 存在且已經設定了粘制位則為真。

[ -p file ] 如果 file 存在且是乙個名字管道(f如果o)則為真。

[ -r file ] 如果 file 存在且是可讀的則為真。

[ -s file ] 如果 file 存在且大小不為0則為真。

[ -t fd ] 如果檔案描述符 fd 開啟且指向乙個終端則為真。

[ -u file ] 如果 file 存在且設定了suid (set user id)則為真。

[ -w file ] 如果 file 如果 file 存在且是可寫的則為真。

[ -x file ] 如果 file 存在且是可執行的則為真。

[ -o file ] 如果 file 存在且屬有效使用者id則為真。

[ -g file ] 如果 file 存在且屬有效使用者組則為真。

[ -l file ] 如果 file 存在且是乙個符號連線則為真。

[ -n file ] 如果 file 存在 and has been mod如果ied since it was last read則為真。

[ -s file ] 如果 file 存在且是乙個套接字則為真。

[ file1 -nt file2 ] 如果 file1 has been changed more recently than file2, or 如果 file1 exists and file2 does not則為真。

[ file1 -ot file2 ] 如果 file1 比 file2 要老, 或者 file2 存在且 file1 不存在則為真。

[ file1 -ef file2 ] 如果 file1 和 file2 指向相同的裝置和節點號則為真。

[ -o optionname ] 如果 shell選項 「optionname」 開啟則為真。

[ -z string ] 「string」 的長度為零則為真。

[ -n string ] or [ string ] 「string」 的長度為非零 non-zero則為真。

[ string1 == string2 ] 如果2個字串相同。 「=」 may be used instead of 「==」 for strict posix compliance則為真。

[ string1 != string2 ] 如果字串不相等則為真。

[ string1 < string2 ] 如果 「string1」 sorts before 「string2」 lexicographically in the current locale則為真。

[ string1 > string2 ] 如果 「string1」 sorts after 「string2」 lexicographically in the current locale則為真。

Django Web開發指南

國內第一本django圖書 django web開發指南 歡迎使用django 歡迎來到django的世界,很高興能和你一起進行這趟旅程。你會發現有了這個強大的web框架,做每件事情都變得便捷起來 從設計開發新應用到不用大刀闊斧地修改 就能為現有 提供新的特性和功能。關於本書 市面上已經有了一些講解...

SQL Server 開發指南

sql server 資料庫設計 一 資料庫設計的必要性 二 什麼是資料庫設計 三 資料庫設計的重要 四 資料模型 實體 關係 e r 資料模型 實體 entity 屬性 attribute 關係 relationship 五 資料庫設計步驟 1 需求分析階段 2 概要設計階段 3 詳細設計階段 六...

linux epoll 開發指南

原文 關於epoll的問題很早就像寫文章講講自己的看法,但是由於ffrpc一直沒有完工,所以也就拖下來了。epoll主要在伺服器程式設計中使用,本文主要 伺服器程式中epoll的使用技巧。epoll一般和非同步io結合使用,故本文討論基於以下應用場合 epoll是為非同步io操作而設計的,epoll...