Shell脚本——for,while,until循环
1、for循环:
语句格式:
for i in 循环判断
do
循环体
done
举例:九九乘法表(for循环版本)
#!/bin/bash
# Author: Sean Martin
# Blog: https://www.cnblogs.com/shy13138/
# Time: 2019-08-16 10:35:48
# Name: 99for.sh
# Version: v1.0
for i in {1..9};do
for j in $(seq $i);do
echo -ne "$i*$j=$((i*j)) "
done
echo ''
done
2、while循环
语句格式:
while 循环判断
do
循环体
done
举例:猜拳游戏
#!/bin/bash
# Author: Sean Martin
# Blog: https://www.cnblogs.com/shy13138/
# Time: 2019-08-16 10:35:48
# Name: caiquan.sh
# Version: v1.0
j=1
while [ $j -le 5 ]
do
echo "1.石头 2.剪刀 3.布 "
read -p "请出拳1-3:" i
if [ $i -ne 1 -o $i -ne 2 -o $i -ne 3 ];then
echo "请输入1-3之间的数"
fi
game=(石头 剪刀 布)
num=$((RANDOM%3))
echo computer=${game[$num]}
case $i in
1)
if [ 0 -eq $num ];then
echo "平局"
elif [ 1 -eq $num ];then
echo "你输了"
else
echo "你赢了"
fi;;
2)
if [ 1 -eq $num ];then
echo "平局"
elif [ 0 -eq $num ];then
echo "你输了"
else
echo "你赢了"
fi;;
3)
if [ 2 -eq $num ];then
echo "平局"
elif [ 1 -eq $num ];then
echo "你输了"
else
echo "你赢了"
fi;;
esac
let j++
done
3、until循环
until循环与while循环类似
语句格式:
until 循环判断
do
循环体
done
举例:
99乘法表(until版)
#!/bin/bash
# Author: Sean Martin
# Blog: https://www.cnblogs.com/shy13138/
# Time: 2019-08-16 10:35:48
# Name: 99until.sh
# Version: v1.0
i=1
until [[ $i -gt 9 ]]
do
j=1
until [[ $j -gt $i ]]
do
let "sum = $i*$j"
echo -n -e "$i*$j=$sum\t"
let "j++"
done
echo ""
let "i++"
done
Shell脚本——for,while,until循环的更多相关文章
- shell脚本——作业二(循环作业)
1.通过位置变量创建linux系统账户及密码 $1 是执行脚本的第一个参数,$2 是执行脚本的第二个参数 #!/bin/bash #创建用户与密码 declare -i c=0 if [ -z $1 ...
- shell脚本判断语句和循环语句
if判断语句 exit跳出判读语句 不加exit的结果 read -n(不换行) 判断是否输入的是数字 read age[[ $age =~ ^[0-9]+$ ]]if [ $? -ne 0 ]; t ...
- Shell脚本之:退出循环
Shell也使用 break 和 continue 来跳出循环. break命令 下面的例子中,脚本进入死循环直至用户输入数字大于5,使用break跳出这个循环. #!/bin/bash while ...
- Shell脚本之七 选择、循环结构
一.if else if 语法格式 if condition then command1 command2 ... commandN fi 写成一行(适用于终端命令提示符): if [ $(ps -e ...
- shell脚本之for 列表循环
作用:对列表进行循环处理 语法: for var in list do commands done 案例: 1.读取列表中的值 2.读取列表中的复杂值 异常案例:未显示出“'”单引号,使语句出现异常 ...
- shell脚本学习之for循环
1.数字循环 [root@zabbix ~]# for i in {1..10};do echo $(expr $i \* 3);done;36912151821242730 [root@zabbix ...
- 用shell脚本写一个for循环
一.输出十遍北京 for((i=1;i<10;i++))> do> echo '北京';> done 二.死循环 for((;;))do#java -jar producer. ...
- 在shell脚本中进行条件控制以及使用循环
转载请标明:http://www.cnblogs.com/winifred-tang94/ if条件语句语法: if [ 条件表达式 ] then 代码 else 代码 fi 注意:在上面的if条件语 ...
- Linux shell脚本编程(三)
Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...
- shell 脚本之循环使用 for while 详解
任何一种编程语言中循环是比不可少的,当然 shell 脚本也少不了循环语句,包括 for 语句. while 语句.文中主要以实际用例来说明 for while 都有哪些常见的使用方法和技巧. 一.f ...
随机推荐
- The working copy is locked due to a previous error.
SVN报错: The working copy is locked due to a previous error. 不能更新项目代码... 解决方式: 解决:右键你的左侧管理目录中的相关目录,然后点 ...
- 【JVM学习笔记】扩展类加载器
扩展类加载器独有的特点,代码如下 public class Sample { } public class Test { static { System.out.println("Test ...
- Docker Swarm常用命令
#查看集群节点 docker node ls #创建nginx服务 #docker pull hub.test.com:5000/almi/nginx:0.1 #下载私有仓库镜像 docker ser ...
- androidstudio出包问题--Warning: there were 1 unresolved references to classes or interfaces.
问题:存在unresolved的类或接口导致打包失败 Warning: there were 1 unresolved references to classes or interfaces. You ...
- Crunch黑客神器-创造个性字典
先来看第一个命令: crunch 6 7 123456 -o pass.txt 是什么意思呢?我们打开终端,输入这个命令之后,crunch代表使用crunch这个工具,6代表生成的密码最小是6位数,7 ...
- MariaDB知识点总结03--从主+多主集群
一.从主架构 1.从主复制原理 从库生成两个线程,一个I/O线程,一个SQL线程: i/o线程去请求主库 的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中:主库会生 ...
- 2019-2020 ICPC, Asia Jakarta Regional Contest C. Even Path
Pathfinding is a task of finding a route between two points. It often appears in many problems. For ...
- bzoj 4736: 温暖会指引我们前行 (LCT 维护最大生成树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4736 题面: 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出 ...
- [转帖]RedHat 如何更改网卡名 从ens192 改为eth0的问题
RedHat 如何更改网卡名 从ens192 改为eth0的问题 2017年03月27日 17:50:47 the_conquer_zzy 阅读数 2416 版权声明:本文为博主原创文章,遵循CC ...
- django CBV装饰器 自定义django中间件 csrf跨站请求伪造 auth认证模块
CBV加装饰器 第一种 @method_decorator(装饰器) 加在get上 第二种 @method_decorator(login_auth,name='get') 加在类上 第三种 @met ...