Shell循环语句:if

[[email protected] ~]# vim mkcdrom.sh

#!/bin/bash
DIR="/media/cdrom"
if [ ! -e $DIR ]
then
mkdir -p $DIR
fi



[[email protected] ~]# vim chkhost.sh
#!/bin/bash

ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $1 is on-line"
else
echo "Host $1 is off-line"
fi

[[email protected] ~]# bash chkhost.sh 192.168.1.11
Host 192.168.1.11 is on-line

注释:-c参数来规定尝试的次数,并使用-i参数定义每个数据包的发送间隔,以及使用-W参数定义等待超时时间



[[email protected] ~]# vim chkscore.sh
#!/bin/bash
read -p "Enter your score (0-100) :" GRADE
if [ $GRADE -gt 100 ];
then
echo "$GRADE is Error"
elif [ $GRADE -ge 85 ] && [ $GRADE -le 100 ];
then
echo "$GRADE is Excellent"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ];
then
echo "$GRADE is Pass"
else
echo "$GRADE is Fail"
fi
[[email protected] ~]# bash chkscore.sh
Enter your score (0-100) :200
200 is Error
[[email protected] ~]# bash chkscore.sh
Enter your score (0-100) :93
93 is Excellent
[[email protected] ~]# bash chkscore.sh
Enter your score (0-100) :80
80 is Pass
[[email protected] ~]# bash chkscore.sh
Enter your score (0-100) :23
23 is Fail

原文地址:https://blog.51cto.com/12400136/2460174

时间: 2024-08-30 03:17:54

Shell循环语句:if的相关文章

老男孩教育每日一题-第63天-批量创建用户并设置随机密码(要求不能使用shell循环语句)

题目:批量添加20个用户,用户名为user1~20,密码为5个随机字符(要求不能使用shell循环语句) 解决方法 方法1  echo user{1..20}|xargs -n1|sed -r 's#(.*)#useradd \1 \&\& echo \1 >>/tmp/passwd.txt \&\& echo $RANDOM |md5sum |cut -c 1-5>>/tmp/passwd.txt \&\& echo `tail -

[转帖]shell 循环语句for/do/done和while/do/done以及break,continue

shell 循环语句for/do/done和while/do/done以及break,continue https://blog.csdn.net/weixin_38280090/article/details/81843264 原创舌耳 发布于2018-08-19 22:58:17 阅读数 15072 收藏展开 for/do/doneShell脚本的for循环结构和C语言很不一样,它类似于某些编程语言的foreach循环.例如: #! /bin/sh for FRUIT in apple ba

Shell循环语句多种用法

for循环语句 列表形式有: 实例1 在命令中定义一系列的值 第一种写法: [[email protected] ~]# vim 1.sh #!/bin/bash for i in 1 2 3 4 //在命令中定义列表 do echo $i done [[email protected] ~]# sh 1.sh 1 2 3 4 第二种写法: [[email protected] ~]# vim 2.sh #!/bin/bash for i in {1..5} do echo $i done [[

shell 循环语句应用实例

1 for语句 语法格式 for 变量 in 值(或者循环条件) do 命令 done 给多个用户发送邮件 #!/bin/bash domain=163.com for user in tom hom jem do mail -s "happy new year" [email protected]$domain < /var/log/messages done 打印9*9的乘法口诀 #!/bin/bash for i in {1..9} do for ((j=1,j<=i

shell基础(八)-循环语句

国庆过后:感觉有点慵懒些了:接着上篇:我们继续来学习循环语句. 一. for循环 与其他编程语言类似,Shell支持for循环. for循环一般格式为: for 变量 in 列表 do command1 command2 ... commandN done 列表是一组值(数字.字符串等)组成的序列,每个值通过空格分隔.每循环一次,就将列表中的下一个值赋给变量 例如,顺序输出当前列表中的数字 for01.sh $ cat for01.sh #!/bin/sh for i in 1 2 3 4 5

shell脚本中用到的条件和循环语句

本博文介绍一下shell脚本中常用的条件和循环语句: 条件语句: 循环语句: 示例: if语句: eg1. eg2. 2.case语句: 简单的case语句: 配合循环的case语句: 3.for语句: 简单的for语句:eg1. eg2. 和case搭配的for语句:eg3. 4.while语句: eg1. eg2. 5.util语句: 6.select语句:

Shell中的循环语句实例

1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do     echo number $x done 注:"for" 循环总是接收 "in" 语句之后的某种类型的字列表.在本例中,指定了四个英语单词,但是字列表也可以引用磁盘上的文件,甚至文件通配符.实例1.2 #!/bin/bash for x in /var/log/* do     #echo "$x is a file

shell学习之for循环语句【初学者】

前言:日常系统管理工作中有大量需要重复运行的指令,shell编程提供了for.while.until.select循环语句以实现特定指令的反复执行功能,在所有的循环语句中,变量必须要有初始值,每次运行命令序列前都需要对条件进行过滤,满足条件才会运行命令,否则不执行相关操作.下面介绍的就是for循环语句的两种语法格式. for 循环每次处理依次列表内信息,直至循环耗尽.相对于while.until的循环方式是必须符合某个条件的状态,for这种语法是已经知道要进行几次循环的状态. 语法格式1: fo

Linux shell的条件判断、循环语句及实例

shell条件判断的两个特殊设备 /dev/null linux系统的空设备,也称为位桶,任何写入其中的数据均会被丢弃当你不想将标准化输出显示或者保存至文件时可以将文件从定向到/dev/null 禁止标准化输出 cat $filename > /dev/null 禁止标准化错误 rm $filename > /dev/null /dev/zero Linux的输入设备,可以用他初始化文件,可以无限制输出0, 另一个作用是用0去填充一个指定大小的文件 在条件判断语句中&&表示an