也不知道写啥,折腾了好几天,总算能用了,碰到各种问题,也不知道怎么总结,先这样吧,想起来再写。
#!/bin/bash
LOCAL_IP=`ifconfig -a|grep inet|grep -v 127.*|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
#LOCAL_IP=$(ifconfig -a|grep inet|grep -v 127.*|grep -v inet6|awk '{print $2}'|tr -d "addr:") # parm=$(command),parm=`command` same use
NFS_SERVER_IP=192.168.8.101
NFS_SERVER_DIR=/home/alon/linuxstudy/nfsdir
NFS_CLIENT_DIR=/root/mnt/
#MOUNT_INFO="df -T |grep $NFS_SERVER_IP|awk '{print $1}'"
MOUNT_INFO="df -T |grep 192.168.8.101|awk '{print \$1}'"
createDir()
{
while [ 1 ];do
if [ -e $NFS_CLIENT_DIR ];then #judge wether'/root/mnt' is exist,if not,creat dir
echo ">>>>>>>> /root/mnt/ already exist! " && break
else
echo ">>>>>>>> /root/mnt/ not exist,Create! "
mkdir $NFS_CLIENT_DIR && continue
fi
done
}
pingIP()
{
ping $NFS_SERVER_IP -c 2 -w 2 > /dev/null
if [ $? -eq 0 ];then
echo ">>>>>>>> $NFS_SERVER_IP Connect!"
else
echo ">>>>>>>> $NFS_SERVER_IP Unconnect! Please Check Netconfig!"
exit
fi
}
excuteMt()
{
tmpDir="$NFS_SERVER_IP"':'"$NFS_SERVER_DIR"
tmp=$(eval $MOUNT_INFO)
if [[ -n "$tmp" ]] && [[ "$tmp = $tmpDir" ]];then
echo ">>>>>>>> $tmp has already mounted!"
exit
fi
mount -t nfs -o nolock $NFS_SERVER_IP:$NFS_SERVER_DIR $NFS_CLIENT_DIR
if [ $? -ne 0 ];then
echo ">>>>>>>> Mount command error! "
exit
fi
tmp=$(eval $MOUNT_INFO)
if [ -n "$tmp" ];then
echo ">>>>>>>> $tmp "
echo ">>>>>>>> mount success!!"
else
echo ">>>>>>>> df command not find mount infomation"
exit
fi
}
excuteUmt()
{
tmp=$(eval $MOUNT_INFO)
if [ -z "$tmp" ];then
echo "<<<<<<<< Path have not been mounted!"
exit
fi
umount $NFS_CLIENT_DIR
if [ $? -eq 0 ];then
echo "<<<<<<<< umount success!!"
fi
}
mt_alon()
{
curDir=`pwd`
if [ $curDir = "$NFS_CLIENT_DIR" ];then
echo '>>>>>>>> Please exit current path'
exit
fi
if [[ "$1" = "mount" ]] || [[ "$1" = "MOUNT" ]];then
pingIP
createDir
excuteMt
elif [[ "$1" = "umount" ]] || [[ "$1" = "UMOUNT" ]];then
excuteUmt
elif [[ "$1" = "default" ]] || [[ "$1" = "DEFAULT" ]];then
echo "######## ServerIP : $NFS_SERVER_IP "
echo "######## ServerPath : $NFS_SERVER_DIR"
echo "######## LocalIP : $LOCAL_IP "
echo "######## LocalPath : $NFS_CLIENT_DIR"
else
echo ">>>>>>>> 'MOUNT/mount' is mount!"
echo ">>>>>>>> 'UMOUNT/umount' is umount!"
echo ">>>>>>>> 'default/DEFAULT' is default setting!"
fi
}
shellcmd=$1
mt_alon $shellcmd