您的位置:首页 > 运维架构 > Shell

Shell Step by Step (3) —— Stdin & if

2015-10-01 18:00 501 查看
4.输入输出

#! /bin/bash
# Read users input and then get his name
read -p "Please input your first name: " firstName
read -p "Please input your last name: " lastName
echo -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 ……


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: