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.www58058.com/65494

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

相關推薦

  • Linux常用基礎命令

    pwd顯示工作目錄 [root@localhost ~]# pwd/root cd 切換回家目錄,注意:bash中,~表示家目錄: [root@localhost ~]# cd[root@localhost ~]# cd ~USERNAME:切換至指定用戶的家目錄;cd ~切換回自己的家目錄; cd -:在上一次所在目錄與當前目錄之間來回切換; […

    Linux干貨 2017-06-26
  • Linux磁盤及文件系統

    Linux磁盤及文件系統 一、硬盤 硬盤接口 PATA(Paralled Advanced Technology Attachment,并行的先進技術規范),也叫IDE(Integrated Drive Eelectronics,集成電路驅動設備),是作為一種簡單而廉價的接口而開發的。速度中等,容量大而且非常便宜。 串行ATA接口SATA(Serial AT…

    Linux干貨 2016-09-19
  • SED命令

    sed是一種流編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區中的內容,處理完成后,把緩沖區的內容送往屏幕。接著處理下一行,這樣不斷重復,直到文件末尾。文件內容并沒有改變,除非你使用重定向存儲輸出。Sed主要用來自動編輯一個或多個文件,簡化對文件的反復操作,編寫轉…

    Linux干貨 2017-05-01
  • N26-博客作業-week9

    1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現; #!/bin/bash # declare -i nologin_num=0,login_num=0 for i in $(cut -d: -f7 /etc/passwd); do if …

    Linux干貨 2017-04-01
  • lvm基本應用,擴展及縮減實現

    一、    LVM的創建     LVM因為他的可擴展和可伸縮的特性,被廣泛的應用于磁盤管理中,創建LVM則必須先創建pv然后創建vg最后才能創建lv,他們之間的關系如下:呈金字塔結構     1、創建查看pv     …

    Linux干貨 2016-03-09
  • 正則表達式基礎以及grep的簡單使用

    正則表達式基礎以及grep的簡單使用   1,定義 正則表達式是你所定義的模式模板,Linux可以用它來過濾文本。Linux工具(比如grep、sed、gawk)能夠在處理數據時使用正則表達式對數據進行模式匹配。如果數據匹配模式,它就會被接受并進一步處理;如果數據不匹配,它就會被濾掉。 2,正則表達式的原則 (1)正則表達式模式都區分大小寫。(2)…

    2017-04-09

評論列表(1條)

  • luoweiro
    luoweiro 2017-02-23 08:07

    希望基礎知識總結后能有對應的實驗腳本,腳本是練習的,不只是整理語法。

欧美性久久久久