《shell脚本if..then..elif..then.if语句的总结》

第一种:

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

 

第三种:

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

posted @   Linux.应用  阅读(12864)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示