中文字幕亚洲第一精品|精品国产免费一区二区|久久婷婷五月六月综合版|中文字幕熟妇久久久人妻|久久综合精品国产一区无码|国产成人精品永久免费视频|午夜亚洲国产精品理论片a级|久久精品一区二区三区无码护土

 訪問手機版  

Linux常用命令|Linux培訓(xùn)學(xué)習(xí)|考試認證|工資待遇與招聘,認準(zhǔn)超級網(wǎng)工!

招聘|合作 登陸|注冊

網(wǎng)絡(luò)工程師培訓(xùn)

當(dāng)前位置:網(wǎng)絡(luò)工程師 > 技術(shù)課程 > linux > 熱點關(guān)注 > linux常用命令

我使用過的Linux命令之if

時間:2018-09-07

linux命令行怎么打開_linux常用命令面試_linux命令

linux命令

Shell中的條件判斷語句,與其他編程語言類似。linux命令

如果需要知道有哪些條件判斷方式,通過man test就可以得到幫助。

if 條件; then

語句

fi

if 條件; then

語句

else

語句

fi

if 條件; then

語句

elif 條件; then

語句

fi

if 條件; then

語句

elif 條件; then

語句

else

語句

fi

if [ "foo" = "foo" ]; then
    echo expression evaluated as true
fi

[root@jfht ~]# if [ "foo" = "foo" ]; then

> echo expression evaluated as true

> fi

expression evaluated as true

[root@jfht ~]#

if [ "foo" = "foo" ]; then
    echo expression evaluated as true
else
    echo expression evaluated as false
fi

[root@jfht ~]# if [ "foo" = "foo" ]; then

> echo expression evaluated as true

> else

> echo expression evaluated as false

> fi

linux命令行怎么打開_linux常用命令面試_linux命令

expression evaluated as true

[root@jfht ~]#

T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
    echo expression evaluated as true
else
    echo expression evaluated as false
fi

[root@jfht ~]# T1="foo"

[root@jfht ~]# T2="bar"

[root@jfht ~]# if [ "$T1" = "$T2" ]; then

> echo expression evaluated as true

> else

> echo expression evaluated as false

> fi

expression evaluated as false

[root@jfht ~]#

示例四 判斷命令行參數(shù)數(shù)量

文件 if_4.sh

#!/bin/sh

if [ "$#" != "1" ]; then
    echo "usage: $0 <file>"
    exit 1
fi

[root@smsgw root]# cat if_4.sh

#!/bin/sh

if [ "$#" != "1" ]; then

echo "usage: $0 <file>"

exit 1

fi

[root@smsgw root]# chmod +x if_4.sh

[root@smsgw root]# ./if_4.sh

usage: ./if_4.sh <file>

[root@smsgw root]# ./if_4.sh hello

[root@smsgw root]#

if grep -q root /etc/passwd; then
    echo account root exists
else
    echo account root not exist
fi