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 "请输入一个参数& ...
随机推荐
- ZZ:git只clone仓库中指定子目录和指定文件的实现
原文链接: http://blog.csdn.net/xuyaqun/article/details/49275477 git目前唯一不能实现的是:不能像svn那样,针对子目录设置权限,这与git分布 ...
- spring boot开发笔记——mybatis
概述 mybatis框架的优点,就不用多说了,今天这边干货主要讲mybatis的逆向工程,以及springboot的集成技巧,和分页的使用 因为在日常的开发中,当碰到特殊需求之类会手动写一下s ...
- 2019-4-25 html学习笔记
一.概念 文本 用于储存和记录文字信息的载体 html 超文本标记语言(本质就是给文本增加语义 如<h1></h1>就是给文字添加一级标题的语义) 注:互联网三大基石 有 ...
- python中匿名函数lambda
简单来说,编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命 名一个函数的场合下使用,也就是指匿名函数. 先看它的几个用法: map( lambda x: x*x, [y f ...
- iOS-实现后台长时间运行
前言 一般APP在按下Home键被挂起后,这时APP的 backgroundTimeRemaining 也就是后台运行时间大约只有3分钟,如果在退出APP后,过十几二十二分钟或者更长时间再回到APP, ...
- Python模块——subprocess
subprocess模块 通过Python去执行一条系统命令或脚本. 三种执行命令的方法 subprocess.run(*popenargs, input=None, timeout=None, ch ...
- Xshell连接ESXI方法
第一步.ESXI打开ssh功能按住F2进入设置如下图: 第二步.输入密码 第三步.选择Troubleshooting Options 回车 第四步.选择Enable SSH 这里只介绍了一种方式打开E ...
- Python --代码风格检查 pep8
pip3 install pycodestyle pycodestyle .. Pep8编码规范 https://blog.csdn.net/ratsniper/article/details/789 ...
- interface21 - web - DispatcherServlet(DispatcherServlet初始化流程)
前言 最近打算花点时间好好看看spring的源码,然而现在Spring的源码经过迭代的版本太多了,比较庞大,看起来比较累,所以准备从最初的版本(interface21)开始入手,仅用于学习,理解其设计 ...
- AutoCAD 凸度(bulge)的概念及使用WPF函数画图
前言 凸度(bulge)是AutoCAD 中一个非常重要的概念,凸度控制着两点之间弧度大小,弧度的方向.各种复杂的图像有可能就是成百上千的弧线组成的.从AutoCAD中导出的数据也有该值,一般的形式 ...