Shell脚本之 一键安装samba服务,任何人都可以访问,目录只读——超详细!!!

一、shell要求

1、写一个shell脚本,能够实现一键安装并配置samba服务,执行该脚本时需要带一个路径(格式$0 $1) /opt/samba.sh /opt/samba
2、目录若存在,则自动创建,任何人都可以访问,并且不需要密码,并且是只读的

二、实验

创建编写一个samba.sh脚本

vi /opt/samba.sh

开始编写

#!/bin/bash
if [ "$#" -ne 1 ]
then
   echo "运行脚本格式为:$0 /dir/"
exit 1
else
   if ! echo $1 |grep -q '^/.*'
   then
        echo "请提供一个绝对路径。"
        exit 0
   fi
fi

if ! rpm -q samba >/dev/null
then
   echo "将要安装samba"
   sleep 1
   yum -y install samba
   if [ $? -ne 0 ]
   then
      echo "samba 安装失败"
      exit 1
   fi
fi

dirconf="/etc/samba/smb.conf"
cat >> $dirconf << EOF
[global]
        workgroup = workgroup
        security = user
        map to guest = bad user
[share]
        comment= share all
        path = $1
        browseable = yes
        public = yes
        writable = no
EOF

if [ ! -d $1 ]
then
   mkdir -p $1
fi

chmod 777 $1
chown nobody:nobody $1
echo "www.51xit.top" > $1/51xit.txt

systemctl start smb
if [ $? -ne 0 ]
then
   echo "samba服务启动失败,请检查配置文件是否正常"
else
   echo "samba服务启动正常"
fi

然后给执行权限

chmod +x /opt/samba.sh

执行测试一下

.opt/samba.sh  /opt/samba/

猜你喜欢

转载自blog.csdn.net/m0_46563938/article/details/108314807