標籤:shell stdin if bash file
4.輸入輸出
#! /bin/bash# Read users input and then get his nameread -p "Please input your first name: " firstNameread -p "Please input your last name: " lastNameecho -e "Your full name is: $firstName $lastName"
read用法:
read [-ers] [-a 數組] [-d 分隔字元] [-i 緩衝區文字] [-n 讀取字元數] [-N 讀取字元數] [-p 提示符] [-t 逾時] [-u 檔案描述符] [名稱 ...]
5.條件判別式
-e 是否存在-nt (newer than) file1 是否比 file2 新-ot (older than) file1 是否比 file2 舊-ne 表示 不等於-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"))> 大於(需要雙括弧),如:(("$a" > "$b"))>= 大於等於(需要雙括弧),如:(("$a" >= "$b"))-z (zero) 是否為零
[ -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則為真。 [ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.
e.g:
if [ "$UID" -ne "$ROOT_UID" ] then ……
熱門推薦
LNMP環境搭建——Nginx篇
在RHEL6.5中配置本地YUM源
Ubuntu下Zabbix安裝及使用問題
MySQL雙主熱備問題處理
Shell Step by Step (3) —— Stdin & if