bash 脚本编程十七 NFS client自动调度

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

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

2.自动创建目录并mount

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

install.sh脚本:

#!/bin/bash 

source ../../common/tool.sh

nfsClient="nfs-common"
nfsServerFolder=10.112.18.158:/opt/share
nfsClientFolder=~/test_nfs_dir

hasDpkg $nfsClient
r=$?

if [ $r -eq 1 ]
then
    echo "$nfsClient was installed"
else
    echo "$nfsClient was not installed"
    apt-get install $nfsClient
fi


#config /opt/share as nfs folder
createFolder $nfsClientFolder
mount -t nfs4 $nfsServerFolder $nfsClientFolder

mountString="$nfsServerFolder $nfsClientFolder nfs rsize=8192,wsize=8192,timeo=14,intr"
searchString="$nfsServerFolder"
mountConfigFile="/etc/fstab"

findStringInFile $searchString $mountConfigFile
m=$?

if [ $m -eq 1 ]
then
    echo "auto mount was configured"
else
    echo "auto mount was not configured, configuring..."
    echo $mountString >> $mountConfigFile
fi

这里用了一个新函数: findStringInFile

这个新函数放在tool.sh脚本中:

#$1 search string
#$2 file path
#return 1 if found
#return 0 if not found
function findStringInFile {
    h=`grep "$1" $2`
    echo "h: $h"
    if [ -n "$h" ]
    then
	return 1
    else
	return 0
    fi
}