shell提高篇

6. 條件判斷

1.基本語法java

[ condition ](注意condition先後要有空格)shell

注意:條件非空即爲true,[ fsdm ]返回true,[] 返回false。vim

2. 經常使用判斷條件bash

(1)兩個整數之間比較less

= 字符串比較函數

-lt 小於(less than) -le 小於等於(less equal)oop

-eq 等於(equal) -gt 大於(greater than)this

-ge 大於等於(greater equal) -ne 不等於(Not equal)spa

(2)按照文件權限進行判斷code

-r 有讀的權限(read) -w 有寫的權限(write)

-x 有執行的權限(execute)

(3)按照文件類型進行判斷

-f 文件存在而且是一個常規的文件(file)

-e 文件存在(existence) -d 文件存在並是一個目錄(directory)

3.案例實操

(1)23是否大於等於22

 

[fsdm@hadoop101 datas]$ [ 23 -ge 22 ]
[fsdm@hadoop101 datas]$ echo $?
0  (true)

 

(2)helloworld.sh是否具備寫權限

 

[fsdm@hadoop101 datas]$ [ -w helloworld.sh ]
[fsdm@hadoop101 datas]$ echo $?
0  (true)

 

(3)/home/fsdm/cls.txt目錄中的文件是否存在

 

[fsdm@hadoop101 datas]$ [ -e /home/fsdm/cls.txt ]
[fsdm@hadoop101 datas]$ echo $?
1   (false)

 

(4)多條件判斷(&& 表示前一條命令執行成功時,才執行後一條命令,|| 表示上一條命令執行失敗後,才執行下一條命令)

 

[fsdm@hadoop101 ~]$ [ condition ] && echo OK || echo notok
OK
[fsdm@hadoop101 datas]$ [ condition ] && [ ] || echo notok
notok

 

7. 流程控制(重點)

7.1 if 判斷

1.基本語法

複製代碼
if [ 條件判斷式 ];then 
  程序
fi
或者
if [ 條件判斷式 ] 
  then
    程序
fi
複製代碼

 

注意事項:

(1)[ 條件判斷式 ],中括號和條件判斷式之間必須有空格

(2)if後要有空格

2.案例實操

(1)輸入一個數字,若是是1,則輸出fsdm zhen shuai,若是是2,則輸出heihei zhen mei,若是是其它,什麼也不輸出。

 

複製代碼
[fsdm@hadoop101 datas]$ touch if.sh
[fsdm@hadoop101 datas]$ vim if.sh

#!/bin/bash

if [ $1 -eq "1" ]
then
        echo "fsdm zhen shuai"
elif [ $1 -eq "2" ]
then
        echo "heihei zhen mei"
fi

[fsdm@hadoop101 datas]$ chmod 777 if.sh 
[fsdm@hadoop101 datas]$ ./if.sh 1
fsdm zhen shuai
複製代碼

 

7.2 case 語句

1.基本語法

複製代碼
case $變量名 in 
  "值1") 
    若是變量的值等於值1,則執行程序1 
    ;;
  "值2") 
    若是變量的值等於值2,則執行程序2 
    ;;
  …省略其餘分支… 
  *) 
    若是變量的值都不是以上的值,則執行此程序
    ;;
esac
複製代碼

 

注意事項:

1) case行尾必須爲單詞「in」,每個模式匹配必須以右括號「)」結束。

2) 雙分號「;;」表示命令序列結束,至關於java中的break。

3) 最後的「*)」表示默認模式,至關於java中的default。

2.案例實操

(1)輸入一個數字,若是是1,則輸出fsdm,若是是2,則輸出heihei,若是是其它,輸出renyao。

 

複製代碼
[fsdm@hadoop101 datas]$ touch case.sh
[fsdm@hadoop101 datas]$ vim case.sh

!/bin/bash

case $1 in
"1")
        echo "fsdm"
;;

"2")
        echo "heihei"
;;
*)
        echo "renyao"
;;
esac

[fsdm@hadoop101 datas]$ chmod 777 case.sh
[fsdm@hadoop101 datas]$ ./case.sh 1
fsdm
複製代碼

 

7.3 for 循環

1.基本語法1

for (( 初始值;循環控制條件;變量變化 )) 
  do
    程序
  done

 

2.案例實操

(1)從1加到100

 

複製代碼
[fsdm@hadoop101 datas]$ touch for1.sh
[fsdm@hadoop101 datas]$ vim for1.sh

#!/bin/bash

s=0
for((i=0;i<=100;i++))
do
        s=$[$s+$i]
done
echo $s

[fsdm@hadoop101 datas]$ chmod 777 for1.sh 
[fsdm@hadoop101 datas]$ ./for1.sh 
「5050」
複製代碼

 

3.基本語法2

for 變量 in 值1 值2 值3… 
  do 
    程序 
  done

4.案例實操

(1)打印全部輸入參數

 

複製代碼
[fsdm@hadoop101 datas]$ touch for2.sh
[fsdm@hadoop101 datas]$ vim for2.sh

#!/bin/bash
#打印數字

for i in $*
    do
      echo "fsdm love $i "
    done

[fsdm@hadoop101 datas]$ chmod 777 for2.sh 
[fsdm@hadoop101 datas]$ bash for2.sh heihei haha hehe
ban xx love heihei
ban xx love haha
ban xx love hehe
複製代碼

 

(2)比較$*和$@區別

(a)$*和$@都表示傳遞給函數或腳本的全部參數,不被雙引號「」包含時,都以11 2 …$n的形式輸出全部參數。

 

複製代碼
[fsdm@hadoop101 datas]$ touch for.sh
[fsdm@hadoop101 datas]$ vim for.sh

#!/bin/bash 

for i in $*
do
      echo "fsdm love $i "
done

for j in $@
do      
        echo "fsdm love $j"
done

[fsdm@hadoop101 datas]$ bash for.sh heihei haha hehe
fsdm love heihei 
fsdm love haha 
fsdm love hehe 
fsdm love heihei
fsdm love haha
fsdm love hehe
複製代碼

 

(b)當它們被雙引號「」包含時,「$*」會將全部的參數做爲一個總體,以「11 2 …$n」的形式輸出全部參數;「$@」會將各個參數分開,以「11」「 2」…」$n」的形式輸出全部參數。

 

複製代碼
[fsdm@hadoop101 datas]$ vim for.sh

#!/bin/bash 

for i in "$*" 
#$*中的全部參數當作是一個總體,因此這個for循環只會循環一次 
        do 
                echo "fsdm love $i"
        done 

for j in "$@" 
#$@中的每一個參數都當作是獨立的,因此「$@」中有幾個參數,就會循環幾回 
        do 
                echo "fsdm love $j" 
done

[fsdm@hadoop101 datas]$ chmod 777 for.sh
[fsdm@hadoop101 datas]$ bash for.sh heihei haha hehe
fsdm love heihei haha hehe
fsdm love heihei
fsdm love haha
fsdm love hehe
複製代碼

 

7.4 while 循環

1.基本語法

while [ 條件判斷式 ] 
  do
    程序
  done

 

2.案例實操

(1)從1加到100

 

 

 

 

複製代碼
[fsdm@hadoop101 datas]$ touch while.sh
[fsdm@hadoop101 datas]$ vim while.sh

#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
do
        s=$[$s+$i]
        i=$[$i+1]
done

echo $s

[fsdm@hadoop101 datas]$ chmod 777 while.sh 
[fsdm@hadoop101 datas]$ ./while.sh 
5050
複製代碼

 

8. read讀取控制檯輸入

1.基本語法

read(選項)(參數)

選項:

-p:指定讀取值時的提示符;

-t:指定讀取值時等待的時間(秒)。

參數

變量:指定讀取值的變量名

2.案例實操

(1)提示7秒內,讀取控制檯輸入的名稱

 

複製代碼
[fsdm@hadoop101 datas]$ touch read.sh
[fsdm@hadoop101 datas]$ vim read.sh

#!/bin/bash

read -t 7 -p "Enter your name in 7 seconds " NAME
echo $NAME

[fsdm@hadoop101 datas]$ ./read.sh 
Enter your name in 7 seconds fsdm
fsdm
複製代碼

 

9. 函數

9.1 系統函數

1.basename基本語法

basename [string / pathname] [suffix]   (功能描述:basename命令會刪掉全部的前綴包括最後一個(‘/’)字符,而後將字符串顯示出來。

選項:

suffix爲後綴,若是suffix被指定了,basename會將pathname或string中的suffix去掉。

2.案例實操

(1)截取該/home/fsdm/fsdm.txt路徑的文件名稱

 

[fsdm@hadoop101 datas]$ basename /home/fsdm/fsdm.txt 
fsdm.txt
[fsdm@hadoop101 datas]$ basename /home/fsdm/fsdm.txt .txt
fsdm

 

3. dirname基本語法

dirname 文件絕對路徑 (功能描述:從給定的包含絕對路徑的文件名中去除文件名(非目錄的部分),而後返回剩下的路徑(目錄的部分))

4.案例實操

(1)獲取fsdm.txt文件的路徑

 

[fsdm@hadoop101 ~]$ dirname /home/fsdm/fsdm.txt 
/home/fsdm

 

9.2 自定義函數

1.基本語法

複製代碼
[ function ] funname[()]
{
Action;
[return int;]
}
funname
複製代碼

 

2.經驗技巧

(1)必須在調用函數地方以前,先聲明函數,shell腳本是逐行運行。不會像其它語言同樣先編譯。

(2)函數返回值,只能經過$?系統變量得到,能夠顯示加:return返回,若是不加,將以最後一條命令運行結果,做爲返回值。return後跟數值n(0-255)

3.案例實操

(1)計算兩個輸入參數的和

 

複製代碼
[fsdm@hadoop101 datas]$ touch fun.sh
[fsdm@hadoop101 datas]$ vim fun.sh

#!/bin/bash
function sum()
{
    s=0
    s=$[ $1 + $2 ]
    echo "$s"
}

read -p "Please input the number1: " n1;
read -p "Please input the number2: " n2;
sum $n1 $n2;

[fsdm@hadoop101 datas]$ chmod 777 fun.sh
[fsdm@hadoop101 datas]$ ./fun.sh 
Please input the number1: 2
Please input the number2: 5
7
相關文章
相關標籤/搜索