bash脚本执行的控制语句

简介:

一、bash脚本执行的控制语句:

1、顺序执行:默认,逐条执行各语句。


2、选择执行if:分支,条件判断,执行符合条件的分支。

(1)、单分支:

            if 条件;then

              分支

            fi

(2)、双分支

            if 条件;then

              分支1

            else

              分支2

            fi

(3)、多分支:

            if 条件1;then

              分支1

            elif 条件2;then

              分支2

            elif 条件3;then

              分支3

            ...

            else

               分支n

            fi

(4)例1:写一个脚本,实现如下功能:

        1、让用户通过键盘输入一个用户名

        2、如果用户存在,就显示其用户名和UID

        3、否则,就显示用户不存在。

    #!/bin/bash

    read -t 10 -p "Enter a username:" userName

    if id $userName $> /dev/null;then

    userID=`id -u $userName`

    else

    echo "$userName not exist"

    fi


例2:写一个脚本,实现如下功能:

    1、让用户通过键盘输入一个用户名,如果用户不存在就退出

    2、如果用户的UID大于等于500,就说明是普通用户

    3、否则,就说明是管理员或者系统用户。

#!/bin/bash

read -t 10 -p "Enter a userName" userName

if ! id $userName $> /dev/null;then

  echo "$userName not exist."

  exit 6

fi


userID=`id -u $userName`


if [ $userID -ge 500 ];then

   echo "A common user."

else 

   echo "Admin or System user."

fi


例3:写一个脚本,实现如下功能:

    1、让用户通过键盘输入一个用户名,如果用户不存在就退出

    2、如果用户的UID等于GID,就说它是“good guy”

    3、否则,就说它是“bad guy”


#!/bin/bash

read -t 10 -p "Enter a userName" userName

if ! id $userName $> /dev/null;then

  echo "$userName not exist."

  exit 6

fi


if [ `id -u $userName` -eq `id -g $userName` ];then

   echo "Good guy."

else 

   echo "Bad guy."

fi



3、循环执行:将同一段代码反复执行有限次。

(1)、for:实现知道循环次数,

for var_name in 列表;

do

循环体

done


例1、循环执行添加三个用户;xiaowang,xiaoli,xiaosun

#!/bin/bash

for userName in xiaowang xiaoli xiaosun;

do

useradd $userName

done


(2)列表的生成方法:

生成数字:{start..end},seq [start] [step] end


例1:生成10个用户,分别为user101...user110

for userName in `seq 101 110`;

do

 useradd user$userName

done


例2:将上面生成的10个用户删除,同时删除用户家目录

for userName in {101..110};

do

 userdel -r user$userName

done


例3:写一个脚本,用file命令显示/var/log目录下的每个文件的内容类型

#!/bin/bash

dirName=/var/log

for  fileName in $dirName/*

do 

file $fileName

done


例4:写一个脚本,要求如下

    a、创建/tmp/scripttest目录,用变量名保存目录名

    b、在目录里创建测试文件tfile1-tfile20

    c、创建用户testuser1和testuser2

    d、将tfile1-tfile10的属主和属组改为testuser1

    e、将tfile11-tfile20的属主和属组改为testuser2

#!/bin/bash

dirName=/tmp/scripttest

mkdir $dirName

for fileNo in {1..20}

 do

  touch $dirName/tfile$fileNo

 done

   useradd testuser1

   useradd testuser2


for fileNo in {1..10}

  do

        chown testuser1:testuser1 $dirName/tfile$fileNo

  done


for fileNo in {11..20}

  do

        chown testuser2:testuser2 #dirName/file$fileNo

  done

或者

#!/bin/bash

dirName=/tmp/scripttest

mkdir $dirName

   useradd testuser1

   useradd testuser2


for fileNo in {1..10}

 do

  touch $dirName/tfile$fileNo

  chown testuser1:testuser1 $dirName/tfile$fileNo

 done


for fileNo in {11..20}

 do

  touch $dirName/tfile$fileNo

  chown testuser2:testuser2 $dirName/tfile$fileNo

 done

例5:用for写一个脚本,要求如下:

    1、显示/etc/init.d/functions、/etc/rc.d/rc.sysinit和/etc/fstab各有多少行

    2、输出格式为:/etc/init.d/functions: 行数 lines.

                   /etc/rc.d/rc.sysinit: 行数 lines.

                   /etc/fstab: 行数 lines.

    #!/bin/bash

      for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab

      do

        lineCount=`wc -l $fileName|cut -d' ' -f1`

        echo "$fileName :$lineCount lines"

      done

例6:显示/etd/passwd中的第3、7和11行中的用户名和ID号

    

  #!/bin/bash

      for lineNo in 3 7 11

      do

        head -n #lineNo|tail -1|cut -d: -f1,3

      done


例7:判断当前系统上的所有用户是Good guy还是Bad guy,条件如下:

      1、如果用户的UID等于GID,就说它是“good guy”

      2、否则,就说它是“bad guy”

#!/bin/bash


for userName in `cut -d :-f1 /etc/passwd`; do


if [ `id -u $userName` -eq `id -g $userName` ];then

   echo "Good guy."

else 

   echo "Bad guy."

fi


done



例8:求200内所有3的整数倍的正整数的和

#!/bin/bash

declare -i sum=0

for i in {1..200};do

   if [ $[$i%3] -eq 0 ];then

let sum+=$i

   fi

done

echo "The sum is :$sum."


(3)、while:条件满足则循环,否则退出。

while 条件测试;do

    循环体;

done

例如:

求100以内所有正整数的和。

declare -i sum=0;i=1

while [ $i -le 100 ];do

 let sum+=$i

 let i++

done

echo $sum


(4)、until:条件不满足则循环,否则退出。看以看出它与while相反。

until 测试条件;do

 循环体

done


二、检查bash脚本语法命令:bash -n 脚本文件

    

三、执行脚本:bash 脚本文件,无需修改文件的执行权限











本文转自lzf0530377451CTO博客,原文链接:http://blog.51cto.com/8757576/1546226 ,如需转载请自行联系原作者





相关文章
|
2月前
|
监控 安全 Shell
防止员工泄密的措施:在Linux环境下使用Bash脚本实现日志监控
在Linux环境下,为防止员工泄密,本文提出使用Bash脚本进行日志监控。脚本会定期检查系统日志文件,搜索敏感关键词(如"password"、"confidential"、"secret"),并将匹配项记录到临时日志文件。当检测到可疑活动时,脚本通过curl自动将数据POST到公司内部网站进行分析处理,增强信息安全防护。
109 0
|
11天前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
18 0
Linux Bash 脚本中的 IFS 是什么?
|
12天前
|
存储 弹性计算 运维
用bash脚本创建目录
【4月更文挑战第29天】
15 3
|
2月前
|
存储 Unix Shell
【简化Cmake编译过程 】编写通用的bash脚本:简化和构建cmake高效自动化任务
【简化Cmake编译过程 】编写通用的bash脚本:简化和构建cmake高效自动化任务
49 0
|
7月前
|
监控 Shell Linux
使用Python和Bash编写内网监控工具:自动巡检脚本示例
为了确保内网的顺畅运行,自动化监控工具变得不可或缺。本文将介绍如何使用Python和Bash编写一个简单而强大的内网监控工具,它可以帮助您自动巡检网络和系统状态,及时发现问题并采取措施。
249 0
|
9月前
|
Shell
使用Bash备份脚本
使用Bash备份脚本
54 1
|
9月前
|
Shell 测试技术 Python
如何在Bash Shell脚本中使用`exec`命令?
如何在Bash Shell脚本中使用`exec`命令?
144 0
|
11月前
|
运维 Shell
善用chatGPT学习 | bash脚本如何判断字符串在数组中
善用chatGPT学习 | bash脚本如何判断字符串在数组中
150 0
|
12月前
|
安全 Shell 开发工具
记一次符合Google Coding Style的Bash脚本重构
记一次符合Google Coding Style的Bash脚本重构。最近我在思考这样一个问题,顺便看一下gpt对这个问题的解释。搜索发现:
记一次符合Google Coding Style的Bash脚本重构
|
12月前
|
Shell Linux 程序员
Shell-/bin/bash和/bin/sh解释器的误用引起的脚本语法错误
Shell-/bin/bash和/bin/sh解释器的误用引起的脚本语法错误
282 0