shell營運自動化if-read 今天的課程中我們將學習對使用者輸出的參數進行判斷。 例子:如果你說別人壞話?那就要挨打了。 故事2: 當你吃零食時,貓兒在你身邊叫的時候,你聽到了,然後你將手中的零食,分了一塊給小貓,小貓得到零食後,就離開你了。很顯然這隻貓是吃貸。 下面我們拆分這個故事,並實現它。放開那隻貓讓你來。 這個故事裡,聽,分,離開是關鍵字 主人公聽到後,做出的反應是分一點給貓,然後貓得到後就走了 2、1 read shell: read 從命令列讀取一行使用者輸入 根據編程來看用的最多的是: read -p “你的描述” ppp 意思: 在命令列輸出一個提示使用者輸入的等待,然後定義了變數叫ppp(名字隨便取,好的習慣是,變數名要用意義,好比,西瓜為什麼叫西瓜不叫馬鈴薯?因為這是常識) 然後使用者輸入後,將使用者的輸入讀進變數ppp 我們接到了變數,然後就應該幹嘛?對這變數使用之。2.1.sh #!/bin/sh #code by scpman #read read -p "What's your Name: " name echo "Hi," $name echo "now Time : `date`" 在這個指令碼中,我們提示使用者輸入名字,然後輸出hi,使用者輸出的名字,並在指令碼中輸出了當前系統時間。 2、2 if 在2.1中,我們已經接到使用者輸入了,那你給貓一隻狗,貓會吃嗎?任何時候,我們都不相信使用者的輸入!貓有原則,給只狗就是不吃,只吃老鼠(主人公在吃的零食) 如果你給的是老鼠,貓就吃,否則,就不吃。 那就用if來瞭解這個事吧 文法: if ....then...fi 如果給的是老鼠,就吃 if ...else....fi 如果給的不是老鼠,而是別的呢 If....elif...elif....else...fi 如果給的是老鼠,或者給餅乾,給別的等 if也在一些固定的運算式用來判斷一些問題: 列出一些: 條件運算式檔案運算式 if [ -f file ] 如果檔案存在if [ -d ... ] 如果目錄存在if [ -s file ] 如果檔案存在且非空 if [ -r file ] 如果檔案存在且可讀if [ -w file ] 如果檔案存在且可寫if [ -x file ] 如果檔案存在且可執行 整數Variant 運算式 if [ int1 -eq int2 ] 如果int1等於int2 if [ int1 -ne int2 ] 如果不等於 if [ int1 -ge int2 ] 如果>=if [ int1 -gt int2 ] 如果>if [ int1 -le int2 ] 如果<=if [ int1 -lt int2 ] 如果< 字串Variant 運算式 If [ $a = $b ] 如果string1等於string2 字串允許使用賦值號做等號if [ $string1 != $string2 ] 如果string1不等於string2 if [ -n $string ] 如果string 非空(非0),返回0(true) if [ -z $string ] 如果string 為空白if [ $sting ] 如果string 非空,返回0 (和-n類似) 條件運算式引用變數要帶$ ,並最好加” $變數名”(通過失敗的經驗得來的) 2.3 執行個體 1、如果給的是老鼠,貓就吃 思考:可能的情況: A如果你只是揮了下手,什麼都沒給 B 如果你給的不是老鼠 C 如果你給了老鼠,卻扔給了別人 #!/bin/sh #code by scpman #cat’s food read -p “Please input the cat’s food:” food if [ -n “$food” ] then echo $food fi 要是有輸入就輸出,沒有輸入就退出,並提示使用者什麼都沒輸入。 如果得到了輸入就進行判斷,如果給的是老鼠就說謝謝,如果不是老鼠就不要,並提示使用者 # vi 2.3.2.sh #!/bin/sh #code by scpman #cat's food read -p "Please input the cat's food: " food if [ -n "$food" ] then echo the food:$food else echo You give cat a null fi if [ "$food" = 'laoshu' ] then echo 3Q,cat love laoshu elif [ "$food" = 'pig' ] then echo sorry,cat not love pig else echo cat not like others! Fi # sh 2.3.2.sh Please input the cat's food: You give cat a null #這裡為空白應該提示完直接退出? cat not like others! #這一句怎麼也輸出了? Shell: exit 指令碼退出 # vi 2.3.3.sh #!/bin/sh #code by scpman #cat's food read -p "Please input the cat's food: " food if [ -n "$food" ] then echo the food:$food else echo You give cat a null exit ####看這裡,#號代表注釋,這樣當條件走這時,就直接提示並退出 了 fi if [ "$food" = 'laoshu' ] then echo 3Q,cat love laoshu elif [ "$food" = 'pig' ] then echo sorry,cat not love pig else echo cat not like others! Fi # sh 2.3.3.sh Please input the cat's food: You give cat a null 這次就對了 # sh 2.3.3.sh Please input the cat's food: pig the food:pig sorry,cat not love pig scpman# sh 2.3.3.sh Please input the cat's food: lkjdslfldsf the food:lkjdslfldsf cat not like others! scpman# sh 2.3.3.sh Please input the cat's food: laoshu the food:laoshu 3Q,cat love laoshu 當然用不了,寫這麼多行,就能搞定,本書的所有例子,不要求有多少行,只要求思路清晰 希望大家少寫NB的代碼(只有自己想幾次才看懂的代碼!) 看到著,免得被大家罵,咱們趕緊來一個有用的例子吧! 2.4 小執行個體: 判斷/usr/test目錄在不在,如果不在就輸出,然後建立這個目錄 建立完這個目錄後,讀/etc/passwd檔案,找出檔案中所有使用者並在/usr/test/下建立出各使用者名稱字對應的檔案 編程:先看懂你要做什麼,然後喝口水,想好了思路再開始,一氣呵成。 外行:好,這個需求簡單,幾分鐘寫完了,然後再看下要求,再重複來,鍵盤也是有尊嚴的 # vi 2.4.sh #!/bin/sh #code by scpman #mkdir ,touch file mulu="/usr/test" if [ -d "$mulu" ] then echo $mulu is have else mkdir $mulu echo $mulu create ok fi for username in `cat /etc/passwd |grep -v "^#" | awk -F':' '{print $1}'` do cd $mulu touch $mulu/$username done ls -l $mulu 指令碼中的for迴圈部分明天講 # sh 2.4.sh /usr/test create ok total 0 -rw-r--r-- 1 root wheel 0 Jan 20 07:42 _dhcp -rw-r--r-- 1 root wheel 0 Jan 20 07:42 _pflogd -rw-r--r-- 1 root wheel 0 Jan 20 07:42 bin -rw-r--r-- 1 root wheel 0 Jan 20 07:42 bind -rw-r--r-- 1 root wheel 0 Jan 20 07:42 daemon -rw-r--r-- 1 root wheel 0 Jan 20 07:42 games -rw-r--r-- 1 root wheel 0 Jan 20 07:42 kmem -rw-r--r-- 1 root wheel 0 Jan 20 07:42 mailnull -rw-r--r-- 1 root wheel 0 Jan 20 07:42 man -rw-r--r-- 1 root wheel 0 Jan 20 07:42 news -rw-r--r-- 1 root wheel 0 Jan 20 07:42 nobody -rw-r--r-- 1 root wheel 0 Jan 20 07:42 operator -rw-r--r-- 1 root wheel 0 Jan 20 07:42 pop -rw-r--r-- 1 root wheel 0 Jan 20 07:42 proxy -rw-r--r-- 1 root wheel 0 Jan 20 07:42 root -rw-r--r-- 1 root wheel 0 Jan 20 07:42 scpman -rw-r--r-- 1 root wheel 0 Jan 20 07:42 smmsp -rw-r--r-- 1 root wheel 0 Jan 20 07:42 sshd -rw-r--r-- 1 root wheel 0 Jan 20 07:42 toor -rw-r--r-- 1 root wheel 0 Jan 20 07:42 tty -rw-r--r-- 1 root wheel 0 Jan 20 07:42 uucp -rw-r--r-- 1 root wheel 0 Jan 20 07:42 www