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语句案例的更多相关文章

  1. Linux Shell脚本编程while语句

    Linux Shell脚本编程while语句案例 1,每隔3秒,打印一次系统负载 #!/bin/bash while truedo    uptime    sleep 3done 2,把监控结果保存 ...

  2. Linux shell脚本编程if语句的使用方法(条件判断)

    if 语句格式if  条件then Commandelse Commandfi        别忘了这个结尾If语句忘了结尾fitest.sh: line 14: syntax error: unex ...

  3. Linux shell脚本编程(三)

    Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...

  4. Linux shell脚本编程(二)

    Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...

  5. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  6. Linux Shell脚本编程--Linux特殊符号大全

    Linux Shell脚本编程--Linux特殊符号大全 linux_shell 特殊符号的介绍 2011

  7. Linux Shell脚本编程-基础1

    概述:  shell脚本在Linux系统管理员的运维工作中非常重要.shell脚本能够帮助我们很方便的管理服务器,因为我们可以指定一个任务计划,定时的去执行某一个脚本以满足我们的需求.本篇将从编程基础 ...

  8. 【学习】Linux Shell脚本编程

    1.脚本的组成和执行 Linux shell脚本的结构并不复杂,其主要由变量.内部命令以及shell的语法结构和一些函数.其他命令行的程序等组成,以下是一个简单的shell脚本. #!/bin/bas ...

  9. Linux shell脚本编程基础之练习篇

    shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash ] then echo "请输入一个参数& ...

随机推荐

  1. Vuejs——(10)组件——父子组件通信

    版权声明:出处http://blog.csdn.net/qq20004604   目录(?)[+]   本篇资料来于官方文档: http://cn.vuejs.org/guide/components ...

  2. 关于调试WCF时引发的异常XmlException: Name cannot begin with the '<' character, hexadecimal value 0x3C” on Client Side

    问题描述:在使用VS2015调试WCF时,偶遇抛出异常名称不能以“<”字符(十六进制0x3c)开头,平时运行时(不调试)没有问题的. 解决方法:检查后发现为了检查异常的位置,勾选了引发通用语言运 ...

  3. 优化版小程序canvas,增加失败逻辑,及完善文字

    wxml <view class="shareBox" style="backgound:{{isShow ? '#000' : '#fff'}}" wx ...

  4. react写一个todo

    概述 最近学习redux,打算先复习一下react,所以用react写了一个todo.记录下来,供以后开发时参考,相信对其他人也有用. 代码 代码请见我的github 组织架构如下图:

  5. ElasticSearch 工具类封装(基于ElasticsearchTemplate)

    1.抽象接口定义 public abstract class SearchQueryEngine<T> { @Autowired protected ElasticsearchTempla ...

  6. Python3基础语法你学会了么

      编码 默认:源码文件以UTF-8编码,字符串都是unicode字符串 指定:   标识符 第一个字符:字母表中的字符或下划线 _ 其它部分:由字母.数字.下划线 _ 组成 大小写敏感 python ...

  7. centos7配置apache服务

    首先写下基本的步骤: 1.环境准备:关闭防火墙(不关闭无法访问,我这里只是简单的配置,实际部署项目应该会有具体的设置),关闭selinux(试过,不关闭也没事,一般都关闭) 配置 ip 2.安装软件包 ...

  8. Windows 10安装Python 3 7成功打印Hello World!

    Python下载 Python最新源码,二进制文档,新闻资讯等可以在Python的官网查看到: Python官网:https://www.python.org/ 你可以在以下链接中下载 Python ...

  9. 【转】ASP.NET Core MVC 配置全局路由前缀

    本文地址:http://www.cnblogs.com/savorboard/p/dontnet-IApplicationModelConvention.html作者博客:Savorboard 前言 ...

  10. IDA远程调试 在内存中dump Dex文件

    1. 首先使用调试JNI_OnLoad函数的方法,先将apk以调试状态挂起,使用IDA附加上去. 2. 然后在libdvm.so中的dvmDexFileOpenPartial函数上下一个断点 3. 然 ...