One-click installation of samba service, anyone can access, the directory is read-only
- [Shell requirements]
Write a shell script that can implement one-click installation and configuration of samba services. When executing the script, you need to bring a shared path, which is a shared directory. If the directory exists, samba needs to be created automatically.
It is required that anyone can access it, does not require a password, and is read-only.
-
[Shell analysis]
1. The user who needs to judge is whether the directory is an absolute path, that is, whether it starts with'/'
2. The script needs to judge whether the samba service has been installed, if it is already installed, there is no need to execute yum -y install samba
3, the configuration file can use sed -i
4, samba configuration can refer to http://www/apelearn.com/study_v2/chapter24.html#id1
Among them, the new samba configuration on centos7 is slightly different. Anything required by this question Anyone can access it, no need to modify the security item, just keep the default. -
[Shell configuration]
#!/bin/bash if [ "$#" -ne 1 ] ####判断位置变量的个数 是否为 1 then echo "运行脚本格式为:$0 /dir/" ####是的话,则输出“运行脚本格式为:$0 /dir/” exit 1 ####返回一层继续判定 else if ! echo $1 |grep -q '^/.*' ####判定 输出的$1位置上的格式是否是绝对路径 then echo "请提供一个绝对路径。" ####是的话则输出"请提供一个绝对路径。" exit 0 ####返回 fi fi if ! rpm -q samba >/dev/null ####判定samba服务是否安装,反馈的数据信息导入/dev/null then echo "将要安装samba" ####已安装,则输出 "将要安装samba" sleep 1 ####睡眠一秒先执行下面命令 yum -y install samba ####yum安装 if [ $? -ne 0 ] ####判断不等于0 then echo "samba 安装失败" ####则输出"samba 安装失败" exit 1 fi fi dirconf="/etc/samba/smb.conf" ####samba配置文件修改追加 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 ####开启samba服务 if [ $? -ne 0 ] ####判断开启成功开启服务 then echo "samba服务启动失败,请检查配置文件是否正常" else echo "samba服务启动正常" fi
-
[Shell test]
chmod +x /opt/samba.sh ###Set permissions
/opt/samba.sh /opt/samba/ ###Execute script