for my first year CS I need to know Shell Scripting, but I'm really terrible at this. I wrote this script for example
#!/bin/bash
[ $(whoami) != "root" ] && echo "Run as superuser" && exit 1
while getopts "ugb: " opt; do
case $opt in
u)
read -p "Wat is de naam van de user die je wilt toevoegen?: " username
[ $username == ""] && echo "Geen usename meegegeven!" && exit 1
read -p "Wat is de groepsnaam voor de gebruiker?: " group_name
[ "$(getent group $group_name)" == "" ] && echo "De groep bestaat niet, exiting" \ && exit 1
echo "adduser -m -g $group_name $username"
create_softlink $group_name $username
g)
read -p "Wat is de groepnaam voor de user?: " group_name
[ "$(getent group $group_name)" != "" ] && echo "De groep bestaat, exiting" \ && exit 1
addgroup "$groupname"
mk_shared_folder "$groupname"
b)
BACKUP_FOLDER="/var/backup"
[ ! -d "$BACKUP_FOLDER"] && mkdir -p "$BACKUP_FOLDER"
[ -d "$SHARED_FOLDER/$1"] && echo "Shared folder bestaat niet" && exit 1
zip "$SHARED_FOLDER/$1" "$BACKUP_FOLDER/$1-$(date +"%m-%d-%y")"
Now I want to execute this script. For example making a user. In a simple script, you simply do ./scriptname.sh
. But I don't know what the command is.
I'm sorry if this makes absolutely no sense. I'm really bad at this and barely understand any of it myself.
EDIT: my quesion has been flagged as a duplicate. However ./scriptname.sh will NOT run the script.
chmod u+X filename
. You can check, if the file is executable if you runls -l | grep filename
in your directory and check if the first column contains ax
bash -n <SCRIPTNAME>
finds (some) syntax errors and you can debug your script more easily, by running it withbash -x -u <SCRIPTNAME> [ARGUMENTS...]
. See the Bash manual for the meaning of-n
,-x
and-u
. It would also help, if you explained the intended function of the script and the meaning of the non-standard commandscreate_softlink
andmk_shared_folder
.