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重新解释出 ...
随机推荐
- TLS1.0和TLS1.1的区别
TLS1.1是对TSL1.0的改进其中包括: 改进"抗抵赖"安全特性上的缺陷 完成协议对椭圆曲线的支持,提出了改进的支持ECC算法的传输层安全协议, 握手协议引入了数字签名及验证机 ...
- [1] MVC & MVP &MVVM
开发架构之MVC & MVP & MVVM
- (cljs/run-at (->JSVM :browser) "语言基础")
前言 两年多前知道cljs的存在时十分兴奋,但因为工作中根本用不上,国内也没有专门的职位于是搁置了对其的探索.而近一两年来又刮起了函数式编程的风潮,恰逢有幸主理新项目的前端架构,于是引入Ramda. ...
- jQuery时间日期插件laydate,兼容bootstrap
解压后,将laydate整个文件放至您项目的任意目录,不要移动其文件结构,它们具有完整的依赖体系. 使用只需在页面引入laydate.js即可. 如果您的网站的js采用合并或模块加载,您需要打开lay ...
- Cordova各个插件使用介绍系列(六)—$cordovaDevice获取设备的相关信息
详情请看:Cordova各个插件使用介绍系列(六)—$cordovaDevice获取设备的相关信息 在项目中需要获取到当前设备,例如手机的ID,联网状态,等,然后这个Cordova里有这个插件可以用, ...
- 【Android Developers Training】 4. 启动另一个Activity
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 信息安全的核心:CIA三元组 | 安全千字文系列1
我们总是在说信息安全管理,那么信息安全管理到底是在管什么?我们要如何定义信息安全? 这里就要引出信息安全最基本的概念:CIA三元组. 这里的 C,指的是Confidentiality机密性 这里的 I ...
- win 结束占用端口的进程
在web开发的时候,经常开启http服务器监听某个端口,例如npm run dev等等 以下介绍通过命令行结束占用端口的程序(注:测试环境为win10) 1.启动命令行 2.在命令行输入 netsta ...
- touchmover手机移动端的拖动
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name= ...
- Ionic 应用图标,信息修改
Ionic 应用图标,信息修改 Ionic 应用图标 修改 准备好替换的图标并生成各个尺寸的图标 1.使用命令行进入项目根目录,执行命令ionic resources 替换的图片放在resources ...