1.if 是单分支语句,使用格式如下: 
if condition ; then 
statement 
….. 
fi 
2.if … else 是双分支语句,使用格式如下: 
if condition ; then 
statement 
…. 
else 
statement 
…. 
fi 
3.if …elif…elif…else 是多分支语句,使用格式如下: 
if condition ; then 
statement 
…. 
elif condition ; then 
statement 
….. 
elif condition ; then 
statement 
….. 



else 
statement 
…. 
fi 
4.while 语句是循环语句,当条件满足的情况下才循环,不满足则退出循环,使用格式如下: 
while condition ; do 
statement 
….. 
done 
5.until 语句也是循环语句,当条件不满足的情况下循环,满足则不循环,使用格式如下: 
until condition ; do 
statement 
….. 
done 
6.case 也是循环语句,使用格式如下: 
case $var(变量) ; in 
value1) 
……

value2) 
…..

*)

.. 
.. 
.. 
esac

脚本练习:

1.计算100以内所有能被3整除的正整数的和。

复制代码代码如下:
#!/bin/bash 
declare -i sum=0 
for I in {1..100}; do 
if [ $[$I%3] -eq 0 ]; then 
let sum+=$I 
fi 
done 
echo " the sum is :$sum" 

2.计算100以内所有奇数的和以及所有偶数的和

复制代码代码如下:
#!/bin/bash 
# echo "exercise" 
declare -i sum1=0 
declare -i sum2=0 
for I in {1..100}; do 
if [ $[$I%2] -eq 0 ]; then 
let sum1+=$I 
else 
let sum2+=$I 
fi 
done 
echo " the even sum is :$sum1" 
echo " the oddnumber sum is :$sum2" 

3.判断/var/log下的文件的类型: 
如果是普通文件,则说明其为普通文件; 
如果是目录文件,则说明其为目录文件; 
如果是符号链接文件,则说明其为符号链接文件; 
否则,说明文件类型无法识别;

复制代码代码如下:
#!/bin/bash 
file1=/var/log/* 
for file in $file1 ; do 
if [ -f $file ]; then 
echo "$file is common file" 
elif [ -d $file ]; then 
echo "$file is directory file" 
else 
echo "$file is unknow" 
fi 
done 

4.写一个脚本,分别显示当前系统上所有默认shell为bash的用户和默认shell为 
/sbin/nologin的用户 
并统计各类shell下的用户总数,显示结果形如:bash,3user,they 
are:root,redhat,gentoo nologn,2user,they are:bin,ftp

复制代码代码如下:
#!/bin/bash 
file=/etc/passwd 
bsh='/bin/bash' 
nobsh='/sbin/nologin' 
use=`cat $file | cut -d: -f1` 
declare -i d1=0 
declare -i d2=0 
for I in $use ; do 
s=`grep "^$I:" $file | cut -d: -f7` 
if [ "$s" = $bsh ] ; then 
let d1=$d1+1 
muser=$I\,$muser 
elif [ "$s" = $nobsh ] ; then 
let d2=$d2+1 
suser=$I\,$suser 
fi 
done 
echo "BASH,$d1 users ,they are:" 
echo $muser 
echo 
echo "NOLOGIN,$d2 users ,they are:" 
echo $suser 

5.写一个脚本: 
(1)如果不存在,就创建文件/tmp/maintenance;如果存在,就事先删除 
(2)在文件/tmp/maintenance中添加如下内容: 
172.16.0.6 
172.16.0.17 
172.16.0.20 
(3)测试172.16.0.0/16网络内的所有主机是否在线,如果在线就显示其在线,如果此主机 
在/tmp/maintenance文件中,就显示其正处于维护状态;否则,就显示其状态未知;

复制代码代码如下:
#!/bin/bash 
file=/tmp/maintenace 
if [ -e $file ]; then 
rm -rf $file &> /dev/null 
fi 
touch $file 
cat >> $file << EOF 
172.16.0.6 
172.16.0.17 
172.16.0.20 
EOF 
bnet=172.16 
for net in {0..254} ; do 
for host in {1..254} ; do 
if ping -c1 -W1 $bnet.$net.$host &> /dev/null ; then 
echo "$bnet.$net.$host is up." 
elif grep "$bnet.$net.$host$" $file &> /dev/null ;then 
echo "$bnet.$net.$host is under maintenance." 
else 
echo "$bnet.$net.$host state is unknow." 
fi 
done 
done 

6写一个脚本,完成以下功能: 
(1)、提示用户输入一个用户名; 
(2)、显示一个菜单给用户,形如: 
U|u show UID 
G|g show GID 
S|s show SHELL 
Q|q quit 
(3)、提醒用户选择一个选项,并显示其所选择的内容;如果用户给的是一个非上述所提示的选项,则提醒用户给出的选项错误,并请其重新选择后执行; 
第一种方法:

复制代码代码如下:
#!/bin/bash 
read -p "Enter a user name:" username 
! id $username &> /dev/null && echo " Come on ,the user you input unexit" && exit 9 
cat << EOF 
U|u show UID 
G|g show GID 
S|s show SHELL 
Q|q quit 
EOF 
read -p "Enter your choice:" op 
case $op in 
U|u) 
id -u $username;; 
G|g) 
id -g $username;; 
S|s) 
grep "^$username\>" /etc/passwd | cut -d: -f7;; 
Q|q) 
exit 8 ;; 
*) 
echo "input option wrong ,quit" 
exit 9

esac

第二种方法:

复制代码代码如下:
#!/bin/bash 
read -p "Enter a user name:" username 
! id $username &> /dev/null && echo "Come on ,you input user notexit" && exit 9 
cat << EOF 
U|u show UID 
G|g show GID 
S|s show SHELL 
Q|q quit 
EOF 
read -p "Enter your option:" op 
while true; do 
case $op in 
U|u) 
id -u $username 
break

G|g) 
id -g $username 
break

S|s) 
grep "^$username\>" /etc/passwd | cut -d: -f7 
break

Q|q) 
exit 7 ;; 
*) 
read -p "Wrong option,Enter a right option:" op ;; 
esac 
done 

7写一个脚本: 
(1)、判断一个指定的脚本是否是语法错误;如果有错误,则提醒用户键入Q或者q无视错误并退出,其它任何键可以通过vim打开这个指定的脚本; 
(2)、如果用户通过vim打开编辑后保存退出时仍然有错误,则重复第1步中的内容;否则,就正常关闭退出。 
第一种方法

复制代码代码如下:
#!/bin/bash 
[ ! -f $1 ] && echo "wrong path." && exit 2 
until bash -n $1 &> /dev/null ; do 
read -p " Q|q to quit .others to edit:" opt 
case $opt in 
Q|q) 
echo "quit..." 
exit 3

*) 
vim $1

esac 
done 

第二种方法:

复制代码代码如下:
#!/bin/bash 
[ ! -f $1 ] && echo "wrong path." && echo "Quit!" && exit 9 
until bash -n $1 &> /dev/null ; do 
read -p " Grammar wrong please enter Q|q to quit .others to edit:" opt 
case $opt in 
Q|q) 
echo "quit..." 
exit 3

*) 
vim $1 
bash -n $1 &> /dev/null 
val=$? 
[ "$val" -ne 0 ] && echo "xiu gai bu cheng gong. "

esac 
done 

第三种方法

复制代码代码如下:
#!/bin/bash 
[ ! -f $1 ] && echo "Wrong scripts." && exit 4 
bash -n $1 &> /dev/null 
valu=$? 
until [ $valu -eq 0 ] ; do 
read -p "Q|q to quit ,others to edit:" op 
case $op in 
Q|q) 
echo "Quit." 
exit 9

*) 
vim $1 
bash -n $1 &> /dev/null 
valu=$?

esac 
done 

8 写一个脚本: 
查看redhat用户是否登录了系统,如果登录了,就通知当前脚本执行者“redhat 
is logged on.”;否则,就睡眠5秒钟后再次进行测试;直到其登录为止退出; 
第一种方法

复制代码代码如下:
#!/bin/bash 
who | grep "^redhat\>" &> /dev/null 
reval=$? 
until [ $reval -eq 0 ] ;do 
sleep 5 
who | grep "^redhat\>" &> /dev/null 
reval=$? 
done 
echo "redhat is logged on." 

第二种方法:

复制代码代码如下:
#!/bin/bash 
until who | grep "^redhat\>" &> /dev/null ; do 
sleep 5 
done 
echo "redhat is logged on" 

9写一个脚本: 
(1)、向系统中添加20个用户,名字为linuxer1-linuxer20,密码分别为其用户名,要使用while循环; 
(2)、要求:在添加每个用户之前事先判断用户是否存在,如果已经存在,则不再添加此用户; 
(3)、添加完成后,显示linuxer1-linuxer20每个用户名及对应的UID号码和GID号码,形如 stu1, UID: 1000, GID: 1000

复制代码代码如下:
#!/bin/bash 
declare -i I=1 
while [ $I -le 20 ] ; do 
l=linuxer$I 
let I++ 
! id $l &> /dev/null && useradd $l &> /dev/null && echo "the user:$l" | passwd --stdin $l &> /dev/null && echo "a dd user $l successfully" || echo " The user $l is exit. " 
d=`id -u $l` 
g=`id -g $l` 
echo " $l ,UID:$d,GID:$g " 
done 

bash 编程中循环语句用法的更多相关文章

  1. Shell编程之循环语句与echo的用法

    Shell编程之循环语句与echo的用法 目录 Shell编程之循环语句与echo的用法 一.echo用法 1. echo常用选项 2. 常用的转义字符 3. 特殊符号%.#的用法 二.循环语句 1. ...

  2. 04 shell编程之循环语句

    Shell编程之循环语句 学习目标: 掌握for循环语句编程 掌握while循环语句编程 目录结构: For循环语句 l  读取不同的变量值,以逐个执行同一组命令 l  For语句结构 for 变量名 ...

  3. shell编程之循环语句for / while / until

    shell编程之循环语句与函数 一.条件测试 二.循环语句 ① for循环语句结构(遍历) 示例1 示例2 ② while循环语句结构(迭代) 示例1 示例2 ③ until 循环语句结构 示例1 一 ...

  4. Shell编程中Shift的用法

    Shell编程中Shift的用法 位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shif ...

  5. “全栈2019”Java第二十七章:流程控制语句中循环语句for

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  6. “全栈2019”Java第二十六章:流程控制语句中循环语句do-while

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  7. “全栈2019”Java第二十五章:流程控制语句中循环语句while

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  8. Shell编程中Shift的用法【转】

    本文转载自:http://www.cnblogs.com/image-eye/archive/2011/08/20/2147153.html Shell编程中Shift的用法 位置参数可以用shift ...

  9. Shell脚本中循环语句for,while,until用法

    循环语句: Bash Shell中主要提供了三种循环方式:for.while和until. 一.for循环 for循环的运作方式,是讲串行的元素意义取出,依序放入指定的变量中,然后重复执行含括的命令区 ...

随机推荐

  1. nginx反向代理取得IP地址

    nginx反向代理后,在应用中取得的ip都是反向代理服务器的ip,取得的域名也是反向代理配置的url的域名,解决该问题,需要在nginx反向代理配置中添加一些配置信息,目的将客户端的真实ip和域名传递 ...

  2. Gnuradio 学习

    8月份就把usrp买过来了,不过由于一直比较忙, 也没怎么弄. 这几天准备学习学习,暂时也不知道能干些啥.不管怎么样先按教程走一遍,熟悉熟悉设备也是好的吧. 首先去Ettus 下载了一个官方指导,一步 ...

  3. 一个用python实现的东方时尚(驾校)抢课程序

    2014-12-23 东方时尚约车还要网上选课,资源太紧张了,于是我决定自己写一个抢票程序来帮我刷票. 第一步,抓包.浏览器登陆选课系统,用抓包工具观察网络行为,这里我用的fildder.这里抓包主要 ...

  4. The '_imaging' module for the PIL could not be imported: DLL load failed: The specified module could not be found

    I uninstalled the PIL and installed the Pillow and the problem solved.PIL worked fine for me with th ...

  5. WinPcap编程(前言&&学习)

    计算机网络课设要求用WinPcap写对ARP包的接收与发送. 所以学了一下WinPcap的内容. 参考的博客: http://blog.csdn.net/htttw/article/details/7 ...

  6. 读取word文件.选择了TextParse

    待续! 代码还没分离出来.. 分离后会上传上来 不支持wps 文件 . ]]>

  7. 初识DSP

    初识DSP 1.TI DSP的选型主要考虑处理速度.功耗.程序存储器和数据存储器的容量.片内的资源,如定时器的数量.I/O口数量.中断数量.DMA通道数等.DSP的主要供应商有TI,ADI,Motor ...

  8. LU分解(2)

    接着上次LU分解的讲解,这次给出使用不同的计算LU分解的方法,这种方法称为基于GaxPy的计算方法.这里需要了解lapapck中的一些函数.lapack中有一个函数名为gaxpy,所对应的矩阵计算公式 ...

  9. springMVC+ mongdb + redis +Jedis 环境搭建

    环境信息: JDK1.7 : Eclipse 4.4.1 ; mongdb + mongVUE:mongDB的安装 redis的下载和安装:window下安装redis maven3.0.5 新建ma ...

  10. [Java] List / ArrayList - 源代码学习笔记

    在阅读 List / ArrayList 源代码过程中,做了下面的笔记. LinkedList 的笔记较多,放到了另一篇博文 LinkedList / Queue- 源代码学习笔记 List List ...