bash 脚本编程十七 NFS client自动部署


1.自动检测并安装nfs-common,

2.自动创建目录并mount

3.同时检查/etc/fstab文件中是否有配置,没有则加入。确保下次开机能自动mount。

install.sh脚本:

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

相关内容