變量參數簡單介紹:傳入路徑

   在shell腳本中有一種變量叫 位置變量,他是這樣表示的:$1,表示傳入的一個參數,直接跟在腳本後面,咱們這裏給你們介紹一個例子,這裏須要傳入一個路徑,而後來壓縮傳入的路徑下的全部文件shell

首先須要判斷這個路徑是否存在,是不是一個正確的路徑,是不是個文件等。。。bash

還要判斷傳入的參數的個數,若是不等於1,則提示只須要一個參數就夠了,直接退出腳本。less

下面是個截圖:ide

  
  
           
  
  
  1. #!/bin/bash 
  2. #判斷傳入的參數是否等於1,不然退出腳本 
  3. if [ $# -ne 1 ] 
  4. then 
  5.         echo "Too much or less parameter .Only be on parameter is good" 
  6.         sleep 10 
  7.         exit 0 
  8. fi 
  9. DATE=$(date +%Y%d%m) 
  10. path=$1 
  11. #判斷傳入的路徑是否存在 
  12. if [ -a $path ] 
  13. then 
  14.         echo "$path is exit,script will be continue...." 
  15. else 
  16.         echo "$path is not exit,please input a real path" 
  17.         sleep 5 
  18.         exit 0 
  19. fi 
  20. #判斷傳入的路徑是不是文件夾 
  21. if [ -d $path ] 
  22. then 
  23.         echo "$path is a directory,scritp will be continue...." 
  24. else 
  25.         echo "$path is not a direct ,script will be logout" 
  26.         sleep 5 
  27.         exit 0 
  28. fi 
  29. #開始壓縮 
  30. zip -r /root/shell$1.$DATE.zip $1 
  31. #根據返回值判斷是否壓縮,成功與否都寫入日誌 
  32. if [ $? -eq 0 ] 
  33. then 
  34.         echo "zip floder success" >> /root/shell$1.$DATE.log 
  35. else 
  36.         echo "zip failed " >> /root/shell$1.$DATE.log 
  37. fi 
相關文章
相關標籤/搜索