shell程式設計基礎 2 與

2021-09-08 03:48:57 字數 1725 閱讀 2069

shell 程式設計重要的應用就是管理系統,對於管理系統中成千上萬的程式而言,查詢某個檔名是否存在,並且獲取該檔名所指代檔案基本資訊是系統管理員的基本任務。shell命令可以很輕鬆的完成這項任務。

#program this is a example for ################

######### command test ################

read -p "type in the filename: " filename

test -z $filename &&echo

"you must put in a legal name"&&exit

0

//連續的&&表示命令是順序執行的,前乙個執行成功才能執行後乙個中間有任何乙個環節錯誤,則返回報錯資訊

test ! -e

$filename &&echo

"not exist" &&exit

0test -e

$filename &&echo

"exit"

這裡想實現實現的功能是,如果檔案不存在則退出script,本來想寫作

test -e

$filename&&echo

"exit"||echo

"not exit"&&exit

0

但是發現這樣無論filename是否存在,程式都會在這裡退出

因為如果檔案存在,則test傳回乙個0值,||判斷後執行echo "exit",然後又傳回乙個0值,&&判斷後執行exit 0

如果test 傳回乙個非0值,則||判斷後執行echo "not exit",傳回0值,&&判斷後還是執行exit 0;

如果寫成

test -e

$filename ||echo

"not exit"&&echo

"exit"

如果存在,test返回非0,執行echo "exit"

如果不存在,則test返回0,執行echo "not exit"

echo "not exit"又返回0,再執行echo "exit"

所以沒辦法在一條語句中判斷並推出。

test -f

$filename

&&filetype=

"file"

test -d

$filename

&&filetype=

"dictory"

test -r

$filename

&&perm=

" readable"

test -w

$filename

&&perm=$" writable"

test -x

$filename

&&perm=$" execuable"

/perm=$」__」表示在變數後補充___

echo

"file type is $filetype and the mod is $perm"

exit

2

shell程式設計 2

在shell裡,使用變數之前通常並不需要實現為他們做出宣告。預設情況下,所以變數都被看做字串來儲存,即使它們被賦值為數值也是如此。shell和一些工具程式會在需要時把數值型字串轉換為對應的數值以對它們進行操作。linux大小寫敏感。在shell中,我們可以通過在變數名前加乙個 符號來訪問它的內容。無...

shell程式設計 2

編寫shell程式,實現自動刪除50個賬號的功能。賬號名為stud1至stud50。程式實現及注釋如下 bin bash deluser.sh 考察while迴圈 i 1while i le 50 do 檢視賬戶是否存在 只需要在 etc passwd檔案查詢就可以了。我是利用了乙個管道符,再利用g...

Shell程式設計系列 基礎教程2

上一章節 是關於基礎命令ls chomd sudo pwd 的 前面的四個命令對於我們的日常的使用都有相當重要的作用,本期我們將檢視以下的幾個命令 移動檔案命令mv 也可以用作重新命名 複製檔案cp 在控制台顯示字串 echo 移動 重新命名檔案mv 移動檔案 重新命名檔案 mv a.txt b.t...