Skip to content

Instantly share code, notes, and snippets.

@greatbody
Created December 18, 2023 09:23
Show Gist options
  • Save greatbody/5a8e7ae4fc7b8682c9bfafedff71caf2 to your computer and use it in GitHub Desktop.
Save greatbody/5a8e7ae4fc7b8682c9bfafedff71caf2 to your computer and use it in GitHub Desktop.
Create a Samba share using script
#!/bin/bash
# With GPT-4 assitant
# Function to validate the existence of the provided path
validate_path() {
echo -e "\033[33mPath to share:\033[0m" # Yellow prompt
while true; do
read -r share_path
if [ -d "$share_path" ]; then
break
else
echo "The path does not exist. Please enter a valid path."
fi
done
}
echo -e "\033[33mWhat is your share name?\033[0m" # Yellow prompt
read -r share_name
echo -e "\033[33mComment of the share:\033[0m" # Yellow prompt
read -r share_comment
validate_path
echo -e "\033[33mPrevent macOS temp files (._.DS_Store, .DS_Store)? Yy/Nn\033[0m" # Yellow prompt
read -r prevent_macos_temp
echo -e "\033[33mRead only? Yy/Nn\033[0m" # Yellow prompt
read -r read_only
if [[ $read_only =~ ^[Yy]([Ee][Ss])?$ ]]; then
readonly_config=" read only = yes\n"
else
readonly_config=" read only = no\n"
# Ask for create mask input or use default
echo -e "\033[33mCreate mask (default 0600):\033[0m" # Yellow prompt
read -r create_mask
create_mask=${create_mask:-0600}
# Ask for directory mask input or use default
echo -e "\033[33mDirectory mask (default 0700):\033[0m" # Yellow prompt
read -r directory_mask
directory_mask=${directory_mask:-0700}
readonly_config+=" create mask = $create_mask\n"
readonly_config+=" directory mask = $directory_mask\n"
fi
echo -e "\033[33mAllow Guest? Yy/Nn\033[0m" # Yellow prompt
read -r allow_guest
if [[ $allow_guest =~ ^[Yy]([Ee][Ss])?$ ]]; then
guest_ok_config=" guest ok = yes\n"
else
guest_ok_config=""
fi
config="\n[${share_name}]\n comment = ${share_comment}\n path = ${share_path}\n"
if [[ $prevent_macos_temp =~ ^[Yy]([Ee][Ss])?$ ]]; then
config+=" veto files = /._*/.DS_Store/\n"
config+=" delete veto files = yes\n"
fi
config+=$readonly_config
config+=$guest_ok_config
sudo sh -c "echo \"$config\" >> /etc/samba/smb.conf"
sudo systemctl restart smbd
echo "Share of ${share_name} has been added"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment