Linux Shell脚本编程while语句案例
1,每隔3秒,打印一次系统负载
#!/bin/bash while true
do
uptime
sleep
done
2,把监控结果保存到文件,在后台执行,然后用tail -f监控文件变化
ghostwu@dev:~/linux/shell/flow_control$ sh while.sh &
[]
#!/bin/bash while true
do
uptime >> log.txt
sleep
done
ghostwu@dev:~/linux/shell/flow_control$ tail -f log.txt
:: up min, user, load average: 0.33, 0.35, 0.32
:: up min, user, load average: 0.33, 0.35, 0.32
:: up min, user, load average: 0.31, 0.34,
...
3,进程调度相关命令
fg: 把当前脚本或者任务放到前台执行。如果指定某个任务:fg 任务编号。 任务编号通过jobs查询
bg: 把任务放到后台执行
jobs:查看当前执行的脚本或者任务
ctrl+z:暂停执行当前的脚本
sh while1.sh & : 加上&,表示后台执行脚本
ghostwu@dev:~/linux/shell/flow_control$ fg
sh while.sh
^Z
[]+ Stopped sh while.sh
ghostwu@dev:~/linux/shell/flow_control$ jobs
[]+ Stopped sh while.sh
ghostwu@dev:~/linux/shell/flow_control$ bg
[]+ sh while.sh &
ghostwu@dev:~/linux/shell/flow_control$ jobs
[]+ Running sh while.sh &
ghostwu@dev:~/linux/shell/flow_control$ sh while.sh &
[]
ghostwu@dev:~/linux/shell/flow_control$ jobs
[]- Running sh while.sh &
[]+ Running sh while.sh &
ghostwu@dev:~/linux/shell/flow_control$ fg
sh while.sh
^Z
[]+ Stopped sh while.sh
ghostwu@dev:~/linux/shell/flow_control$ bg
[]+ sh while.sh &
ghostwu@dev:~/linux/shell/flow_control$ jobs
[]- Running sh while.sh &
[]+ Running sh while.sh &
4,用while循环打印0, 1, 2, 3, 4
#!/bin/bash
i=
while [ $i -lt ]
do
echo $i
(( i++ ))
done
两个中括号也可以
#!/bin/bash
i=
while [[ $i -lt ]]
do
echo $i
(( i++ ))
done
还可以用计算表达式
#!/bin/bash
i=
while (( i < ))
do
echo $i
(( i++ ))
done
5,计算1....100的和
ghostwu@dev:~/linux/shell/flow_control$ sh sum.sh
++..+=
ghostwu@dev:~/linux/shell/flow_control$ cat sum.sh
#!/bin/bash i=
sum=
while (( i <= ))
do
(( sum = sum + i ))
(( i++ ))
done
echo "1+2+3..+100="${sum}
6,猜数字
#!/usr/bin/bash sum=$((RANDOM%)) echo "需要你猜的数是:"$sum sleep echo "请输入1-50之间的数,开始猜吧!" count= function type_num(){
read -p "请输入一个数吧:" n
expr $n + &>/dev/null
if [ $? -ne ]; then
echo "请输入一个数字"
type_num
fi
} function guess(){
(( count++ ))
if [ $n -eq $sum ]; then
echo "你猜中了,你的次数是:"${count}
if [ $count -lt ]; then
echo "你太厉害了"
elif [ $count -ge -a $count -lt ]; then
echo "还是不错的,加油"
else
echo "你有点水啊"
fi
exit
elif [ $n -gt $sum ]; then
echo "猜大了"
type_num
else
echo "猜小了"
type_num
fi
} function main(){
type_num
while true
do
guess
done
} main
Linux Shell脚本编程while语句案例的更多相关文章
- Linux Shell脚本编程while语句
Linux Shell脚本编程while语句案例 1,每隔3秒,打印一次系统负载 #!/bin/bash while truedo uptime sleep 3done 2,把监控结果保存 ...
- Linux shell脚本编程if语句的使用方法(条件判断)
if 语句格式if 条件then Commandelse Commandfi 别忘了这个结尾If语句忘了结尾fitest.sh: line 14: syntax error: unex ...
- Linux shell脚本编程(三)
Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...
- Linux shell脚本编程(二)
Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- Linux Shell脚本编程--Linux特殊符号大全
Linux Shell脚本编程--Linux特殊符号大全 linux_shell 特殊符号的介绍 2011
- Linux Shell脚本编程-基础1
概述: shell脚本在Linux系统管理员的运维工作中非常重要.shell脚本能够帮助我们很方便的管理服务器,因为我们可以指定一个任务计划,定时的去执行某一个脚本以满足我们的需求.本篇将从编程基础 ...
- 【学习】Linux Shell脚本编程
1.脚本的组成和执行 Linux shell脚本的结构并不复杂,其主要由变量.内部命令以及shell的语法结构和一些函数.其他命令行的程序等组成,以下是一个简单的shell脚本. #!/bin/bas ...
- Linux shell脚本编程基础之练习篇
shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash ] then echo "请输入一个参数& ...
随机推荐
- [UWP]不那么好用的ContentDialog
ContentDialog是UWP开发中最常用的组件之一,一个体验良好的UWP应用很难避免不去使用它.博客园里也有许多的文章介绍如何来利用ContentDialog实现各种自定义样式的弹窗界面.不过实 ...
- 591. Tag Validator
Given a string representing a code snippet, you need to implement a tag validator to parse the code ...
- 字符串拼接时使用StringBuffer还是StringBuilder?
StringBuffer.StringBuilder和String一样,也用来代表字符串.String类是不可变类,任何对String的改变都 会引发新的String对象的生成:StringBuffe ...
- struts2框架学习笔记7:struts2标签
三大标签: 1.JSP:脚本,为了替代servlet,已过时 2.JSTL:标准标签库(core.format.sql.xml),还未淘汰的只有core库 3.Struts2标签库:由Struts2开 ...
- Failed to acquire lock on file .lock in /tmp/kafka-logs. A Kafka instance in another process or thread is using this directory.
1. 问题现象 启动 kafka 时报错:Failed to acquire lock on file .lock in /tmp/kafka-logs. A Kafka instance in an ...
- OC学习4——OC新特性之块(Block)
文章主要参考 关于OC中的block自己的一些理解(一) 对块的深入理解 浅析ios开发中Block块语法的妙用 1.关于block block的作用:保存一段代码. 苹果官方推荐的一种语法,类似 ...
- [宏]__stringify
Linux内核中有如下两个宏: #define __stringify_1(x...) #x #define __stringify(x...) __stringify_1(x) 写代码测试如下: # ...
- Java运行环境(win10)
系统安装Java后,配置运行环境,我的系统是win10,之前随便装了,没想到最近执行javac命令报错,(网上找了一堆都没用)处理方式如下: 环境变量-新建:变量名:%JAVA_HOME% 变量值: ...
- 【LeetCode】1. 两数之和
题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样 ...
- 1. Spring 框架简介及官方压缩包目录
一.Spring 框架简介及官方压缩包目录介绍 1.主要发明者:Rod Johnson 2.轮子理论推崇者: 2.1 轮子理论:不用重复发明轮子. 2.2 IT 行业:直接使用写好的代 ...