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. pb数据导出

    pb数据导出(一) 1.在窗口新建用户事件  ue_export    2.事件调用函数 gf_dw_to_excel(THIS.dw_dict) 3.写函数 :boolean lb_setborde ...

  2. 使用PostgreSQL进行中文全文检索

    code[class*="language-"], pre[class*="language-"] { background-color: #fdfdfd; - ...

  3. i==1 && resolve()

    for( var i=100000 ; i>0 ; i-- ){ i==1 && resolve() } var dd = 988889;console.log(`${dd}`) ...

  4. 使用 mybatis-generator 自动生成 MyBatis 代码

    首先把这三个文件放到一个目录里,我是放到了C盘下的wangbo目录下: 接下来就是设置config.xml文件了,config.xml: <?xml version="1.0" ...

  5. webpack热加载:修改文件自动刷新浏览器并更新

    概述 之前用react脚手架,觉得那种修改了能立即自动刷新浏览器并更新的功能实在非常人性化,所以想在开发其它项目的时候能用上.于是查了一些资料记录在此,供以后开发时参考,相信对其他人也有用. 其实代码 ...

  6. 人生苦短:Python里的17个“超赞操作

    人生苦短,我选Python”.那么,你真的掌握了Python吗?   1. 交换变量 有时候,当我们要交换两个变量的值时,一种常规的方法是创建一个临时变量,然后用它来进行交换.比如: # 输入 a = ...

  7. linux ls统计文件个数

    Linux下有三个命令:ls.grep.wc.通过这三个命令的组合可以统计目录下文件及文件夹的个数. 统计当前目录下文件的个数(不包括目录) ls -l |grep "^-"|wc ...

  8. Spring Boot 核心配置文件 bootstrap & application 详解。

    用过 Spring Boot 的都知道在 Spring Boot 中有以下两种配置文件 bootstrap (.yml 或者 .properties) application (.yml 或者 .pr ...

  9. 构建NTP时间服务器

    NTP服务器是用于局域网服务器时间同步使用的,可以保证局域网所有的服务器与时间服务器的时间保持一致,某些应用对时间实时性要求高的必须统一时间. 互联网的时间服务器也有很多,例如ntpdate ntp. ...

  10. Python--DBUtil

    Python--DBUtil包 1 简介 DBUtils是一套Python数据库连接池包,并允许对非线程安全的数据库接口进行线程安全包装.DBUtils来自Webware for Python. DB ...