《shell脚本if..then..elif..then.if语句的总结》
2014-06-01 12:17
525 查看
第一种:
#!/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 ~
#!/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 ~
相关文章推荐
- shell脚本:使用if-then语句和test命令
- Shell脚本语法-- if/then/elif/else/fi
- Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif
- Shell脚本语法--if/then/elif/else/fi
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本基础5-if-then语句
- shell 中 if 语句内判断参数总结
- shell脚本之判断 if,elif, case
- 对shell脚本里if语句的一点剖析
- Shell if 语句总结
- 书写优雅的shell脚本(一)- if语句
- shell 中的 if-elif-else 语句使用实例
- shell 脚本 if 语句
- shell脚本编程之for语句、if语句使用介绍
- shell 脚本编写 if else then
- if嵌套语句 shell脚本实例 测试是否闰年
- 总结:工作中用到的SHELL脚本、shell语句
- shell脚本编程之for语句、if语句使用介绍