0

i have the following function in my "update.sh" File:

#!/bin/bash
updateSymfony()
{
    path=/var/www/symfony

    echo "Falls der Symfony-Pfad abweichend von /var/www/symfony ist, geben Sie in bitte hier an, ansonsten nur Enter drücken"
    echo " "
    read pathanswer
    echo $path
    echo $pathanswer
    if [ -n $pathanswer ]
       then
            if [ $pathanswer!="" ]
                then
                    if [ $pathanswer!=" " ]
                        then
                            if [ -d "$pathanswer" ]
                                then
                                    $path=$pathanswer
                                else
                                    echo "Leider existiert der Pfad $pathanswer nicht"
                                    return
                            fi
                    fi
            fi
    fi

    if [ -d "$path" ]; then

            cp $path $path-backup -R
            cd $path

            php composer.phar selfupdate

            php composer.phar update

            clearCache

            clearCacheProd

            setOwner
    else
            echo "Es wurde kein Symfony gefunden"
    fi

}

I wanna ask if the prompt-answer is empty. This should be when i push the Enter-Key. But my if's didn't response the right answer.

What i am doing wrong?

2 Answers2

1

In single square brackets, always quote variables:

if [ -z "$var" ]

Or, switch to double squares:

if [[ -z $var ]]
choroba
  • 231,213
  • 25
  • 204
  • 289
0

You should use [[]] instead of [ as it is a bash built-in that behaves better.

Normally read the_text with a if [[ -z "$the_var" ]]; then echo "text empty" should suffice.

I would not check for ' ' chars. Instead i would check if the entered path exists or using a default you provide.

See Read a variable in bash with a default value on how to set a variable if no value was entered.

Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76