linux while文件,linux while 循环
13. 07.
Bash While Loop Example – Bash While 循环实例
Q. How do I use bash while loop to repeat certain task under Linux / UNIX operating system?
A. Bash while loop is a control flow statement that allows code or
commands to be executed repeatedly based on a given condition. For
example, run echo command 5 times or read text file line by line or
evaluate the options passed on the command line for a script.
while loop syntax
while [ condition ]
do
command1
command2
command3
done
command1 to command3 will be executed repeatedly till condition is
true. The argument for a while loop can be any boolean expression.
Infinite loops occur when the conditional never evaluates to false. For
example following while loop will print welcome 5 times on screen:
#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done
Here is a sample shell code to calculate factorial using while loop:
#!/bin/bash
counter=$1
factorial=1
while [ $counter -gt 0 ]
do
factorial=$(( $factorial * $counter ))
counter=$(( $counter - 1 ))
done
echo $factorial
To run just type:
$ chmod +x script.sh
$ ./script.sh 5
Output:
120
While loops are frequently used for reading data line by line from file:
#!/bin/bash
FILE=$1
# read $FILE using the file descriptors
exec 3exec 0while read line
do
# use $line variable to process line
echo $line
done
exec 0
You can easily evaluate the options passed on the command line for a script. using while loop:
......
..
while getopts ae:f:hd:s:qx: option
do
case "${option}"
in
a) ALARM="TRUE";;
e) ADMIN=${OPTARG};;
d) DOMAIN=${OPTARG};;
f) SERVERFILE=$OPTARG;;
s) WHOIS_SERVER=$OPTARG;;
q) QUIET="TRUE";;
x) WARNDAYS=$OPTARG;;
\?) usage
exit 1;;
esac
done
.......
..
作者: | 可以转载, 转载时务必以超链接形式标明文章和作者信息及版权声明
网址:
Tags: , , ,
linux while文件,linux while 循环相关推荐
- linux php文件,Linux php文件安装目录在哪
php文件安装目录一般默认在"/var/www"里面,如果是自定义安装,则可以用Linux的命令进行查找,查找的命令是"find / -name "*www*& ...
- linux 编码文件,linux文件编码
linux下新建一个文件,或采用fopen新建,那么文件的编码是什么? 怎么查看文件编码格式: 查看文件编码file命令 file ip.txt ip.txt: UTF-8 Unicode text, ...
- 怎么解压linux镜像文件,linux 怎么解压文件
linux怎么解压文件 Linux提供了一个很简单的功能,用来解压*.zip 文件. 解压命令:unzip filename.zip Linux系统中还有一类后缀为*.tar的文件. 解压命令为: t ...
- linux设备文件,Linux 文件系统与设备文件
1 Linux 文件系统 1.1 Linux 文件系统与设备驱动关系 下图表明了 Linux 中虚拟文件系统.磁盘/Flash文件系统以及一般的设备文件与设备驱动程序之间的关系. 文件系统与设备驱动之 ...
- linux 移动 文件,linux移动文件命令
linux移动文件命令 mv命令 功能:为文件或目录改名或将文件由一个目录移入另一个目录中.该命令如同DOS下的ren和move的组合. 语法:mv [选项] 源文件或目录 目标文件或目录 说明: 视 ...
- python 测试linux dev文件,Linux测试开发人员要掌握的Linux命令有哪些?
今天小编要跟大家分享的文章是关于Linux测试开发人员要掌握的学习Linux和 一.文件和目录相关 文件和目录相关 1.进入目录--cd cd /usr/local //进入/usr/local这个目 ...
- linux epoll 文件,Linux面试必知:一句话讲透epoll-文件句柄
1. epoll概念 在Linux的Man文档中,我们可以看到如下定义Epoll - I/O event notification facility epoll是一种I/O事件通知机制 I/O事件I/ ...
- 定时从linux获取文件,Linux 使用scp命令定时将文件备份到另一台服务器
主要思路就是使用scp命令 + linux定时执行任务. 目标:2台服务器,A为主服务器,B为备份服务器,需要每天中午12点将A上的文件备份到B上. 1.首先在A上安装except 一路yes下去即可 ...
- 分析 linux 日志文件,linux精讲|操作系统常见日志文件分析
linux运维,离不开对系统日志的分析,除syslog外,还有常用的dmesg.wtmp.btmp.bash_history等系统日志文件以及应用程序相关的日志. 一.dmesg日志:记录内核日志信息 ...
最新文章
- 【学习摘录】推荐算法
- WEB程序员需要掌握的十大MySQL优化技巧
- SAP中记账码的作用
- Cisco正式停止MARS产品的销售
- 索尼PS5突然“上架”?预购价约7244元...
- php intval0.57100,应用NuSoap构建新型的基于PHP的Web服务
- python数据分析师工资待遇_预测python数据分析师的工资
- 阿里云mysql可视化_MySql可视化工具MySQL Workbench使用教程
- 在线手机WAP模拟器或软件,用电脑IE浏览器在线浏览手机wap网站
- Android ndk开发入门集锦一
- 关于Microsoft office深色模式设置
- 车载视频监控基于JTT808/1078管理平台商业开源
- vue3+el-table表格表头增加斜线
- RBF技术实现“双花漏洞”研究
- WIN10下系统缩放比例(DPI)的魔幻设置
- WECHAT 微信扫码关注公众号方法无法获取头像和昵称了
- 芯擎科技正式发布“龍鹰一号”,引领“中国智造”智能座舱芯片新篇章
- go timer和ticker使用方式
- 小程序中maring-top、maring-left、maring-right、maring-bottom失效/没有用的原因及解决方案
- 使用SqlConnection.connectionString连接数据库