歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> bash 腳本編程十七 NFS client自動部署

bash 腳本編程十七 NFS client自動部署

日期:2017/3/1 10:09:27   编辑:Linux編程

1.自動檢測並安裝nfs-common,

2.自動創建目錄並mount

3.同時檢查/etc/fstab文件中是否有配置,沒有則加入。確保下次開機能自動mount。

install.sh腳本:

  1. #!/bin/bash
  2. source ../../common/tool.sh
  3. nfsClient="nfs-common"
  4. nfsServerFolder=10.112.18.158:/opt/share
  5. nfsClientFolder=~/test_nfs_dir
  6. hasDpkg $nfsClient
  7. r=$?
  8. if [ $r -eq 1 ]
  9. then
  10. echo "$nfsClient was installed"
  11. else
  12. echo "$nfsClient was not installed"
  13. apt-get install $nfsClient
  14. fi
  15. #config /opt/share as nfs folder
  16. createFolder $nfsClientFolder
  17. mount -t nfs4 $nfsServerFolder $nfsClientFolder
  18. mountString="$nfsServerFolder $nfsClientFolder nfs rsize=8192,wsize=8192,timeo=14,intr"
  19. searchString="$nfsServerFolder"
  20. mountConfigFile="/etc/fstab"
  21. findStringInFile $searchString $mountConfigFile
  22. m=$?
  23. if [ $m -eq 1 ]
  24. then
  25. echo "auto mount was configured"
  26. else
  27. echo "auto mount was not configured, configuring..."
  28. echo $mountString >> $mountConfigFile
  29. fi

這裡用了一個新函數: findStringInFile

這個新函數放在tool.sh腳本中:

  1. #$1 search string
  2. #$2 file path
  3. #return 1 if found
  4. #return 0 if not found
  5. function findStringInFile {
  6. h=`grep "$1" $2`
  7. echo "h: $h"
  8. if [ -n "$h" ]
  9. then
  10. return 1
  11. else
  12. return 0
  13. fi
  14. }
Copyright © Linux教程網 All Rights Reserved