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 "请输入一个参数& ...
随机推荐
- noip第23课资料
- SDWebImage之UIView+WebCache
UIView+WebCache是我们能很方便的使用sd_setImageWithURL:系列方法来加载图片的关键类.UIButton(WebCache).MKAnnotationView(WebCac ...
- AJPFX简述:MetaTrader 4移动交易平台
(AJPFX)移动交易平台可以让客户随时通过客户手中的移动设备例如智能手机.PDA等管理自己帐户和进行交易.移动交易平台提供了完整的交易帐户管理分析选项,当客户无法使用台式计算机的时候,移动交易平台为 ...
- Java学习笔记36(File类)
File类可以对操作系统中的文件进行操作: File类的静态成员变量: package demo; import java.io.File; public class FileDemo { publi ...
- Dispatch Queue 之 dispatch_sync

- [每天解决一问题系列 - 0011] 如何清除Windows中的Icon缓存
问题描述: 当更换一个应用或者快捷方式的图标后,会看到图标并没有及时更新 问题原因: 原因是Windows为了使图标显示更快,做了缓存%localappdata%\IconCache.db 解决方案: ...
- 课程四(Convolutional Neural Networks),第二 周(Deep convolutional models: case studies) ——3.Programming assignments : Residual Networks
Residual Networks Welcome to the second assignment of this week! You will learn how to build very de ...
- oracle查锁及解锁命令
--查询行锁语句 select sql_text from v$sql a,v$session b where a.sql_id=b.sql_id and b.event='enq: TX - row ...
- 高手速成android开源项目【tool篇】
主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...
- 开启python学习之路
新生入学这一周来,基本都在看python从入门到精通,边看书边敲代码,简单的几行代码,巩固学到的知识,像当初学习各类编程语言一样,从最初开发环境的搭建,数据类型等,学习中做好笔记,然后学会运用. 学习 ...