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设计模式初探之装饰者模式
定义:动态给一个对象添加一些额外的职责,就象在墙上刷油漆.使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活.设计初衷:通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种 ...
- gulp报错
这个问题网上搜索到的答案不一定能够解决问题,有可能是node版本问题,需要升级 到最新版就可以了....
- Python 内置函数汇总
循环设计与循环对象 range() enumerate() zip() iter() 函数对象 map() filter() reduce() 序列操作 all([True, 1, "hel ...
- python flask(多对多表查询)
我们在flask的学习中,会难免遇到多对多表的查询,今天我也遇到了这个问题.那么我想了好久.也没有想到一个解决的办法,试了几种方法,可能是思路的限制我放弃了,后来,我就在网上百度,可是发现百度出来的结 ...
- iOS 转场动画探究(一)
什么是转场动画: 转场动画说的直接点就是你常见的界面跳转的时候看到的动画效果,我们比较常见的就是控制器之间的Push和Pop,还有Present和Dismiss的时候设置一下系统给我们的modalTr ...
- 通知栏Notification的整理
一.介绍 通知栏适用于交互事件的通知,是位于顶层可以展开的通知列表. 二.功能作用 1.显示接收到短消息,及时消息等信息(如QQ.微信.新浪.短信) 2.显示客户端的推送消息(如有新版本发 ...
- 【ASP.NET MVC 牛刀小试】 URL Route
例子引入 先看看如下例子,你能完全明白吗? using System; using System.Collections.Generic; using System.Linq; using Syste ...
- php检测当前浏览器是否为微信浏览器
<?php /** php检测当前浏览器是否为微信浏览器 */ function is_weixin_browser(){ if(strpos($_SERVER['HTTP_USER_AGENT ...
- 【Android Developers Training】 63. 定义形状
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 关于CSS3中transform变换的小坑
2017年6月30日15:05:46 今天在写一个demo的时候,发现CSS3中transform变换的一个特性. 首先,我先描述一下我发现的情况(问题再现): <div class=" ...