歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> shell腳本中的if..then..elif..then.if語句

shell腳本中的if..then..elif..then.if語句

日期:2017/3/3 15:54:08   编辑:關於Linux

第一種:

#!/bin/bash service vsftpd start &> /dev/null if

[ $? -eq 0 ]

then

echo "ftp is start"

else

service vsftpd start

fi

第二種:

#!/bin/bash read -p "input your file name " file_name

if [ -d $file_name ] //判斷是否為目錄

then

echo " this file is directory!"

elif [ -f $file_name ] //是否為文件

then

echo "this file is file"

elif [ -c $file_name -o -b $file_name ] //是否為字符設備或塊設備,符合其一就ok

then

echo "this is block file" else echo "this file unknown"

fi

[root@redhat shellscripts]# ll /dev/sda brw-rw----. 1 root disk 8, 0 Jun 1 10:05 /dev/sda

[root@redhat shellscripts]# ll /dev/tty crw-rw-rw-. 1 root tty 5, 0 Jun 1 10:05 /dev/tty

本欄目更多精彩內容:http://www.bianceng.cn/OS/Linux/

第三種:

#!/bin/bash

if [ $# -ne 2 ]

then

echo "Not enough parameters"

exit 0 //如果不是2個數,則跳出循環

fi

if [ $1 -eq $2 ]

then

echo "$1 = $2"

elif [ $1 -lt $2 ]

then

echo "$1 < $2"

fi

執行的結果:

[root@redhat shellscripts]# sh test.sh 1 1 1 = 1

[root@redhat shellscripts]# sh test.sh 1 2 1 < 2 ~

Copyright © Linux教程網 All Rights Reserved