bash脚本编程---循环
单分支的if语句:
if 测试语句then代码分支fi双分支的if语句:
if 测试条件;then条件为真时执行的分支else条件为假时执行的分支fi
例1:通过参数传递一个用户名给脚本,此用户不存时,则添加之;
#!/bin/bashif ! (grep "^$1\>" /etc/passwd &> /dev/null);thenuseradd $1echo $1 | passwd --stdin $1 &>/dev/nullecho "add user $1 finished."elseecho "the user $1 is exists."fi- #如果给出的用户不存在,就添加,用户帐号与密码相同,如果存在就提示存在
#!/bin/bashif [ $# -eq 1 ];thenecho "At least one username."exit 2fi#先判断是否输入了一个参数,如果没有或者输入了多个参数就退出#如果是“-lt”,则表示最少一个参数,即使输入多个也只取第一个if ! (grep "^$1\>" /etc/passwd &> /dev/null);thenuseradd $1echo $1 | passwd --stdin $1 &>/dev/nullecho "add user $1 finished."elseecho "the user $1 is exists."fi
#!/bin/bashif [ $# -lt 2 ];thenecho "Must give two num."exit 2fiif [ $1 -ge $2 ];thenecho "Max num is $1."elseecho "Max num is $2."fi
#!/bin/bashif [ $# -lt 2 ];thenecho "Must give two num."fideclare -i max=$1if [ $1 -lt $2 ];thenmax=$2fiecho "The max num is $max"#先对max进行赋值,然后进行比较
#!/bin/bashif [ $# -lt 1 ];thenecho "Must give a username."finum=$(id -u $1)let num2=$num%2#echo "num is $num"#echo "num2 is $num2"if [ $num2 -eq 1 ];thenecho "the uid is ji"elseecho "the uid is ou"fi
#!/bin/bashif [ $# -lt 2 ];thenecho "Must give two filename."exit 2fiif [ -f $1 ];thennum1=$(cat $1 |wc -l)echo "the file $1 is exists and hangshu is $num1."elseecho "the file $1 is not exists."exit 4fiif [ -f $2 ];thennum2=$(cat $2 |wc -l)echo "the file $2 is exists and hangshu is $num2."elseecho "the file $2 is not exists."exit 3fiif [ $num1 -lt $num2 ];thenecho "$2 hangshu is more."elseecho "$1 hangshu us more."
1.bash编程之选择执行--if语句
1.1 单分支if语句
if 测试条件;then为真时执行;fi
1.2 双分支if语句
if 测试条件;then为真时执行else为假时执行fi
1.3 多分支语句
if 测试条件;then条件1为真时执行elif condition-2;then条件2为真时执行elif condition-3;then条件3为真时执行。。。。elif condition-n;then条件n为真时执行else所有条件都不满足时执行的分支fi
#!/bin/bash#if [ $# -lt 1 ];thenecho "Must give a path."exit 2fiif [ -L $1 ];thenecho "Symbolic link."elif [ -b $1 ];thenecho "block special."elif [ -c $1 ];thenecho "Character special file."elif [ -S $1 ];thenecho "Socket file."elif [ -f $1 ];thenecho "common file."elif [ -d $1 ];thenecho "Directory."lseecho "UNKNOW."fi
#!/bin/bash[ $# -lt 1 ] && echo "At least one username."&& exit 1 #先判断参量是否存在! id $1 &>/dev/null && echo "Bo such user."&& exit 2 #判断用户是否存在uid=$(id -u $1)if [ $uid -eq 0 ];thenecho "the user is root."elif [ $uid -le 1000 ];thenecho "the user is system user."elseecho "the user is login user."fi
2.bash编程之循环执行
2.1 for循环
for VARIABLE in LIST;do循环体done
(a):{start..end}(b):seq [start [incremtal]] last
3.返回列表的命令
2.2 while循环
while CONDITION;do循环体循环控制变量修正表达式done
2.3 until循环
until CONDITION;do循环体循环控制变量修正表达式done
#!/bin/bashdeclare -i sum=0declare -i i=1#until [ $i -gt 100 ];do#判断i的值是否大于100,为假时循环while [ $i -le 100 ];do#判断i的值是否小于等于100,为真时循环let sum+=$ilet i++doneecho $sum
#!/bin/bashdeclare -i sum=0#for i in {1..100};dofor i in `seq 1 100`;dolet sum=$sum+$idoneecho "sum=$sum"
#!/bin/bashfor i in {101..103};doname="user${i}"! id $name &> /dev/null && useradd $name && echo "add user ${name} succeed!"echo "$i" | passwd --stdin "$name" &> /dev/null && echo "set passwd '${name}' for $name succeed!"done
#!/bin/bashfor j in {1..9};dofor i in `seq 1 $j`;doecho -n -e "${i}x${j}=$[${i}*${j}]\t"doneechodone
#!/bin/bashfor s in {1..9};doj=$[10-$s]for i in $(seq 1 $j);doecho -n -e "${i}X${j}=$[${i}*${j}]\t"doneechodone
#!/bin/bashdeclare -i s=9until [ $s -eq 0 ];dodeclare -i j=1#注意:每次循环,i的值都会发生变化,一定要重新定义until [ $j -gt $s ];doecho -n -e "${s}X${j}=$[${s}*${j}]\t"let j++done- let s--
echodone
#!/bin/bashdeclare -i s=9while [ $s -gt 0 ];dodeclare -i j=1while [ $j -le $s ];doecho -n -e "${s}X${j}=$[${s}*${j}]\t"let j++donelet s--echodone
bash脚本编程---循环的更多相关文章
- Bash脚本编程学习笔记07:循环结构体
本篇中涉及到算术运算,使用了$[]这种我未在官方手册中见到的用法,但是确实可用的,在此前的博文<Bash脚本编程学习笔记03:算术运算>中我有说明不要使用,不过自己忘记了.大家还是尽量使用 ...
- 脚本命令高级Bash脚本编程指南(31):数学计算命令
题记:写这篇博客要主是加深自己对脚本命令的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 高等Bash脚本编程指南(31):数学盘算命令 成于坚持,败于止步 操作数字 factor ...
- Bash脚本编程总结
bash脚本编程之用户交互: read [option]… [name …] -p ‘PROMPT’ -t TIMEOUT bash -n /path/to/some_script 检测脚本中的 ...
- bash脚本编程知识储备
bash脚本编程: 脚本程序:解释器解释执行: 首先得理清一些琐碎的知识点,我尽量把我所学的帮朋友一起梳理一下 编程环境:(我会在接下来的篇章,图文例子三结合的方式带大家一起学习) ...
- Bash脚本编程之算术运算
简介 Bash所支持的算术运算和C语言是一样的,这里指的是操作符(operator)以及它们的优先级(precedence).结合性(associativity)和值,详见Shell Arithmet ...
- Bash脚本编程之数组
数组简介 在bash脚本编程当中,变量是存储单个元素的内存空间:而数组是存储多个元素的一段连续的内存空间. 数组由数组名和下标构成,如下. ARRAY_NAME[SUBSCRIPT] 数组按照下标的类 ...
- Bash脚本编程学习笔记06:条件结构体
简介 在bash脚本编程中,条件结构体使用if语句和case语句两种句式. if语句 单分支if语句 if TEST; then CMD fi TEST:条件判断,多数情况下可使用test命令来实现, ...
- Bash 脚本编程
概述 Bash (GNU Bourne-Again Shell) 是许多Linux发行版的默认Shell. shell语法 变量 定义:your_name="hellohhy" 使 ...
- 高级Bash脚本编程指南(27):文本处理命令(三)
高级Bash脚本编程指南(27):文本处理命令(三) 成于坚持,败于止步 处理文本和文本文件的命令 tr 字符转换过滤器. 必须使用引用或中括号, 这样做才是合理的. 引用可以阻止shell重新解释出 ...
随机推荐
- spring管理配置文件的工厂类--PropertiesFactoryBean
使用这个工厂的配置,可以很方便的获取配置文件中的属性.具体使用如下; 对于属性配置,一般采用的是键值对的形式,如: key=value 属性配置文件一般使用的是XXX.properties,当然有时候 ...
- map | make_pair
#include <map> void func(std::map<int,std::pair<const char*,int>> &T_map) { st ...
- mailto调用本地默认客户端发邮件
下面介绍如何利用 Mailto功能: 实现 Mailto的基本html代码: <a href="mailto:123@qq.com">点击这里发邮件!</a> ...
- RELabel : 一个极简的正则表达式匹配和展示框架
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- 20+行代码使用es5 Object.defineProperty 实现简单的watch功能
/** * 一个简单的demo 帮助理解defineProperty,只对Object类型参数有效 */ $watch=function(myObject,callback){ function in ...
- vue-resource promise兼容性问题
背景 其实这个问题在之前的项目开发中就出现过,但是当初只解决问题了,并没有针对问题作总结:于是乎今天踩到了自己埋的坑,所以决定记录一下.那么到底是什么问题呢?就是"在安卓低版本,如果你在vu ...
- 实现最简单PHP MVC实例
关于网上大多MVC的简介我就不再多说,就是Model(模型)View(视图) C(控制器)这里作为一个刚入门PHP MVC框架的我,这里我搭建一个最简易的mvc项目,从而理解MVC 1在apache服 ...
- Cordova各个插件使用介绍系列(六)—$cordovaDevice获取设备的相关信息
详情请看:Cordova各个插件使用介绍系列(六)—$cordovaDevice获取设备的相关信息 在项目中需要获取到当前设备,例如手机的ID,联网状态,等,然后这个Cordova里有这个插件可以用, ...
- Rest模式get,put,post,delete含义与区别(转)
POST /uri 创建 DELETE /uri/xxx 删除 PUT /uri/xxx 更新或创建 GET /uri/xxx 查看 GET操作是安全的.所谓安全是指不管进行多 ...
- oracle创建数据库表空间 用户 授权 导入 导出数据库
windows下可以使用向导一步一步创建数据库,注意编码. windows连接到某一个数据库实例(不然会默认到一个实例下面):set ORACLE_SID=TEST --登录开始创建表空间及可以操作的 ...