linux下shell script學習(二)

今天再來學習shell腳本,下面對自己已經掌握的一些shell語法及命令進行總結!

1.read命令提示用戶輸入字符串

設置好PATH變量,這個比較好的是我們寫的腳本在執行時可以直接使用一些外部命令,而不必加上絕對路徑。

[zoulei@CentOS test]$ vim test.sh
  1 #!/bin/bash
  2 PATH=$PATH
  3 export PATH
  4 read -p "Your first name:" firstname
  5 read -p "Your last name:" lastname
  6 echo -e "Your full name:$firstname $lastname"
  7 exit 0
[zoulei@CentOS test]$ sh test.sh
Your first name:zou
Your last name:lei
Your full name:zou lei
[zoulei@CentOS test]$ echo $?
0

exit 0的作用是離開script且執行完腳本的同時回傳一個0給系統,然後在shell命令行輸入echo $?則可得到0的值。

2.test命令的測試功能

[zoulei@CentOS test]$ vim test.sh
  1 #!/bin/bash
  2 PATH=$PATH
  3 export PATH
  4 echo -e "Input name of the file that you want to check"
  5 read -p "Filename:" filename
  6 test -z $filename && echo "You must input a filename" && exit 0
  7 test ! -e $filename && echo "The file '$filename' DO NOT exist" && exit 0
  8 exit 0

[zoulei@CentOS test]$ sh test.sh
Input name of the file that you want to check
Filename:test.sh
[zoulei@CentOS test]$ sh test.sh
Input name of the file that you want to check
Filename:hh.sh
The file 'hh.sh' DO NOT exist
[zoulei@CentOS test]$
test -z string的作用是判定輸入的字符串是否爲零,若string爲空字符串,則爲true!

test -e filename表示filename文件是否存在,

test ! -x filename表示當filename不具有X時,回傳ture!這裏的x指e,f,d等測試標誌,x爲e用看來測試文件是否存在!

3.條件表達式

(1)if...elif..else...fi結構

[zoulei@CentOS test]$ vim test.sh
  1 #!/bin/bash
  2 PATH=$PATH
  3 export PATH
  4 read -p "Please input [Y/N]" choice
  5 if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
  6     echo "OK!continue"
  7     exit 0
  8
  9 elif [ "$choice" == "N" ] || [ "$choice" == "n" ];then
 10     echo "oh!interupt"
 11     exit 0
 12 else
 13     echo "Input [Y/N]"
 14 fi
 15 exit 0
 16

[zoulei@CentOS test]$ vim test.sh
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]y
OK!continue
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]n
oh!interupt
[zoulei@CentOS test]$
[zoulei@CentOS test]$ vim test.sh
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]y
OK!continue
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]n
oh!interupt
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]k
Input [Y/N]
[zoulei@CentOS test]$
(2).case...esac結構

[zoulei@CentOS test]$ vim test.sh
  1 #!/bin/bash
  2 PATH=$PATH
  3 export PATH
  4 read -p "Please input your choice:" choice
  5 case $choice in
  6     "one")
  7        echo "Your choice is ONE"
  8      ;;
  9     "two")
 10        echo "Your choice is TWO"
 11      ;;
 12     "three")
 13        echo "Your choice is THREE"
 14      ;;
 15      *)
 16        echo "Usage $0{one|two|three}"
 17      ;;
 18 esac
 19 exit 0

*************************************************************************************************************************************************************************************

注意:

case結構語法如下:

case  $變量名稱 in   <==關鍵字爲case,變量前有$符號

    "第一個變量名稱")

      ;;                   <==每個類型結尾使用兩個連續的分號來處理!

    "第二個變量名稱")

      ;;

     *)                <==最好一個變量的內容都會用*來代表其他值

  esac                 <==最終的case結尾!“case反過來寫表示結尾”

**************************************************************************************************************************************************************************************

[zoulei@CentOS test]$ sh test.sh
Please input your choice:one
Your choice is ONE
[zoulei@CentOS test]$ sh test.sh
Please input your choice:kk
Usage test.sh{one|two|three}
[zoulei@CentOS test]

**************************************************************************************************************************************************************************************

注意:

($0,$1...)爲shell script 的默認變量,$0表示執行的腳本文件名,$1表示腳本文件名後接的第一個參數,$2表示腳本文件名後接的第二個參數。

$#代表腳本文件名後接的參數個數,$@代表"$1","$2","$3"之意,每個變量是獨立的。

**************************************************************************************************************************************************************************************

4.循環

(1)while...do...done結構

[zoulei@CentOS test]$ vim test.sh
  1 #!/bin/bash
  2 PATH=$PATH
  3 export PATH
  4
  5 while [ "$choice" != "yes" ]
  6 do
  7     read -p "Give your choice [yes/no]:" choice
  8 done
  9 echo "OK! you input the correct answer"
 10 exit 0

[zoulei@CentOS test]$ sh test.sh
Give your choice [yes/no]:no
Give your choice [yes/no]:yes
OK! you input the correct answer
[zoulei@CentOS test]$

(2)for...do...done結構

[zoulei@CentOS test]$ vim test.sh
  1 #!/bin/bash
  2 PATH=$PATH
  3 export PATH
  4
  5 for animal in cat dog elephant
  6 do
  7     echo "There are ${animal}s..."
  8 done
  9  #echo "OK! you input the correct answer"
 10 exit 0
[zoulei@CentOS test]$ sh test.sh
There are cats...
There are dogs...
There are elephants...
*************************************************************************************************************************************************************************************

注意:

語法爲:

for var in con1 con2 con3 ...

do

            程序段

done
這個$var的變量內容在循環工作時:1.第一次循環時,$var的內容爲con1;2.第二次循環時,$var的內容爲con2;3.第三次循環時,$var的內容爲con3;......

****************************************************************************************************************************************************************************************************

再看個例子:

[zoulei@CentOS test]$ vim test.sh
  1 #!/bin/bash
  2 PATH=$PATH
  3 export PATH
  4 users=$(cut -d ':' -f1 /etc/passwd)
  5
  6 for username in $users
  7 do
  8     id $username
  9    
 10 done
 11
 12 exit 0
[zoulei@CentOS test]$ sh test.sh
uid=0(root) gid=0(root) 組=0(root)
uid=1(bin) gid=1(bin) 組=1(bin),2(daemon),3(sys)
uid=2(daemon) gid=2(daemon) 組=2(daemon),1(bin),4(adm),7(lp)
uid=3(adm) gid=4(adm) 組=4(adm),3(sys)
uid=4(lp) gid=7(lp) 組=7(lp)
uid=5(sync) gid=0(root) 組=0(root)
uid=6(shutdown) gid=0(root) 組=0(root)
uid=7(halt) gid=0(root) 組=0(root)
uid=8(mail) gid=12(mail) 組=12(mail)
uid=10(uucp) gid=14(uucp) 組=14(uucp)
uid=11(operator) gid=0(root) 組=0(root)
uid=12(games) gid=100(users) 組=100(users)
uid=13(gopher) gid=30(gopher) 組=30(gopher)
uid=14(ftp) gid=50(ftp) 組=50(ftp)
uid=99(nobody) gid=99(nobody) 組=99(nobody)
uid=81(dbus) gid=81(dbus) 組=81(dbus)
uid=113(usbmuxd) gid=113(usbmuxd) 組=113(usbmuxd)
uid=69(vcsa) gid=69(vcsa) 組=69(vcsa)
uid=499(rtkit) gid=497(rtkit) 組=497(rtkit)
uid=170(avahi-autoipd) gid=170(avahi-autoipd) 組=170(avahi-autoipd)
uid=498(pulse) gid=496(pulse) 組=496(pulse)
uid=68(haldaemon) gid=68(haldaemon) 組=68(haldaemon)
uid=38(ntp) gid=38(ntp) 組=38(ntp)
uid=48(apache) gid=48(apache) 組=48(apache)
uid=497(saslauth) gid=76(saslauth) 組=76(saslauth)
uid=89(postfix) gid=89(postfix) 組=89(postfix),12(mail)
uid=173(abrt) gid=173(abrt) 組=173(abrt)
uid=42(gdm) gid=42(gdm) 組=42(gdm)
uid=74(sshd) gid=74(sshd) 組=74(sshd)
uid=72(tcpdump) gid=72(tcpdump) 組=72(tcpdump)
uid=500(lingyun) gid=500(lingyun) 組=500(lingyun)
uid=32(rpc) gid=32(rpc) 組=32(rpc)
uid=29(rpcuser) gid=29(rpcuser) 組=29(rpcuser)
uid=65534(nfsnobody) gid=65534(nfsnobody) 組=65534(nfsnobody)
uid=501(zoulei) gid=501(zoulei) 組=501(zoulei)
uid=502(leilei) gid=502(leilei) 組=502(leilei)
這是通過管道命令的cut找出單純的賬號名稱,以id檢查用戶的標識符!

5.函數function

[zoulei@CentOS test]$ vim test.sh
  1 #!/bin/bash
  2    PATH=$PATH
  3    export PATH
  4 function print(){
  5        echo "Your choice is $1"
  6 }
  7 echo "This program will print your choice! "
  8
  9 case $1 in
 10   "one")
 11    print 1
 12    ;;
 13  "two")
 14    print 2
 15    ;;
 16  "three")
 17    print 3
 18    ;;
 19   *)
 20   echo "Usage $0 {one|two|three}"
 21    ;;
 22 esac
 23   exit 0

**************************************************************************************************************************************************************************************************

注意:

function語法:

function fname() {

    程序段

}

那個fname是我們自定義的執行命令的名稱,程序段就是我們要執行它的內容。

***************************************************************************************************************************************************************************************************

[zoulei@CentOS test]$ sh test.sh two
This program will print your choice!
Your choice is 2

其實shell腳本中的函數與C語言中的函數用法是一樣的,都是定義之後,直接調用就可以,可以簡化程序代碼,shell腳本與C定義函數不同的是shell腳本是將一些命令封裝到自定義的函數裏面,當然兩者定義函數時,都可以嵌套的!

   相信學習了shell script的這些語法,看懂別人寫的腳本應該是沒有什麼問題了,只要很熟悉shell命令,那麼我相信也能夠寫出自己想要的腳本,不過一切的要自己多去寫才行!

好了,shell 腳本先總結到這裏了!以後自己要多多練習。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章