LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-08-2007, 05:22 AM   #1
jons
LQ Newbie
 
Registered: Feb 2007
Posts: 3

Rep: Reputation: 0
Cool [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?
 
Old 02-08-2007, 05:50 AM   #2
smoon
LQ Newbie
 
Registered: Feb 2007
Posts: 4

Rep: Reputation: 0
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...
 
Old 02-08-2007, 05:50 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 02-08-2007, 06:02 AM   #4
jons
LQ Newbie
 
Registered: Feb 2007
Posts: 3

Original Poster
Rep: Reputation: 0
thanks, that'd done it.
 
Old 02-08-2007, 06:51 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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

Last edited by jschiwal; 02-08-2007 at 07:35 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
bash script causes trouble with cron Suinatsa Programming 10 06-14-2006 03:20 AM
Debugging a Bash Shell Script solarblast Linux - General 1 02-23-2006 01:44 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
Trouble with a basic bash script Tengil Linux - Newbie 5 03-04-2004 12:59 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration