加入收藏 | 设为首页 | 会员中心 | 我要投稿 佛山站长网 (https://www.0757zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

shell脚本中的if..then..elif..then.if语句

发布时间:2016-10-15 02:17:46 所属栏目: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

第一种:

#!/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 ~

(编辑:佛山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读