shell习题-安装samba


写个shell脚本,能够实现一键安装并配置samba服务,执行该脚本时需要带一个参数,为共享的目录,目录可以不存在,若不存在,需要脚本自动创建。

 

参考答案:

#!/bin/bash

is_samba_installed=`rpm -qa|grep samba|wc -l`
if [ $is_samba_installed != 0 ]
then
    echo "You had already installed Samba."
    exit 0
fi

echo "It will install Samba."
sleep 1
cnfdir="/etc/samba/smb.conf"

chkok(){
    if [ $? != 0 ]
    then
        echo "Error, Please try again."
        exit 1
    fi
}

yum install -y samba
chkok

sed -i 's/MYGROUP/WORKGROUP/' $cnfdir
sed -i 's/user/share/' $cnfdir
sed -i '$a\[fish]' $cnfdir
if [ -d $1 ]
then
    cd $1
    echo "test" > test.txt
    sed -i '$a\[fish]\n\tcomment = Share All\n\tpath = "'$1'"\n\tbrowseable = yes\n\tpublic = yes\n\twritable = no' $cnfdir
else
    mkdir $1
    cd $1
    echo "test" > test.txt
    sed -i '$a\[fish]\n\tcomment = Share All\n\tpath = "'$1'"\n\tbrowseable = yes\n\tpublic = yes\n\twritable = no' $cnfdir
fi
/etc/init.d/smb start
chkok
echo "Please input [\\sambaIP\sharename] to access the share dir."