1

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.

3
  • maybe you have to make the file executeable first by running chmod u+X filename. You can check, if the file is executable if you run ls -l | grep filename in your directory and check if the first column contains a x
    – Wayne_Yux
    Dec 5, 2015 at 12:36
  • There are a bunch of syntax and logic errors and variable escaping issues in your script. bash -n <SCRIPTNAME> finds (some) syntax errors and you can debug your script more easily, by running it with bash -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 commands create_softlink and mk_shared_folder. Dec 5, 2015 at 12:44
  • I'm voting to close this question as off-topic because homework.
    – Mateo
    Dec 6, 2015 at 14:45

1 Answer 1

2

To run a script, you need to do one of the following. Let's say your script is stored in /home/maarten/script.sh:

  1. Give it to bash as an argument:

    bash /home/maarten/script.sh
    
  2. Make it executable and run it by writing its path on the command line:

    chmod +x /home/maarten/script.sh
    /home/maarten/script.sh
    
  3. Make it executable and move it to a directory that is in your $PATH:

    chmod +x /home/maarten/script.sh
    mkdir -p ~/bin && mv /home/maarten/script.sh ~/bin
    

    If a bin directory exists in your home directory (~/bin means $HOME/bin), it will be automatically added to your $PATH next time you log in. To add it manually for the current session without logging out and then back in again, you can run source ~/.profile or PATH="$PATH:~/bin.


Now, your script also has some syntax errors. You need spaces after [ and before ], while must be ended with do, case must be ended with esac and each case statement with ;;, and you should always quote your variables:

#!/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
    ## I removed the echo since you probably want to run the command
    adduser -m -g "$group_name" "$username"
    ## I assume your real script has a function called 
    ## "create_softlink", right? This won't work otherwise
    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"
    ## Like "create_softlink" above, I am assuming you have a 
    ## function called "mk_shared_folder" somewhere. 
    mk_shared_folder "$groupname"
 ;;
 b)
    BACKUP_FOLDER="/var/backup"
    [ ! -d "$BACKUP_FOLDER" ] && mkdir -p "$BACKUP_FOLDER"
    ## Note that $1 is the 1st parameter passed to the script.
    ## However, since you're using getopts, and b expects an argument
    ## that argument is available as $OPTARG when the current $opt is b
    ## I have changed the script accordingly. 
    [ -d "$SHARED_FOLDER/$2" ] && echo "Shared folder bestaat niet" && exit 1
    zip "$SHARED_FOLDER/$OPTARG" "$BACKUP_FOLDER/$OPTARG-$(date +"%m-%d-%y")"
   ;;     
  esac
done
0

Not the answer you're looking for? Browse other questions tagged .