bash脚本编程---循环
单分支的if语句:
if 测试语句then代码分支fi双分支的if语句:
if 测试条件;then条件为真时执行的分支else条件为假时执行的分支fi
例1:通过参数传递一个用户名给脚本,此用户不存时,则添加之;
#!/bin/bash
if ! (grep "^$1\>" /etc/passwd &> /dev/null);then
useradd $1
echo $1 | passwd --stdin $1 &>/dev/null
echo "add user $1 finished."
else
echo "the user $1 is exists."
fi
- #如果给出的用户不存在,就添加,用户帐号与密码相同,如果存在就提示存在
#!/bin/bash
if [ $# -eq 1 ];then
echo "At least one username."
exit 2
fi
#先判断是否输入了一个参数,如果没有或者输入了多个参数就退出
#如果是“-lt”,则表示最少一个参数,即使输入多个也只取第一个
if ! (grep "^$1\>" /etc/passwd &> /dev/null);then
useradd $1
echo $1 | passwd --stdin $1 &>/dev/null
echo "add user $1 finished."
else
echo "the user $1 is exists."
fi
#!/bin/bash
if [ $# -lt 2 ];then
echo "Must give two num."
exit 2
fi
if [ $1 -ge $2 ];then
echo "Max num is $1."
else
echo "Max num is $2."
fi
#!/bin/bash
if [ $# -lt 2 ];then
echo "Must give two num."
fi
declare -i max=$1
if [ $1 -lt $2 ];then
max=$2
fi
echo "The max num is $max"
#先对max进行赋值,然后进行比较
#!/bin/bash
if [ $# -lt 1 ];then
echo "Must give a username."
fi
num=$(id -u $1)
let num2=$num%2
#echo "num is $num"
#echo "num2 is $num2"
if [ $num2 -eq 1 ];then
echo "the uid is ji"
else
echo "the uid is ou"
fi
#!/bin/bash
if [ $# -lt 2 ];then
echo "Must give two filename."
exit 2
fi
if [ -f $1 ];then
num1=$(cat $1 |wc -l)
echo "the file $1 is exists and hangshu is $num1."
else
echo "the file $1 is not exists."
exit 4
fi
if [ -f $2 ];then
num2=$(cat $2 |wc -l)
echo "the file $2 is exists and hangshu is $num2."
else
echo "the file $2 is not exists."
exit 3
fi
if [ $num1 -lt $num2 ];then
echo "$2 hangshu is more."
else
echo "$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 ];then
echo "Must give a path."
exit 2
fi
if [ -L $1 ];then
echo "Symbolic link."
elif [ -b $1 ];then
echo "block special."
elif [ -c $1 ];then
echo "Character special file."
elif [ -S $1 ];then
echo "Socket file."
elif [ -f $1 ];then
echo "common file."
elif [ -d $1 ];then
echo "Directory."lse
echo "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 ];then
echo "the user is root."
elif [ $uid -le 1000 ];then
echo "the user is system user."
else
echo "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/bash
declare -i sum=0
declare -i i=1
#until [ $i -gt 100 ];do
#判断i的值是否大于100,为假时循环
while [ $i -le 100 ];do
#判断i的值是否小于等于100,为真时循环
let sum+=$i
let i++
done
echo $sum
#!/bin/bash
declare -i sum=0
#for i in {1..100};do
for i in `seq 1 100`;do
let sum=$sum+$i
done
echo "sum=$sum"
#!/bin/bash
for i in {101..103};do
name="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/bash
for j in {1..9};do
for i in `seq 1 $j`;do
echo -n -e "${i}x${j}=$[${i}*${j}]\t"
done
echo
done
#!/bin/bash
for s in {1..9};do
j=$[10-$s]
for i in $(seq 1 $j);do
echo -n -e "${i}X${j}=$[${i}*${j}]\t"
done
echo
done
#!/bin/bash
declare -i s=9
until [ $s -eq 0 ];do
declare -i j=1
#注意:每次循环,i的值都会发生变化,一定要重新定义
until [ $j -gt $s ];do
echo -n -e "${s}X${j}=$[${s}*${j}]\t"
let j++
done
- let s--
echo
done
#!/bin/bash
declare -i s=9
while [ $s -gt 0 ];do
declare -i j=1
while [ $j -le $s ];do
echo -n -e "${s}X${j}=$[${s}*${j}]\t"
let j++
done
let s--
echo
done
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重新解释出 ...
随机推荐
- java基础(十四章)
1.Java中的包(package) 2.1 包,对应到磁盘中的文件夹 2.2 新建一个class,默认保存在缺省包中 2.3 声明包的关键字:package package语句,置顶位置 2.4 导 ...
- 1.JAVA WEB 笔记中文乱码
JAVA WEB 乱码问题解析 乱码原因 在Java Web开发过程中,经常遇到乱码的问题,造成乱码的原因,概括起来就是对字符编码和解码的方式不匹配. 既然乱码的原因是字符编码与解码的方式不匹配,那么 ...
- 卷积神经网络的变种: PCANet
前言:昨天和大家聊了聊卷积神经网络,今天给大家带来一篇论文:pca+cnn=pcanet.现在就让我带领大家来了解这篇文章吧. 论文:PCANet:A Simple Deep Learning Bas ...
- Java并发编程之synchronized
在Java编程中,为了保证线程安全,有3种不同的思路1.互斥同步:包括synchronized和lock等. 2.非阻塞同步:如AtomicInteger的increaseAndGet()方法等. 3 ...
- iOS-Core-Animation-Advanced-Techniques(一)
视图(UIView)和图层(CALayer)的关系: 每一个UIview都有一个CALayer实例的图层属性,视图的职责就是创建并管理这个图层,以确保当子视图在层级关系中添加或者被移除的时候,他们关联 ...
- Maven转化为Dynamic Web Module
如今Maven仍然是最常用的项目管理工具,若要将Java Web项目使用Maven进行管理,则首先需要新建Maven项目,然后将其转化为web项目. 在项目右键选择properties,然后点击左侧P ...
- 转换编码,将Unicode编码转换成可以浏览的utf-8编码
//转换编码,将Unicode编码转换成可以浏览的utf-8编码 public function unicodeDecode($name) { $pattern = '/([\w]+)|(\\\u([ ...
- SQL联表查询
数据库中最最常用的语法----select.简单的select语法很直白: select column from table where expression: 从((from)存储数据的地方(tab ...
- Python3.5学习笔记-文件操作
在Python中,操作文件对象使用open函数来创建,下表列出了常用的操作file的函数: 序号 方法及描述 1.file.close() 关闭文件.关闭后文件不能再进行读写操作. 2.file.fl ...
- Linux之通配符
前言:学习通配符有点为正则表达式打基础的感觉……之前学python有学过正则表达式,所以这篇博客学起来还是挺快的. 特殊符号 | #管道符,或者(正则) > #输出重定向 >> #输 ...