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/

测试安装成功会出现最后会出现

Complete!
samba服务启动正常

若是显示问题如下
在这里插入图片描述
解决办法是首先打开配置文件

[[email protected] ~]#systemctl /etc/samba/smb.conf

查看配置文件是否有问题
正确的配置文件如下

[global]
        workgroup = SAMBA
        security = user
        
        passdb backend = tdbsam
        
        printing = cups
        printcap name = cups
        load printers = yes
        cups options = raw
[homes]
        comment = Home Directories
        valid users = %S, %D%w%S
        browseable = No
        read only = No
        inherit acls = Yes
[printers]
        comment = All Printers
        path = /var/tmp
        printable = Yes
        create mask = 0600
        browseable = No
[print$]
        comment = Printer Drivers
        path = /var/lib/samba/drivers
        write list = root
        create mask = 0664
        directory mask = 0775
[global]
        workgroup = workgroup
        security = user
        map to guest = bad user
[share]
        comment= share all
        path = /opt/samba/
        browseable = yes
        public = yes
        writable = no        

更改后再去测试就能成功,若不成功具体文体具体分析
但是防火墙一定不能忘了关
下面是对于脚本的简单解释
1、if [ KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲ -ne 1 ];then …#表示参数个数,-ne是不等于
#判断参数个数
###############
if [ $# -ne 1 ];then
echo “参数个数不为1”
exit
else
echo “参数个数为1”
fi
################
./samba.sh /opt ###表示1个参数
./samba.sh ###表示没参数
./samba.sh /opt /mnt ###表示多个参数
2、判断用户输入的参数是不是以‘/’开头,因为绝对路径是以‘/’开头,如果不是绝对路径,后面针对该路劲不好操作
3、判断是否安装过samba包,rpm -q samba 命令执行后,如果是没安装,则会提示未安装,返回值为非0值,
用这个作为判断条件就可以了。
4、如果没安装,则用yum安装samba,判断返回值是否为0,如果不能正常安装samba包应该退出脚本
5、用cat >> $dirconf << EOF 将配置追加到配置文件里面
6、在Centos 7 s上启动服务命令为systemctl start smb   在Centos6 上启动是/etc/init.d/smb start

猜你喜欢

转载自blog.csdn.net/Laiyunpeng666/article/details/108270844