12.Linuxshell程式設計(指令碼傳參)

黑夜路口發表於2018-03-09

(建立於2018/1/31)

1.傳遞引數

Press ENTER or type command to continue
#!/bin/bash                                                                              
 
echo $0
echo $1
echo $2

執行命令:
./14.sh hello world bye

輸出結果:
./14.sh
hello
world

我們在傳遞了三個引數,hello world bye,但是指令碼中只接到了兩個,因為預設第一個引數$0得到
的是當前檔案路徑,是一個完整路徑,如果我們只要得到檔名不要路徑怎麼做呢

使用basename命令(basename的作用是從檔名中去除目錄和字尾,ru執行basename kernel/include/linux/stddef.h得到stddef.h)

  1 #!/bin/bash                                                                              
  2 
  4 filename=$(basename $0) //注意不要有空格
  5 
  6 echo filename
  7 echo $1
  8 echo $2
  
  輸出
  
tabstop=8root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# ./14.sh hello world bye
filename
hello
world

2.使用$@遍歷所有引數

#!/bin/bash                                                                              

echo "the number of params:$#"    //$#獲取引數個數

for param in "$@"
do
   echo "param:$param"
done

./14.sh hello=bitch world byebye
輸出:

the number of params:3
param:hello=bitch
param:world
param:byebye

getopt命令
。。。

shell中的變數

看下邊是一個簡單的指令碼,定義了三個變數,然後輸出,看下下邊的結果。我們期望列印的結果是一個數字,兩個字串。但是隻是列印了前兩個,然後報了一個錯誤,line 5: zhen: command not found,根據提示可以看到zhen 這個命令沒有找到,為什麼它把zhen這個字串當作了命令呢,原因就在於HEHE這個變數後邊的值由於沒有加雙引號,所以只把ren當作了HEHE的值,然後空格後邊的zhen被當作了命令對待,牢記一點,shell程式設計中,空格後邊的都會被當作命令對待,慎用空格

  1 #!/bin/bash                                                                        
  2 
  3 NDK=10
  4 JNI="ren zhen ming"
  5 HEHE=ren zhen ming
  6 
  7 echo $NDK
  8 echo $JNI
  9 echo $HEHE

執行結果
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./ren.sh 
./ren.sh: line 5: zhen: command not found
10
ren zhen ming

字串中也可以引用變數值

在下邊我把JNI這個變數放在了字串中進行列印

  1 #!/bin/bash                                                                        
  2 
  3 NDK=10
  4 JNI="ren zhen ming"
  5 HEHE="ren zhen ming"
  6 
  7 echo $NDK
  8 echo $JNI
  9 echo $HEHE
 10 
 11 echo "$JNI is a good man"

tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./ren.sh 
10
ren zhen ming
ren zhen ming
ren zhen ming is a good man
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ^C
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# 
將命令執行結果賦值給變數

例如我們想得到當前的時間和當前使用者並將它列印出來,這樣做怎麼樣。可以看到命令date和who只是被當作字串列印了

  1 #!/bin/bash                                                                        
  2 
  3 text=date
  4 
  5 text2=who
  6 
  7 echo $text
  8 echo $text2


tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./ren.sh 
date
who
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# 

那麼是不是應該在date命令前加上$,因為我們取變數值就是用的它,試一下,發現什麼都沒有列印

  1 #!/bin/bash                                                                        
  2 
  3 text=$date
  4 
  5 text2=$who
  6 
  7 echo $text
  8 echo $text2

實際上應該這樣做

  1 #!/bin/bash                                                                        
  2 
  3 text=$(date)
  4 
  5 text2=$(who)
  6 
  7 echo $text
  8 echo $text2

  tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./ren.sh 
Sat Sep 8 11:10:08 CST 2018
root pts/0 2018-09-08 10:13 (101.88.229.243) root pts/1 2018-09-08 10:34 (101.88.229.243) root pts/2 2018-09-08 11:08 (101.88.229.243)

或者這樣做,他們的結果是一樣的,注意這個符號不是單引號,而是你鍵盤右上角esc下邊的那個符號

  1 #!/bin/bash                                                                        
  2 
  3 text=`date`
  4 
  5 text2=`who`
  6 
  7 echo $text
  8 echo $text2


相關文章