linux 条件判断式
1.利用if ...then
if [ 判断条件 ];then
指令
fi
实例一 Y/N:
#!/bin/bash
#Program:
# This program shows "Hello World!" in your screen.
#History:
# // lzyer First release
read -p "Please input (Y/N)? " yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ];
then
echo "OK,continue."
exit
fi
if [ "${yn}" == "N"] || [ "${yn}" == "n" ];
then
echo "oh,interrupt!"
exit
fi
echo "I don't know what your choice is " && exit
实例二:
- 判断 $1 是否为 hello,如果是的话,就显示 "Hello, how are you ?";
- 如果没有加任何参数,就提示使用者必须要使用的参数下达法;
- 而如果加入的参数不是 hello ,就提醒使用者仅能使用 hello 为参数。
#!/bin/bash
#Program:
# check $ is equal to "hello"
#History:
#// lzyer First Release
if [ "${1}" == "hello" ];then
echo "Hi, how are you?"
elif [ "${1}" == "" ]; then
echo "you must input parameter"
else
echo "hello~~~"
fi
实例三 使用netstat 的指令查看服务的状态
#!/bin/bash
#Program:
# using netstat and grep to detect www.ssh,ftp and mail service.
#History:
#// lzyer First release
echo "Now, I will detect your Linux server' service!"
echo -e "The www,ftp,ssh and mail will be detect!"
testfile=/home/lzyer/test/bin/netstat_file.txt
netstat -tuln > ${testfile}
testing=$(grep ":80" ${testfile})
if [ "${testing}" != "" ];then
echo "www is running in your system."
fi
testing=$(grep ":22" ${testfile})
if [ "${testing}" != "" ];then
echo "ssh is running in your system."
fi
testing=$(grep ":21" ${testfile})
if [ "${testing}" != "" ];then
echo "ftp is running in your system."
fi
testing=$(grep ":25" ${testfile})
if [ "${testing}" != "" ];then
echo "mail is running in your system."
fi
实例四:计算退伍时间
#!/bin/bash
#Program:
# You input demobillization date, I calculate how many days before you demobilize.
#History:
#// lzyer First release
echo "this program will try to calculate:"
read -p "how many days before you demobillization date (YYYYMMDD>20170805): " date2
date_d=$(echo ${date2} | grep '[0-9]\{8\}')
if [ "${date_d}" == "" ];then
echo "You input the wrong date format..."
exit
fi
declare -i date_dem=$(date --date="${date2}" +%s)
declare -i date_now=$(date +%s)
declare -i date_total_s=$((${date_dem}-${date_now}))
declare -i date_d=$((${date_total_s}///))
if [ ${date_total_s} -lt ];then
echo "You had been demobilization before : " $((-*${date_d})) "day ago"
else
declare -i date_h=$((((${date_total_s}-${date_d}**))//))
echo "You will demobilization after ${date_d} days and ${date_h} hours."
fi
2.利用 case ..... esac 判断
实例一:
#!/bin/bash
#Program:
# show "hello" from $...by case esac
#History:
#// lzyer first release
case ${} in
"hello")
echo "hello,how are you?";;
"")
echo "please your input sth.";;
*)
echo "Usage ${0} hello";;
esac
3.利用 function 功能
#!/bin/bash
#Program:
# function use
#History:
#// lzyer first release
function print(){
echo "Your choice is ${1}"
}
echo "This program will print your selection !"
case ${} in
"one") print ;;
"two") print ;;
"three") print ;;
*) echo "Usage ${0} one|two|three";;
esac
linux 条件判断式的更多相关文章
- 第十三章、学习 Shell Scripts 条件判断式
利用 if .... then 单层.简单条件判断式 if [ 条件判断式 ]; then 当条件判断式成立时,可以进行的命令工作内容: fi <==将 if 反过来写,就成为 fi !结束 i ...
- 【重点】Shell入门教程:流程控制(3)条件判断式的真假值
之前曾提到,在Bash中什么是真什么是假,是以命令的结束状态是否为0来做判断.传回0,即为真:传回非0,即为假. 在Bash中,这种可以影响程序流程的式子,称为条件判断式.判断式的操作数分成“单元”及 ...
- 【shell】条件判断式
条件判断式的表示格式: 文件判断式: [root@andon ~]# [ -e /root/1 ] && echo yes || echo no #注意[]里面的空格,第一个命令为真打 ...
- shell编程 条件判断式----利用 case ..... esac 判断
条件判断式----利用 case ..... esac 判断 case $变量名称 in <==关键词为 case ,还有变量前有钱字号 "第一个变量内容") &l ...
- shell编程 条件判断式----利用 if .... then ----多重
条件判断式----利用 if .... then ----多重 在同一个数据的判断中,如果该数据需要进行多种不同的判断时,应该怎么作?举例来说,上面的 sh06.sh 脚本中,我们只要进行一次 $yn ...
- Linux 条件判断
1. 按照文件类型判断 -b 文件 #判断文件是否存在,并且是设备文件 -c 文件 #判断文件是否存在,并且是字符设备文件 -d 目录 #判断目录是否存在,并且是否为目录(是目录返回真) -e 文件 ...
- Shell学习笔记 - 条件判断式
1. 判断格式 1) test 参数 文件 例: test -e /root/install.log 2) [ 参数 文件 ] -- 推荐使用 例: [ -e /root/install.log ] ...
- linux条件判断:eq、ne、gt、lt、ge、le
-eq(equal) :判断是否相等,相等为真 -ne(inequality):判断是否不等,不等为真 -gt(greter than):判断是否大于,大于为真 -lt(less than):判断是否 ...
- Linux学习之第十九、条件判断
原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_4.php 条件判断式 只要讲到『程序』的话,那么条件判断式,亦即 ...
随机推荐
- kafka概述
kafka概述 Apache Kafka是一个开源 消息 系统,由Scala写成.是由Apache软件基金会开发的一个开源消息系统项目. Kafka最初是由LinkedIn开发,并于2011年初开源. ...
- hive表格取差集
hive 求两个集合的差集 业务场景是这样的,这里由两个hive表格A和B A的形式大概是这样的:uid B的形式大概是这样的:uid 我想要得到存在A中但是不存在B中的uid 具体代码如下 sele ...
- kivy学习三:打包成window可执行文件
根据官方文档写出如下内容,主要是为了记录自己遇到的坑! 一.打开命令行 1.win+r 2.输入CMD(没错,就是那个黑窗口就是命令行) 二.新建一个新文件夹,用来存放我们打包成的文件(这里一定要注意 ...
- SSH远程登录和端口转发详解
SSH远程登录和端口转发详解 介绍 SSH 是创建在应用层和传输层基础上的安全协议,为计算机上的 Shell(壳层)提供安全的传输和使用环境. SSH 只是协议,有多种实现方式,本文基于其开源实 ...
- [POJ 1004] Financial Management C++解题
参考:https://www.cnblogs.com/BTMaster/p/3525008.html #include <iostream> #include <cstdio> ...
- 涉及到大小变化,类似QScrollArea判断大小是否显示滚动条
涉及到大小变化,类似QScrollArea判断大小是否显示滚动条的情况要注意 这两个属性的设置:
- IDA动态调试SO文件
1. 所需工具 IDA Pro 6.6. 安卓SDK工具 2. 模拟器设置 将ida所在目录的dbgsrv文件夹内的android_server文件push到模拟器中. 设置777属性 启动调试服务器 ...
- 「日常训练」The Intriguing Obsession(CodeForces Round #439 Div.2 C)
2018年11月30日更新,补充了一些思考. 题意(CodeForces 869C) 三堆点,每堆一种颜色:连接的要求是同色不能相邻或距离必须至少3.问对整个图有几种连接方法,对一个数取模. 解析 要 ...
- C++学习009预处理器指令符号 # ## #@ 符号的使用
# ## #@ 符号是预处理器指令符号. 当预处理器遇到#指令符号时,会将#之后的部分用双引号括起来 当预处理去遇到##指令符号时,直接将##前后部分连接起来 当预处理器遇到#@指令符号,将#@之后的 ...
- 玩玩自动化测试之selenium篇
现如今社会科技发展太快了,纯功能点点点已经落后别人好几条街了,所以为了让自己多点职业生涯年限,得挺起肩,傲起头.自动化测试,其本质是用代码程序测试程序,所以其实第一步应该学好编程语言,后再自己开发自动 ...