bash循环
bash脚本-循环02
1.循环执行的条件
2.循环控制语句:continue,break,sleep
2.1continue
while CONDITION1;do
cmd1
...
if condition2;then
continue
fi
cmd2
...
done
#/bin/bash
#
declare -i sum=0
declare -i i=0
while [ $i -le 100 ];do
let i++
if [ $[$i%2] -eq 1 ];then
continue
fi
let sum+=$i
done
echo "sum is $sum"
2.2 break
while CONDITION1;do
CMD1
if CONDITION2;then
break
fi
done
while true;do
循环体
done
#!/bin/bash
declare -i sum=0
declare -i i=1
while true;do
let sum+=$i
let i+=2
if [ $i -gt 100 ];then
break
fi
done
echo "sum is $sum"
#!/bin/bash
name=zhangpf
- until (who | grep "^$name\>" &> /dev/null);do
echo $?
sleep 3
done
echo "$(date +"%F-%T") $name logged on" >> /tmp/sleep.log
#!/bin/bash
name=zhangpf
while true;do
if who | grep "^$name\>" &> /dev/null;then
break
fi
sleep 3
done
echo "$(date +"%F-%T") $name logged on" >> /tmp/login.log
3.循环体的特殊用法
3.1 while循环的特殊用法(遍历文件的行)
while read VARIABLE;do
循环体;
done < /path/to/somefile
#依次读取/path/to/somefile文件中的每一行,且将基赋值给VARIABLE变量;
#!/bin/bash
while read line;do
id=$(echo $line | cut -d: -f3)
name=$(echo $line | cut -d: -f1)
shell=$(echo $line | cut -d: -f7)
if [ $[$id%2] -eq 1 ];then
echo "$name,$id,$shell."
fi
done < /etc/passwd
for ((控制变量初始化;条件判断表达式;控制变量的修正语句));do
循环体
done
#!/bin/bash
declare -i sum=0
for ((i=0;i<=100;i++));do
let sum+=$i
done
echo "sum=$sum"
#!/bin/bash
for ((i=1;i<=9;i++));do
for ((j=1;j<=i;j++));do
echo -e -n "${i}X${j}=$[${i}*${j}]\t"
done
echo
done
4.bash语句之case语句
case $VARIABLE in
PAT1)
分支1
;;
PAT2)
分支2
;;
。。。。
*)
分支n
;;
esac
#!/bin/bash
#version 0.0.1
#author:zhangpf
#date:2016.07
#description:this just a test.
#
prog=$(basename $0)
lockfile=/var/lock/subsys/$prog
case $1 in
start)
if [ -f $lockfile ];then
echo "$prog is running yet."
else
touch $lockfile
[ $? -eq 0 ] && echo "start $prog finished."
fi
;;
stop)
if [ -f $lockfile ];then
rm -f $lockfile
[ $? -eq 0 ] && echo "stop $prog is finished."
else
echo "$prog is not running."
fi
;;
restart)
if [ -f $lockfile ];then
rm -f $lockfile
touch $lockfile
echo "restart $prog finished."
else
touch -f $lockfile
echo "start $prog finished."
fi
;;
status)
if [ -f $lockfile ];then
echo "$prog is running."
else
echo "$prog is stopped."
fi
;;
*)
echo "Usage:$prog {start|stop|restart|status}"
exit 1
esac
bash循环的更多相关文章
- bash循环语句
1 )单分支if语句 if 测试条件 :then 如果满足条件就执行这里的代码 f 2)双分支的if语句 if 测试条件:then 如果满足条件就执行这里的代码 else 如果不满足条件就执行这里 ...
- bash循环for/while/until
shell流程控制之一:for循环 for VAR in LIST; do STATEMENT1 ... done 例: ...
- Bash循环分类介绍
方法一: #!/bin/bash ` do #code here echo $i done 方法二:C语言风格 #!/bin/bash ; i<=; i++)) do printf " ...
- bash循环得到日期目录
#!/bin/bash today=$(date "+%Y%m%d") echo 'today is :'${today} single_input="raw_data/ ...
- bash脚本:集群资源争夺战crazy-killer
背景 公司的集群很多人一起用,有时候就难免资源紧张,某次需要用的时候没资源等了半天还是没资源,再等半天还是没资源,于是就写了个脚本泄愤,建议看到的人拷走放在自己公司集群上长期运行 :) 实现 此脚本运 ...
- Bash 实例,第二部分
我们先看一下处理命令行自变量的简单技巧,然后再看看 bash 基本编程结构. 接收自变量 在 介绍性文章 中的样本程序中,我们使用环境变量 "$1" 来引用第一个命令行自变量.类似 ...
- linux shell 报错 Syntax error: Bad for loop variable
在linux下写了一个简单的shell,循环10次. test.sh #!/bin/bash ## ##循环10次 ## ; i<; i++)); do echo Good Morning ,t ...
- shell脚本练习题->1
猜随机数的大小 描述: 写一个猜数字脚本,当用户输入的数字和预设数字(随机生成一个0-100的数字)一样时,直接退出,否则让用户一直输入:并且提示用户输入的数字比预设数字大或者小 分析: 1:随机数字 ...
- shell系统检测->
系统状态检测脚本练习 1-> 查看磁盘状态 思路:查看磁盘/当前使用状态,如果使用率超过80%则报警发邮件 1.获取磁盘当前使用的值 df -h|grep /$ 2.从获取到的值中提取出,对应的 ...
随机推荐
- EF中防止sql注入
EF作为一个orm框架,本身以及放置了sql的注入,但是如果我们需要执行sql语句的时候了?比如,我们需要查询视图"select * from VM where 条件 = {0}" ...
- 什么是MTBF测试【转】
本文转载自:https://blog.csdn.net/liuhaoemail/article/details/50531489 MTBF测试 目前,终端侧的可靠性测试基本上是采用称为”MTBF测试” ...
- springmvc学习之jdk版本,tomcat版本,spring版本
使用的软件是myeclipse2018,jdk8,tomcat9.0,spring3.2.0 以上为我的软件及各种配置 1.建立了web工程,build path 使用的是默认的j2EE1.8(只有配 ...
- Sitecore8.2 Solr5.1.0配置步骤
1.首先下载Solr安装包,官方提供了几种下载,我选的的solr的5.1.0版本zip包,下载链接:http://mirror.bit.edu.cn/apache/lucene/solr. 2.下载后 ...
- pyhon-request之repsonse的常用方法reponse.text和reponse.content的区别
1. requests在python2 和 python3中通用,方法完全一样 2. request简单易用 requests的作用 作用:发送网络请求,返回响应数据 用法 response = re ...
- C++ 基本数据类型,常量,变量
基本数据类型 整数类型 基本的整数类型(int) 按符号分 符号的 (signed) 无符号的(unsigned) 按照数据范围分 短整数(short) 长整数(long) 长长整数(long lon ...
- _spellmod_aura_pct
属性光环 为玩家增加光环或降低属性 `aura`光环ID `auraType1` 配置属性('空','生命值','全属性','近战攻击强度','远程攻击强度','法术强度','治疗效果','施法速度' ...
- regression | p-value | Simple (bivariate) linear model | 线性回归 | 多重检验 | FDR | BH | R代码
P122, 这是IQR method课的第一次作业,需要统计检验,x和y是否显著的有线性关系. Assignment 1 1) Find a small bivariate dataset (pref ...
- C(n,m)排列组合算法
主要解决C(n,m)问题 static class Extension { public static IList<IList<T>> GetGroup<T>(th ...
- blob canvas img dataUrl的互相转换和用处
blob:代表了一段二进制数据 初始化:var blob = new Blob(array,option)//其中array里面可以包含任意类型对象,option指数据类型如array是['<h ...