0

I'm working on a bash script for automatize some common tasks but I'm having some issues and I need some help. This is the script I'm talking about:

#!/usr/bin/env bash

PS3='Please enter your choice: '
options=("Prepare environment" "Create new group" "Add users to group" "Change directory ownership" "Change directory permissions" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Prepare environment")
            read -e -p "Enter the work directory: " -i "/var/www/html/" directory
            cd directory

            echo "Updating BOWER..."
            npm -g update bower

            echo "Updating COMPOSER..."
            composer self-update

            read -e -p "Enter the environment: " -i "prod" environment

            if [environment eq prod]
            then
                git fetch --all
                git reset --hard origin/master
                git pull

                composer install --no-dev --optimize-autoloader
                php app/console cache:clear --env=prod --no-debug
            else
                read -e -p "Enter the environment type: " -i "local" envtype

                if [envtype eq local]
                then
                    composer update
                    php app/console cache:clear --env=dev
                    php app/console cache:warmup
                else 
                    git fetch --all
                    git reset --hard origin/master
                    git pull

                    composer update
                    php app/console cache:clear --env=dev
                    php app/console cache:warmup
                fi  
            fi          

            echo "Cleaning the house and updating libraries..." 
            bower cache clean --allow-root
            bower prune --allow-root
            bower install --allow-root
            bower update --allow-root
        ;;
        "Create new group")
            read -e -p "Enter the group name: " -i "www-pub" groupname
            groupadd groupname
            echo "You have added a new group: " groupname
            ;;
        "Add users to group")
            read -e -p "Enter the group name: " -i "www-pub" groupname
            loop=true          # "true" is a command

            while true; do
                read -p "enter username: " username
                [[ -z $username ]] && break

                usermod -a -G groupname username # add user to group
                echo "You have added: " username " to group: " groupname
            done        
            ;;
        "Change directory ownership")
            read -e -p "Enter the group name: " -i "www-pub" "Enter the directory: " -i "/var/www/html" groupname directory
            chown -R root:groupname directory
            echo "You have changed the ownership for: " directory " to root:" groupname
            ;;
        "Change directory permissions")
            read -e -p "Enter the directory: " -i "/var/www/html" "Enter the directtory permissions (type -d) : " -i "2775" "Enter the directtory files (type -f) : " -i "0664"  directory folder files
            echo "Setting permissions " folder " to " directory
            find directory -type d -print0 | xargs -0 chmod permissions 
            echo "Setting permissions " files " to " directory " files"
            find directory -type f -print0 | xargs -0 chmod files 
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

Any time I run the script I got this output:

root@test-webserver:/var/www/sis-php-source# /home/script.sh
1) Prepare environment           4) Change directory ownership
2) Create new group              5) Change directory permissions
3) Add users to group            6) Quit
Please enter your choice: 1
Enter the work directory: /var/www/sis-php-source/
/home/script.sh: line 10: cd: directory: No such file or directory
Updating BOWER...
Updating COMPOSER...
You are already using composer version 07c644ac229a21df80180598d8bb9aaba232eecb.
Enter the environment: prod
/home/script.sh: line 20: [environment: command not found
Enter the environment type: local

Why cd command fail? Why environment the script tries to execute environment as command if it's a input var? Can any give me some help?

ReynierPM
  • 710
  • 5
  • 14
  • 30

1 Answers1

2

In bash, you get the value of a variable with $directory. All the details in the manual.

Unless you know specifically when to leave the quotes off, you should always quote "$variable" expansion.

Also, [...] is not syntax, [ is actually a command (an alias for the test command). Commands must be separated from their arguments with spaces.

if [ "$environment" = prod ]

See:

glenn jackman
  • 4,630
  • 1
  • 17
  • 20
  • Not to forget, `eq` must be `-eq` and works only for numbers. – ott-- Feb 07 '15 at 22:44
  • So, let me see if I get it vars should be read as `$groupname` so this is fine: `read -e -p "Enter the group name: " -i "www-pub" $groupname groupadd $groupname` is that sentence right? – ReynierPM Feb 07 '15 at 22:46
  • @ReynierPM, no... the `read` isn't using the value of the variable so you don't prefix the variable name with `$`. The code `read X` would read a value into the variable `X`. The code `echo $X` would write out the _value_ of `X`. – roaima Feb 07 '15 at 23:48
  • @roaima and should be quoted as __glenn__ said? For example in this case `echo "You have added a new group: " "$groupname"`? – ReynierPM Feb 07 '15 at 23:51
  • @ReynierPM, yes: it's good practice to double-quote variables when you use their value. Really I should have written `echo "$X"` in my example but I was trying to keep things simple. In Glenn's case you can also include the variable in the single string, viz `echo "You have added a new group: $groupname"` – roaima Feb 07 '15 at 23:55