Shell 腳本介紹

1.當腳本中使用某個字符串較頻繁並且字符串長度很長時就應該使用變量代替

2.使用條件語句時,變量是必不可少的

3.引用某個命令的結果時,用變量替代

4.寫和用戶交互的腳本時,變量也是必不可少的


內置變量 $0, $1, $2…

數學運算a=1;b=2; c=$(($a+$b))或者$[$a+$b]


測試樣例:寫一個交互腳本:

[root@OBird shell]# vim 2.sh

#!/bin/bash

read  -p "please input anumber:" number

echo $number


[root@OBird shell]# sh 2.sh  #輸入什麼就輸出什麼

please input anumber:99999

99999


[root@OBird shell]# vim 2.sh

#!/bin/bash

read -t 3 -p "please input anumber:" number #“-t 3”加入等待3秒

echo $number


[root@OBird shell]# sh 2.sh  #超時會跳出 

please input anumber:

[root@OBird shell]# 

---------------------分割線------------------------------

[root@OBird shell]# vim 3.sh

#!/bin/bash

##

##

echo $1 $2 $0 $3

[root@OBird shell]# sh 3.sh

3.sh



[root@OBird shell]# vim 3.sh

#!/bin/bash

##

##

echo "\$1=$1"

echo "\$2=$2"

echo "\$0=$0"

echo "\$3=$3"


[root@OBird shell]# sh 3.sh

$1=

$2=

$0=3.sh

$3=


[root@OBird shell]# sh 3.sh 11 22 33  

$1=11

$2=22

$0=3.sh

$3=33


[root@OBird shell]# sh 3.sh 11 22    #寫兩個值$3 是沒有的

$1=11

$2=22

$0=3.sh

$3=



數學運算


[root@OBird shell]# a=1;b=2

[root@OBird shell]# c=$a+$b

[root@OBird shell]# echo $c

1+2

[root@OBird shell]# c=$[$a+$b]   #數學運算應該這樣寫

[root@OBird shell]# echo $c

3

------------------------分割線------------------------------

if 語句


[root@OBird shell]# vim if.sh

#!/bin/bash


a=5

if [ $a -gt 3 ]   #-gt 大於  < 小於 -lt  ==等於 -eq  != 不等於 -ne  >= 大於等於 -ge                            <= 小於等於 -le

then

    echo "a>3"

fi


[root@OBird shell]# sh if.sh 

a>3



[root@OBird shell]# sh -x if.sh  # -x  查看詳細過程

+ a=5

+ '[' 5 -gt 3 ']'

+ echo 'a>3'

a>3


[root@OBird shell]# vim if.sh

#!/bin/bash


a=5

if [ $a -gt 10 ]  #如果

then

    echo "a>10"

else              #否則

   echo "a<=10"

fi

~   

[root@OBird shell]# sh if.sh 

a<=10


[root@OBird shell]# vim if.sh 

#!/bin/bash


a=5

if [ $a -gt 10 ]

then

    echo "a>10"

elif [$a -lt 4 ]

   then

   echo  "a<4"

else

   echo "4<a<10"

fi


[root@OBird shell]# sh -x if.sh 

+ a=5

+ '[' 5 -gt 10 ']'

+ '[' 5 -lt 4 ']'

+ echo '4<a<10'

4<a<10

-------------------------分割線-----------------------

if 的幾種用法:

[ -f file ]判斷是否是普通文件,且存在 

[ -d file ] 判斷是否是目錄,且存在

[ -e file ] 判斷文件或目錄是否存在

[ -r file ] 判斷文件是否可讀

[ -w file ] 判斷文件是否可寫

[ -x file ] 判斷文件是否可執行


[root@OBird shell]# if [ -f 1.txt ]; then echo ok; fi

[root@OBird shell]# touch 1.txt

[root@OBird shell]# if [ -f 1.txt ]; then echo ok; fi #判斷是否存在1.txt

ok


[root@OBird shell]# if [ -r 1.txt ]; then echo ok; fi # -r 判斷文件是否可讀

ok


[root@OBird shell]# if [ -d /tmp/ ]; then echo ok; fi #判斷是否存在 目錄

ok


[root@OBird shell]# vim if2.sh

#!/bin/bash

read -p "please input a number:" n

m=`echo $n|sed 's/[0-9]//g'`

if [ -n "$m" ]  # -n 表示判斷變量是否不爲空

then

   echo "the character you input is not a number,please retry."

else

   echo $n

fi



[root@OBird shell]# sh if2.sh 

please input a number:iiiiiiiiii

the character you input is not a number,please retry.

[root@OBird shell]# sh if2.sh 

please input a number:90909090

90909090



[root@OBird shell]# sh -x if2.sh  #實例分解

+ read -p 'please input a number:' n

please input a number:oiu00909

++ echo oiu00909

++ sed 's/[0-9]//g'

+ m=oiu

+ '[' -n oiu ']'

+ echo 'the character you input is not a number,please retry.'

the character you input is not a number,please retry.


[root@OBird shell]# sh -x if2.sh  #實例分解

+ read -p 'please input a number:' n

please input a number:9909090909

++ sed 's/[0-9]//g'

++ echo 9909090909

+ m=

+ '[' -n '' ']'

+ echo 9909090909

9909090909



[root@OBird shell]# vim if2.sh

#!/bin/bash

read -p "please input a number:" n

m=`echo $n|sed 's/[0-9]//g'`

if [ -z "$m" ]  # -z 表示判斷變量是否  爲空,與-n 相反。

then

   echo $n


else

   echo "the character you input is not a number,please retry."



fi



[root@OBird shell]# sh -x if2.sh 

+ read -p 'please input a number:' n

please input a number:90909090

++ sed 's/[0-9]//g'

++ echo 90909090

+ m=

+ '[' -z '' ']'

+ echo 90909090

90909090

[root@OBird shell]# sh -x if2.sh 

+ read -p 'please input a number:' n

please input a number:jki0000

++ sed 's/[0-9]//g'

++ echo jki0000

+ m=jki

+ '[' -z jki ']'

+ echo 'the character you input is not a number,please retry.'

the character you input is not a number,please retry.


---------------------分割線-------------------------------

case 的用法:不是太常用。在啓動腳本中用的比較多。




---------------------分割線-------------------------------

for循環:


[root@OBird shell]# vim for.sh


#!/bin/bash

for i in `seq 1 10`

do

    echo $i

done


[root@OBird shell]# sh for.sh 

1

2

3

4

5

6

7

8

9

10



[root@OBird shell]# vim for.sh

#!/bin/bash

sum=0

for i in `seq 1 10`

do

    sum=$[$sum+$i]

done

echo $sum

~         

[root@OBird shell]# sh -x for.sh 

+ sum=0

++ seq 1 10

+ for i in '`seq 1 10`'

+ sum=1

+ for i in '`seq 1 10`'

+ sum=3

+ for i in '`seq 1 10`'

+ sum=6

+ for i in '`seq 1 10`'

+ sum=10

+ for i in '`seq 1 10`'

+ sum=15

+ for i in '`seq 1 10`'

+ sum=21

+ for i in '`seq 1 10`'

+ sum=28

+ for i in '`seq 1 10`'

+ sum=36

+ for i in '`seq 1 10`'

+ sum=45

+ for i in '`seq 1 10`'

+ sum=55

+ echo 55

55

---------------------分割線-------------------------------


while 循環 


[root@OBird ~]# vim while.sh  

#!/bin/bash

n=0

while [ $n -le 10 ]

do

      echo $n

      n=$[$n+1]

done

~      

[root@OBird ~]# sh -x  while.sh   #測試結果

+ n=0

+ '[' 0 -le 10 ']'

+ echo 0

0

+ n=1

+ '[' 1 -le 10 ']'

+ echo 1

1

+ n=2

+ '[' 2 -le 10 ']'

+ echo 2

2

+ n=3

+ '[' 3 -le 10 ']'

+ echo 3

3

+ n=4

+ '[' 4 -le 10 ']'

+ echo 4

4

+ n=5

+ '[' 5 -le 10 ']'

+ echo 5

5

+ n=6

+ '[' 6 -le 10 ']'

+ echo 6

6

+ n=7

+ '[' 7 -le 10 ']'

+ echo 7

7

+ n=8

+ '[' 8 -le 10 ']'

+ echo 8

8

+ n=9

+ '[' 9 -le 10 ']'

+ echo 9

9

+ n=10

+ '[' 10 -le 10 ']'

+ echo 10

10

+ n=11

+ '[' 11 -le 10 ']'

---------------------分割線-------------------------------

shell 中斷繼續退出:


[root@OBird shell]# vim for2.sh 


#!/bin/bash

for i in `seq 1 10`

do

    echo $i 

    if [ $i -eq 4 ]

    then

       break

    fi

    echo $i

done

~     

[root@OBird shell]# sh -x for2.sh 

++ seq 1 10

+ for i in '`seq 1 10`'

+ echo 1

1

+ '[' 1 -eq 4 ']'

+ echo 1

1

+ for i in '`seq 1 10`'

+ echo 2

2

+ '[' 2 -eq 4 ']'

+ echo 2

2

+ for i in '`seq 1 10`'

+ echo 3

3

+ '[' 3 -eq 4 ']'

+ echo 3

3

+ for i in '`seq 1 10`'

+ echo 4

4

+ '[' 4 -eq 4 ']'

+ break   #退出整個循環



[root@OBird shell]# vim for2.sh 

#!/bin/bash

for i in `seq 1 10`

do

    echo $i 

    if [ $i -eq 4 ]

    then

       continue  #結束本次循環

    fi

    echo $i

done

echo "for done"

~                    


[root@OBird shell]# sh for2.sh 

1

1

2

2

3

3

4

5

5

6

6

7

7

8

8

9

9

10

10

for done



[root@OBird shell]# vim for2.sh 

#!/bin/bash

for i in `seq 1 10`

do

    echo $i 

    if [ $i -eq 4 ]

    then

       exit

    fi

    echo $i

done

echo "for done"


[root@OBird shell]# sh for2.sh 

1

1

2

2

3

3

4

#此處無for done ,直接退出整個shell



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