bash脚本编程

bash脚本编程之用户交互:

    脚本参数

    用户交互:通过键盘输入数据

    read [option]…[name]…

    -p: "PROMPT"

     -t: TIMEOUT

  #!/bin/bash

  #

  read -p "Enter a username: " name

  [ -z "$name‘] && password="password"

  if id $name &> /dev/null; then

       echo "$name exists."

  else

  useradd $name

       echo "$password" | passwd –stdin $name &> /dev/null

       echo "Add user $name finished."

  fi

  bash -n /path/to/fome_script

       检测脚本中的语法错误

  bash -x /path/to/some_script

       调试执行

  示例:

  #!/bin/bash

  #Version:0.0.1

  #Version:MageEdu

  #Description: read testing

  read -p "Enter a disk special file:"diskfile

  [ -z "$diskfile" ] && echo "Fool" && exit 1

  if fdisk -l | grep "^Disk $diskfile" &> /dev/null; the

   fdisk -l &

  else

      echo "wrong disk special file."

   exit2

   fi

   过程是编程语言的执行流程:

       顺序执行

       选择执行

       循环执行

       选择执行:

  (1)&&,||

  (2)if 语句

  (3)case语句

          

   if语句:三种格式

   单分支的if语句

   if CONDITION,then

   if-true-分支;

   fi

   双分支的if语句

   fi CONDITION; then

   if-true-分支

   else

   if-false-分支

   fi

   多分支的if语句

   if CONDITION1; then

     条件1为真分支;

   elif CONDITION2;then

     条件2为真分支

   elif CONDITION3; then

     条件3为真分支

     …….

   elif CONDITIONn; then

    条件n为真分支  

   else

   所有条件均不满足是的分支

    fi

  注意:即便多个条件可能同时都能满足,分支之后执行其中一个,首先测试为“真”;


示例:脚本参数传递路径给脚本,判断此文件的类型;

   #!/bin/bash

   # 

   if [ $# -lt 1 ]; then

       echo "At least on path."

       exit 1

   fi

   if ! [ -f $1 ]; then

        echo "No such file."

        exit 2

   fi

   if [ -f $1 ]; then

       echo "Common file."

   elif [ -d$1 ]; then

        echo “Directory."

   elif [ -L $1 ]; then

         echo :Symbolic link."

   elif [ -b $1 ]; then

          echo "block special file."

   elif [ -c $1 ]; then

          echo "character special file."

   elif [ -S $1 ]; then

           echo "Socket file."

   else

         echo "Unkown."

   fi

   注意: if语句可嵌套;

   练习:写一个脚本

      (1)传递一个参数给脚本,此参数为用户名;

      (2)根据其ID号来判断用户类型;

           0:管理员

           1-999:系统用户

           1000+:登录用户

   先vim usertype.sh

   #!/bin/bash

   #

   [ $# -lt  1 ] && echo "At least on user name." && exit 1

   ! id $1 &>/dev/null && echo "No such user." && exit 2

   user id=$(id -u $1 )

   if [ $userid -eq 0 ]; then 

     echo "root"

  elif [ $userid -ge 1000 ]; then

     echo "login user."

  else

      echo "system user.”

  fi

  bash -n usertype.sh

  bash -x usertype.sh abc

  bash -x usertype.sh root

  bash -x usertype.sh centos

  bash -x usertype.sh daemon

  cat usertype.sh

 练习:写一个脚本

    (1)列出如下菜单给用户:

        disk)show disks info,

        mem)show memory info;

        cpu)show cpu info;

        *)quit;

  vim sysinfo.sh

  #!/bin/bash

  #

  cat  <<  EOF

  disk)  show disks info

  mem) show memory info

  cpu show  cpu info

  *) QUIT

  EOF

  read -p "Your choice : " option 

  if  [[ "$option"  == "disk" ]]; then

        fdisk -l /dev[sh]d[a-z]

  elif [[ "$option" == "mem" ]]; then

        free -m

  elif [[ "$option" == "cpu" ]]; then

        lscpu

  else

       echo "Unkown option."

   exit 3

  测试:bash -x sysinfo.sh 

 (2)显示用户给出自己的选择,而后显示对应其选择的相应系统信息;

    循环执行:将一段代码重复执行0、1或多次;

    进入条件;条件满足是才进入循环;

    退出条件;每个循环都应该有退出条件;以有机会退出循环;

 

  bash 脚本:

      for 循环

      while循环

      unit循环

  for循环:

     两种格式:

 (1)遍历列表

 (2)控制变量

     遍历列表:

     for VARAIBLE  in LIST; do

     循环体

    done

    进入条件:只要列表有元素,即可进入循环;

    退出条件:列表中的元素遍历完成;

         

    LISTT的生成方式:

  (1)直接给出;

  (2)整数列表

  (a)start..end}

  (b)seq [start  [incremtal]] last

  (3)返回列表命令

  (4)glob

  (5) 变量引用

       $ @,  $*

#!/bin/bash

#

for username in user21 user22 user23; do

        if id $username &> /dev/null; then

               echo "$username exists."

         else

              useradd $username && echo "Add user $username finished."

         fi

         done

示例:求100以内所有的正整数之和;

           sum=0+1

           sum=sum+2

           sum=sum+3

           sum=1+2+3+4…100

#!/bin/bash

#

declare -i sum=0

for i in  {1..100 }; do

          echo "\$sum is $sum, \$i is $i "

          sum=$ [ $sum+$i ]

done

echo $sum

示例:判断/var/log 目标下是每一个文件的内容类型

#!/bin/bash

#

for filename in /var/log/*;do

if [-f $filename ]; then

echo “Common file."

elif [ -d $filename ]: then

echo "Directory."

elif [ -L $filename ]; then

echo "Symbolic link."

elif [ -b $filename ]; then

echo "block special file."

elif [ -c$filename ]; then

echo "character special file."

elif [ -s $filename ]; then

echo "Socket file."

else

echo "Unkown."

done

原创文章,作者:shadow,如若转载,请注明出处:http://www.178linux.com/65494

(0)
shadowshadow
上一篇 2017-01-01 20:16
下一篇 2017-01-01 23:48

相关推荐

  • http协议(2.2&2.4)RPM搭建配置

    http协议(2.2&2.4)RPM搭建配置 httpd的程序环境: 主程序文件:/usr/sbin/httpd CentOS6系列(默认httpd2.2) 1)配置文件: /etc/httpd/conf/httpd.conf、  /etc/httpd/conf.d/*.conf 2)服务脚本: /etc/rc.d/init.d/…

    Linux干货 2016-10-17
  • 文件压缩和软件包管理

    Linux压缩打包 压缩是一种通过特定的算法来减小计算机文件大小的机制。有利于文件在网络上的传输,节约带宽。在Linux中,压缩是以后缀名区分文件的。(Linux中很少见)压缩文件都会呈现醒目的红色。 注意!以下压缩软件只能压缩文件而不能压缩目录。不能对目录直接压缩! compress命令 compress这个命令是非常老旧的一款命令,现在基本已经不用了 而…

    Linux干货 2016-08-24
  • N25期—第一周作业

    1、描述计算机的组成及其功能 计算机五大组成部件:运算器、控制器、存储器、输入设备和输出设备。 运算器和控制器统称中央处理器(CPU)。  存储器分成内存储器和外存储器两大类。  外存储器、输入设备和输出设备统称为外部设备。  中央处理器(CPU)  计算机的中央处理器又称为CPU,它是计算机的核心部分。主要由运算器和…

    Linux干货 2016-12-04
  • 学习宣言

    此刻打盹,你将做梦,此刻学习,你将圆梦! 为了财富自由,前进!

    Linux干货 2016-12-28
  • Linux基础知识之软件包管理(一)

    软件包管理 1.软件运行环境 运维工作的任务: 系统管理、库调用管理(开发接触的多,运维接触的少),安装配置某些程序包,让程序包运行并提供相应类型的服务 程序执行的过程:     程序源代码–>预处理–>编译–>汇编–>链接     预处理:将代码…

    Linux干货 2016-08-22
  • shell与kernel的理解

    Shell 的英文释义是外壳,与kernel 内核名词遥相呼应,一外一内,一壳一核。内核就像瑞士银行的金库,存放着客户的黄金等众多的(硬件)资产,闲杂人等(包括客户)当然是严格禁止入内的,而作为客户要存取金库中的资产则需要通过专门的特定管理人员代为操作完成,并把存取(操作)的结果呈现给客户。在Linux操作系统中,shell的职能就类似于金库的操作人员,客户…

    Linux干货 2016-02-14

评论列表(1条)

  • luoweiro
    luoweiro 2017-02-23 08:07

    希望基础知识总结后能有对应的实验脚本,脚本是练习的,不只是整理语法。