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 ...
随机推荐
- haproxy-负载均衡介绍
参考:http://www.iyunv.com/thread-252539-1-1.html 负载均衡介绍 四层和七层负载均衡的区别 四层 所谓的四层就是ISO参考模型中的第四层.四层负载均衡也称为四 ...
- pyinstaller发布exe,弹出Failed to execute script main
1.在PyCharm中按Alt+F12打开Terminal对话框 1.1我的项目文件放在wxpython目录下,D:\learn\Weather index insurance\wxpython> ...
- Python 常用模块(1) -- collections模块,time模块,random模块,os模块,sys模块
主要内容: 一. 模块的简单认识 二. collections模块 三. time时间模块 四. random模块 五. os模块 六. sys模块 一. 模块的简单认识 模块: 模块就是把装有特定功 ...
- go http server 编程
第一种:最简单的 package main import ( "fmt" "log" "net/http" ) func myHandler ...
- 数据库工具DbVisualize安装、破解教程,亲测可用
之前工作中遇到生产环境不允许导入Oracle的dmp文件,只能导入sql脚本,但是表中存在clob字段,直接用plsql工具无法导出clob字段,用了下dbvisualizer可以直接导出,亲测可用. ...
- C语言Ⅰ博客作业10
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/10097 我在这个课程的 ...
- POJ3450最长公共子串【kmp】
题目链接:http://poj.org/problem?id=3450 题目大意:给定n个长度不超过200的字符串,n < 4000.求这些字符串的最长公共子串,若没有,则输出 “IDENTIT ...
- provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.
通常情况下,要解决这个,你去SQL Server配置管理器(SSCM)和: [1]在SSCM中设置 [1.1]确保共享内存协议启用 [1.2]确保命名管道协议 [1.3]确保TCP / IP被启用,和 ...
- python -- TypeError: 'module' object is not callable
文件: 代码: import pprintmessge = 'It was a bringht cold day in April,and the clocks were striking thrir ...
- Springmvc使用注解实现执行定时任务(定时器)
1.在Spring配置文件中添加 <task:annotation-driven/> 2.在需要调用执行定时任务的类中使用注解 @Service @Lazy(false) //避免spri ...