shell之流程控制
一、if语句、while、for循环各种小例题
1.用户验证
[root@bogon ~]# cat buer.sh
#!/bin/bash #user='buer'
#password='1234.com' read -p 'please your name:' user
read -p 'password:' passwd if [ $user = 'buer' -a $passwd = '1234.com' ];then
echo 'login successful'
fi
2.猜年龄
#!/bin/bash age=57
read -p 'num:' num if [ $num -eq $age ];then
echo 'you get it'
elif [ $num -gt $age ];then
echo 'too big'
elif [ $num -lt $age ];then
echo 'too samll'
fi
3.查询成绩
[root@bogon ~]# cat score.sh
#!/bin/bash read -p 'please your score:' score if [ $score -ge 90 ];then
echo 'very good!'
elif [ $score -ge 70 -a $score -lt 90 ];then
echo 'good!'
elif [ $score -ge 60 -a $score -lt 70 ];then
echo 'just like'
else
echo 'poor'
fi
4.for循环打印1235
[root@bogon ~]# cat for.sh
#!/bin/bash for i in {1..5}
do
if ((i == 4));then
continue
#break
fi
echo '=======' $i
done
脚本4的执行结果:
[root@bogon ~]# ./for.sh
======= 1
======= 2
======= 3
======= 5
5.改进版的猜年龄
[root@bogon ~]# cat oldboy.sh oldboy=57
while :
do
read -p 'input your age:' age
if [ -z $age ];then
continue
fi
if [ $age -eq $oldboy ];then
echo "bingo"
break
elif [ $age -lt $oldboy ];then
echo "little"
elif [ $age -gt $oldboy ];then
echo "big"
else
echo "bye"
fi
done
脚本5结果:
[root@bogon ~]# ./oldboy.sh
input your age:45
too samll
input your age:65
too big
input your age:57
you get it
[root@bogon ~]#
6.查看文件类型
[root@bogon ~]# cat aa.sh
#!/bin/bash while :
do
read -p 'input your file:' file
if [ -z $file ];then
continue
elif [ $file = 'quit' ];then
break
fi
if [ -b $file ];then
echo "$file is block file"
elif [ -d $file ];then
echo "$file is directory file"
elif [ -f $file ];then
echo "$file is regular file"
else
echo "$file unkown"
fi
done
脚本6结果:
[root@bogon ~]# sh aa.sh
input your file:/etc
/etc is directory file
input your file:/pm
/pm unkown
input your file:quit
7.打印乘法口诀表
[root@bogon ~]# cat cheng.sh
#!/bin/bash for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "$i*$j=$[$i*$j]"
done
echo
done
脚本7结果:
[root@bogon ~]# ./cheng.sh
1*1=1
2*1=22*2=4
3*1=33*2=63*3=9
4*1=44*2=84*3=124*4=16
5*1=55*2=105*3=155*4=205*5=25
6*1=66*2=126*3=186*4=246*5=306*6=36
7*1=77*2=147*3=217*4=287*5=357*6=427*7=49
8*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=64
9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81
8.批量创建用户
[root@bogon ~]# cat add.sh
#!/bin/bash for i in {1..5};
do
useradd user$i && echo "user$i create successful"
done
脚本8执行结果:
[root@bogon ~]# sh add.sh
user1 create successful
user2 create successful
user3 create successful
user4 create successful
user5 create successful
9.批量删除用户
[root@bogon ~]# cat del.sh
#!/bin/bash for i in {1..5};
do
userdel -r user$i && echo "user$i delete successful"
done
脚本9执行结果:
[root@bogon ~]# sh del.sh
user1 delete successful
user2 delete successful
user3 delete successful
user4 delete successful
user5 delete successful
10.循环叠加打印数字,并睡眠1s
[root@bogon ~]# cat conut.sh
#!/bin/bash count=1
while ((count < 10))
do
echo '==========>' $count
sleep 1
((count+=1))
done
执行结果:
[root@bogon ~]# sh conut.sh
==========> 1
==========> 2
==========> 3
==========> 4
==========> 5
==========> 6
==========> 7
==========> 8
==========> 9
11.批量ping ip
[root@bogon ~]# cat ping.sh
#!/bin/bash for i in {1..100};
do
ping -c1 192.168.16.$i &> /dev/null
if [ $? -eq 0 ];then
echo "192.168.16.$i OK"
echo "192.168.16.$i" >> /tmp/ip.txt
fi
done
12.模仿登录用户小工具
[root@bogon ~]# cat login.sh
#!/bin/bash user='buer'
passwd='1234.com'
while $tag
do
read -p 'input your name:' name
read -p 'input your passwd:' pas
if [[ $name = $user ]] && [[ $pas = $passwd ]];then
echo 'login successful'
while $tag
do
read -p '>>:' cmd
if [[ $cmd = 'quit' ]];then
tag=false
else
$cmd
fi
done
fi
done
执行结果:
[root@bogon ~]# sh login.sh
input your name:buer
input your passwd:1234.com
login successful
>>:ls
aa.sh a.txt del.sh oldboy.sh 模板 下载
add.sh buer.sh for.sh ping.sh 视频 音乐
age.sh cheng.sh initial-setup-ks.cfg score.sh 图片 桌面
anaconda-ks.cfg conut.sh login.sh 公共 文档
>>:pwd
/root
>>:quit
[root@bogon ~]#
shell之流程控制的更多相关文章
- shell编程--流程控制for,do-while,if-then,break,continue,case等
2.5 流程控制 2.5.1 if语法 1.语法格式 if condition then statements [elif condition then statements. ..] ...
- Shell工具| 流程控制
1. 流程控制 if 判断 ()[ 条件判断式 ],中括号和条件判断式之间必须有空格 ()if后要有空格 [kris@hadoop datas]$ vim if.sh #!/bin/bash -eq ...
- shell编程流程控制
前言: 在linux shell中,通常我们将一些命令写在一个文件中就算是一个shell脚本了,但是如果需要执行更为复杂的逻辑判断,我们就需要使用流程控制语句来支持了. 所谓流程控制既是通过使用流程控 ...
- 10、shell编程+流程控制+分支嵌套
SHELL 编程 shell 是一个命令解释器,侦听用户指令.启动这些指令.将结果返回给用户(交互式的shell) shell 也是一种简单的程序设计语言.利用它可以编写一些系统脚本. ...
- linux shell 之流程控制 if if else while
(1)流程控制不可以为空: (2)if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi 条件 ...
- shell 10流程控制
if 判断 if #shell #!/bin/sh a=5 if [[ $a > 3 ]];then echo "$a>3" fi #写成一行 if [[ $a < ...
- Linux学习 -- Shell编程 -- 流程控制
if语句 单分支 if [ 条件判断式 ]; then 程序 fi 或者 if [ 条件判断式 ] then 程序 fi 例子: 双分支 if [ 条件判断式 ] then 程序 else 程序 fi ...
- linux shell编程流程控制
条件选择 单分支条件 多分支条件 选择执行if语句 单分支 if 判断条件;then 条件为真的分支代码 fi 双分支 if 判断条件; then 条件为真的分支代码 else 条件为假的分支代码 f ...
- Shell命令和流程控制
Shell命令和流程控制 在shell脚本中可以使用三类命令: 1)Unix 命令: 虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令.这些命令通常是用来进行文件和文字 ...
随机推荐
- EIP权限工作流平台总结-1总体说明
预览地址:www.eipflow.com (1) 权限工作流:www.demo.eipflow.com/Account/Login (2) 基础权限版:www.auth.eipflow.com/A ...
- luogu4238 【模板】多项式求逆
ref #include <iostream> #include <cstdio> using namespace std; typedef long long ll; int ...
- 多表头的DataGridView
上次在程序中要用到多表头的DataGridView,在网上搜索了一个,感觉还不错,现在简单的介绍一下它的用法.首先得把这个dll拷贝到相应的目录下,dll名称是myMultiColHea ...
- 白话HMM系列1——从一个缩略语还原的例子说起
HMM到底是一个什么样的东西,我想从我研究的一个应用场景开始说起.之所以想重新描述一下我对HMM的理解,是因为上次面试百度糯米的时候,自己没有把HMM在应用上说的很明白,不过糯米的那位郑姓面试官我也是 ...
- android service笔记
1.service 默认在主线程运行,所以不能在service中直接做访问网络,操作文件等耗时操作,要另外开启线程 2.通过startservice开启的服务,一旦服务开启,这个服务和开启他的调用者之 ...
- 标志寄存器PSW和汇编条件转移指令解释
标志寄存器PSW 标志寄存器PSW(程序状态字寄存器PSW) 标志寄存器PSW是一个16为的寄存器.它反映了CPU运算的状态特征并且存放某些控制标志.8086使用了16位中的9位,包括6个状态标志 ...
- Mac下用tomcat搭建下载服务器
1.下载tomcat 去官方网址: http://tomcat.apache.org/ 下载最新版 2.下载解压后,自己可以随便放在哪个文件夹下,自己记得路径即可.比如Users/你的用户名/Docu ...
- web3无法安装的额解决方案-----yarn命令安装web3
凡是可以用 JavaScript 来写的应用,最终都会用 JavaScript 来写. --Atwood定律(Jeff Atwood在2007年提出) yarn命令详解 https://yarnpkg ...
- BZOJ 4595 SHOI2015 激光发生器 射线,线段,偏转
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4595 题意概述: 给出一条射线和N条线段,射线遇到线段会发生反射,令入射角alpha,出射 ...
- Java基础——集合
java的集合类是一个工具类,存放在java.util包中.它不仅可以存储对象,也可以实现常用数据结构,如栈.队列等.严格的说,集合类存放的是对象的引用,而不是对象本身. java集合主要由这两个接口 ...