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. Git分支管理(四)

    一.什么是分支 分支的好处: 同时并行推进多个功能开发,提高开发效率 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任 何影响.失败的分支删除重新开始即可. 二.分支的操作 1. 创建 ...

  2. luoguP4008 [NOI2003]文本编辑器

    题意 splay维护即可 code: #include<bits/stdc++.h> using namespace std; const int maxn=2000010; int T, ...

  3. javaweb监听器实现与原理

    参考:https://www.cnblogs.com/lxp503238/p/6678688.html https://blog.csdn.net/CPOHUI/article/details/888 ...

  4. SQL-select常用语句

    1.全套装备 select [select选项] 字段列表[字段别名]/* from 数据源[where 条件子句] [group by条件子句] [having 子句] [order by 子句] ...

  5. hadoop 输入路径用正则表达式被默认处理为多个参数的问题

    运行命令 hadoop jar   wordcount.jar   com.WordCount  /inpath/*{beijing,shanghai,guangzhou}*   /outpath/ ...

  6. [RN] react-native FlatList 实现列表选中的最佳方式(刷新指定Item)

    效果如下: 核心思路就是往数据源里面 给每条数据加一个选中状态. 如图在网络请求完成之后,给每条数据添加一个select的状态: data.list.forEach(item => item.s ...

  7. shell 脚本 for,while,case 语句详解及案例

    ################for循环语句的结构#############使用for循环语句时,需要指定一个变量及可能的取值列表,针对每个不同的取值重复执行相同的命令序列,直到变量值用完退出循环. ...

  8. Django性能优化的几种方法

    1.一次性取出你所需要的数据 单一动作,需要多次连接数据库里的时候,最好一次性取出所有需要的数据,减少连接数据库的次数.此类需求推荐使用QuerySet.select_related()和prefet ...

  9. Nginx 的 Timeout Wait 解决

    1.问题解决办法 查看Nginx并发状态 #netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' TIME_WAIT ...

  10. torch_09_GAN

    1.生成对抗网络 让两个网络相互竞争,通过生成网络来生成假的数据,对抗网络通过判别器判别真伪,最后希望生成网络生成的数据能够以假乱真骗过判别器 2.生成模型 就是‘生成’样本和‘真实’的样本尽可能的相 ...