LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] having trouble debugging this bash script. (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-having-trouble-debugging-this-bash-script-526675/)

jons 02-08-2007 05:22 AM

[bash] having trouble debugging this bash script.
 
hello all.
im pretty new to bash and I was wondering if you could give me a pointer with this script:

-------------
Code:

#!/bin/bash
# Automatically generate a vhost.conf
# to allow access to common in path


VHOSTROOT=/var/www/vhosts/

# end.
clear


if [ -n "$1" ]
then
DOMAIN=$1
else
echo "Enter domain name: "
read DOMAIN;
fi


if [ ! "`ls -l $VHOSTROOT | grep $DOMAIN`" = "" ]
then
echo "> Found ${DOMAIN}"
PATHCONF=${VHOSTROOT}${DOMAIN}"/conf"
echo "> ${PATHCONF}"

if [ ! "`ls -l $PATHCONF | grep vhost.conf`" = "" ]
then
echo "> vhost.conf for this domain exists."
echo "> Overwrite (Y/N)"
read yesno

case "$yesno"
in [yY] )
cat << "" > ${PATHCONF}/vhost.conf
;;
[nN] )
echo " "
echo "Exiting..."
exit 0
;;
esac
else
echo "> no vhost.conf found"
fi


(
cat <<-EOFM8
<Directory /var/www/vhosts/${DOMAIN}/httpdocs/>
php_admin_value open_basedir "/var/www/common:/var/www/vhosts/${DOMAIN}/httpdocs:/tmp"
</Directory>
EOFM8
) > ${PATHCONF}/vhost.conf


echo "> vhost.conf created"
echo "> updating Plesk..."
/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=${DOMAIN}

echo "> done. Restarting Apache..."
apachectl -k restart
echo "> done."
echo " "
echo "vhost generation successful"
else
echo "no"
fi

----------------------

when running this via sh -x, i get:

Code:

injection_protection.sh: line 65: syntax error near unexpected token `else'
injection_protection.sh: line 65: `else'

can anyone enlighten me as to where abouts i've gone wrong?

smoon 02-08-2007 05:50 AM

I bet the troublesome line is the following from your case-statement:
Code:

cat << "" > ${PATHCONF}/vhost.conf
What is that supposed to do anyway?
Looks to me like you rather want the subshell doing `cat <<-EOFM8...' in there...

druuna 02-08-2007 05:50 AM

Hi,

I (and probably bash) don't understand what you are trying to to with this line:

cat << "" > ${PATHCONF}/vhost.conf

If you want to empty the file, just do: > ${PATHCONF}/vhost.conf

Hope this helps.

jons 02-08-2007 06:02 AM

thanks, that'd done it.
:)

jschiwal 02-08-2007 06:51 AM

Since the error refers to line numbers, it would be easier for use to examine the code if you used "cat -n script.sh" to add line numbers. It would also allow us to refer to linenumbers in answers.

Also, using indentation would help us understand the logic. The if's/then's/fi's are spread out and are easy to loose track of:
Code:

    1        #!/bin/bash
    2        # Automatically generate a vhost.conf
    3        # to allow access to common in path
    4       
    5       
    6        VHOSTROOT=/var/www/vhosts/
    7       
    8        # end.
    9        clear
    10       
    11       
    12        if [ -n "$1" ]; then
    13            DOMAIN=$1
    14            else
    15            echo "Enter domain name: "
    16            read DOMAIN;
    17        fi
    18       
    19       
    20        if [ ! "`ls -l $VHOSTROOT | grep $DOMAIN`" = "" ]; then
    21            echo "> Found ${DOMAIN}"
    22            PATHCONF=${VHOSTROOT}${DOMAIN}"/conf"
    23            echo "> ${PATHCONF}"
    24       
    25            if [ ! "`ls -l $PATHCONF | grep vhost.conf`" = "" ]; then
    26                echo "> vhost.conf for this domain exists."
    27                echo "> Overwrite (Y/N)"
    28                read yesno
    29       
    30                case "$yesno" in
    31                    [yY] )
    32                        cat << "" > ${PATHCONF}/vhost.conf
    33                        ;;
    34                    [nN] )
    35                        echo " "
    36                        echo "Exiting..."
    37                        exit 0
    38                        ;;
    39                esac
    40                else
    41                    echo "> no vhost.conf found"
    42            fi
    43       
    44       
    45            (
    46            cat <<-EOFM8
    47            <Directory /var/www/vhosts/${DOMAIN}/httpdocs/>
    48            php_admin_value open_basedir "/var/www/common:/var/www/vhosts/${DOMAIN}/httpdocs:/tmp"
    49            </Directory>
    50            EOFM8
    51            ) > ${PATHCONF}/vhost.conf
    52       
    53       
    54            echo "> vhost.conf created"
    55            echo "> updating Plesk..."
    56            /usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=${DOMAIN}
    57       
    58            echo "> done. Restarting Apache..."
    59            apachectl -k restart
    60            echo "> done."
    61            echo " "
    62            echo "vhost generation successful"
    63        else
    64            echo "no"
    65        fi

Oddly, when I moved the "; then"'s up, when I added indentation, line 65 actually has a "if" statement. OK, I could have re-edited it so they would look like
Code:

if something
    then
    statement
    statement
fi

which may be better.

Code:

if [ ! "`ls -l $PATHCONF | grep vhost.conf`" = "" ]; then
Maybe using something like
Code:

if [ -f $PATHCONF/vhost.conf ]
would be a cleaner way of checking if vhost.conf exists.


Code:

    21  if [ ! "`ls -l $VHOSTROOT | grep $DOMAIN`" = "" ]

...

    64  echo " "
    65  echo "vhost generation successful"
    66  else
    67  echo "no"
    68  fi

(using your original script so you can see which is line 65)

From the look at it it seems that the if/then/else/fi statements balance out but not the way you want. You have the if inside the case construct and the corresponding else outside of it.

Since ") > ${PATHCONF}/vhost.conf" will overwrite vhost.conf anyway, you don't need
Code:

    30                case "$yesno" in
    31                    [yY] )
    32                        cat << "" > ${PATHCONF}/vhost.conf
    33                        ;;

Then you can get rid of
Code:

else
echo "> no vhost.conf found"

So the psuedo code logic could be like

if vhost.conf exists
read yesno

case yesno in [yY])
exit;
esac

create vhost.conf


All times are GMT -5. The time now is 03:53 AM.