Linux Shell脚本编程while语句案例
1,每隔3秒,打印一次系统负载
#!/bin/bash
while true
do
    uptime
    sleep 3
done
2,把监控结果保存到文件,在后台执行,然后用tail -f监控文件变化
ghostwu@dev:~/linux/shell/flow_control$ sh while.sh &
[1] 12867
#!/bin/bash
while true
do
    uptime >> log.txt
    sleep 3
done
ghostwu@dev:~/linux/shell/flow_control$ tail -f log.txt
 06:14:32 up 33 min,  1 user,  load average: 0.33, 0.35, 0.32
 06:14:35 up 33 min,  1 user,  load average: 0.33, 0.35, 0.32
 06:14:38 up 33 min,  1 user,  load average: 0.31, 0.34, 0
...
3,进程调度相关命令
fg: 把当前脚本或者任务放到前台执行。如果指定某个任务:fg 任务编号。 任务编号通过jobs查询
bg: 把任务放到后台执行
jobs:查看当前执行的脚本或者任务
ctrl+z:暂停执行当前的脚本
sh while1.sh & : 加上&,表示后台执行脚本
ghostwu@dev:~/linux/shell/flow_control$ fg
sh while.sh
^Z
[1]+  Stopped                 sh while.sh
ghostwu@dev:~/linux/shell/flow_control$ jobs
[1]+  Stopped                 sh while.sh
ghostwu@dev:~/linux/shell/flow_control$ bg
[1]+ sh while.sh &
ghostwu@dev:~/linux/shell/flow_control$ jobs
[1]+  Running                 sh while.sh &
ghostwu@dev:~/linux/shell/flow_control$ sh while.sh &
[2] 13411
ghostwu@dev:~/linux/shell/flow_control$ jobs
[1]-  Running                 sh while.sh &
[2]+  Running                 sh while.sh &
ghostwu@dev:~/linux/shell/flow_control$ fg
sh while.sh
^Z
[2]+  Stopped                 sh while.sh
ghostwu@dev:~/linux/shell/flow_control$ bg
[2]+ sh while.sh &
ghostwu@dev:~/linux/shell/flow_control$ jobs
[1]-  Running                 sh while.sh &
[2]+  Running                 sh while.sh &
 4,用while循环打印0, 1, 2, 3, 4
#!/bin/bash
i=0
while [ $i -lt 5 ]
do
    echo $i
    (( i++ ))
done
两个中括号也可以
#!/bin/bash
i=0
while [[ $i -lt 5 ]]
do
    echo $i
    (( i++ ))
done
还可以用计算表达式
#!/bin/bash
i=0
while (( i < 5 ))
do
    echo $i
    (( i++ ))
done
5,计算1....100的和
ghostwu@dev:~/linux/shell/flow_control$ sh sum.sh
1+2+3..+100=5050
ghostwu@dev:~/linux/shell/flow_control$ cat sum.sh
#!/bin/bash
i=1
while (( i <= 100 ))
do
    (( sum = sum + i ))
    (( i++ ))
done
echo "1+2+3..+100="${sum}
 6,猜数字
#!/usr/bin/bash
sum=$((RANDOM%51))
echo "需要你猜的数是:"$sum
sleep 1
echo "请输入1-50之间的数,开始猜吧!"
count=0
function type_num(){
    read -p "请输入一个数吧:" n
    expr $n + 1 &>/dev/null
    if [ $? -ne 0 ]; then
        echo "请输入一个数字"
        type_num
    fi
}
function guess(){
    (( count++ ))
    if [ $n -eq $sum ]; then
        echo "你猜中了,你的次数是:"${count}
        if [ $count -lt 3 ]; then
            echo "你太厉害了"
        elif [ $count -ge 3 -a $count -lt 6 ]; then
            echo "还是不错的,加油"
        else
            echo "你有点水啊"
        fi
        exit 0
    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语句案例

    1,每隔3秒,打印一次系统负载 #!/bin/bash while true do uptime done 2,把监控结果保存到文件,在后台执行,然后用tail -f监控文件变化 ghostwu@de ...

  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. openlayers在底图上添加静态icon

    越学习openlayer你会发现openlayer是真的很强大,今天记录一下学习的成果,需求是做那种室内的CAD的场景然后里面展示人员icon并且实时展示人员的位置信息,以及点击弹出对应人员的一些位置 ...

  2. ASP.NET开发实战——(五)ASP.NET MVC & 分层

    上一篇文章简要说明了MVC所代表的含义并提供了详细的项目及其控制器.视图等内容的创建步骤,最终完成了一个简单ASP.NET MVC程序. 注:MVC与ASP.NET MVC不相等,MVC是一种开发模式 ...

  3. JOI2013-2019题解

    JOI2013-2019题解 Link

  4. Educational Codeforces Round 70 题解

    噩梦场. 题目出奇的难,好像一群外国老哥看 A 看着看着就哭了-- A 找到 \(b\) 最低的 \(1\),这个 \(1\) 肯定要跟 A 中的一个 \(1\) 搭配,而且是能搭配的 \(1\) 中 ...

  5. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  6. java web开发入门十二(idea创建maven SSM项目需要解决的问题)基于intellig idea(2019-11-09 11:23)

    一.spring mvc action返回string带双引号问题 解决方法: 在springmvc.xml中添加字符串解析器 <!-- 注册string和json解析适配器 --> &l ...

  7. 如何用代码设置机器人初始坐标实现 2D Pose Estimate功能

    前言:ROS机器人有时候会遇到极端的情况:比如地面打滑严重,IMU精度差,导致积累误差严重,或是amcl匹配错误,导致机器人定位失败, 这时候如何矫正机器人位置变得非常重要,我的思路是利用相机或是在地 ...

  8. Reimage Isilon cluster,结果忘记了修改管理口的netmask,怎么办?

    网页打不开了,正常的SSH也连不上,只能用串口,连接到节点上. 然后使用运行下面的命令来修改netmask: isi network subnets modify groupnet0.subnet0 ...

  9. python网络爬虫(1)——安装scrapy框架的常见问题及其解决方法

    Scrapy是为了爬取网站数据而编写的一款应用框架,出名,强大.所谓的框架其实就是一个集成了相应的功能且具有很强通用性的项目模板. 其实在Linux和 Mac安装,就简单的pip命令即可: pip i ...

  10. WPF 精修篇 倾斜 SkewTransform

    原文:WPF 精修篇 倾斜 SkewTransform 倾斜 SkewTransform AngleX 倾斜X角度 AngleY 倾斜Y角度 CenterX CenterY 中心点 <Stack ...