bash 编程中循环语句用法

所属分类: 脚本专栏 / linux shell 阅读数: 1007
收藏 0 赞 0 分享
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

本文出自 “知识体系” 博客
更多精彩内容其他人还在看

Linux shell中的printf的详细用法

这篇文章主要介绍了Linux shell中的printf的详细用法的相关资料,希望能通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

shell字符截取命令之cut命令的实例详解

这篇文章主要介绍了shell字符截取命令之cut命令的实例详解的相关资料,希望通过本文大家能够掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux 在Bash脚本中怎么关闭文件描述符的实例

这篇文章主要介绍了Linux 在Bash脚本中怎么关闭文件描述符的实例的相关资料,希望通过本文能帮助到大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux使用文本编辑器vi常用命令

vi就是一种功能强大的文本编辑器,而vim则是高级版的vi,不但可以用不同颜色显示文字内容,还能进行诸如shell脚本、C语言程序编辑等功能,可以作为程序编辑器。下面通过本文给大家介绍linux 文本编辑器vi常用命令,一起看看吧
收藏 0 赞 0 分享

Linux 中unzip解压时中文乱码的解决办法

这篇文章主要介绍了Linux 中unzip解压时中文乱码的解决办法的相关资料,这里提供两种解决办法,希望能通过本文帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux shell ftp命令根据文件日期下载文件的方法

最近做项目遇到这样的需求要求ftp获取远程数据的文件,根据文件的创建时间点下载文件。下面小编给大家分享知识点小结,感兴趣的朋友要求看看吧
收藏 0 赞 0 分享

Linux下服务器重启的脚本命令

Linux关闭和重启系统一般使用相同的命令可以实现。下面脚本之家小编给大家带来了Linux下服务器重启的脚本命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Linux中的特殊符号与正则表达式

这篇文章主要介绍了Linux中的特殊符号与正则表达式,需要的朋友可以参考下
收藏 0 赞 0 分享

利用shell命令统计日志的方法详解

这篇文章主要给大家介绍了关于利用shell命令统计日志的方法,通过这个命令将会对大家的学习或者工作具有一定的参考学习价值,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Linux中的Syslog命令

syslog是Linux系统默认的日志守护进程,默认的syslog配置文件是/etc/syslog.conf文件。接下来通过本文给大家分享Linux中的Syslog命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享
查看更多