0

Having trouble trying to run this from bash :

[root@bryanserver ~]# $SPACER="#-------#" APACHE_ENABLED=`ls -1 "$HTTPD_HOSTS-EN" | grep ".conf" | sed s/.conf//` APACHE_COUNT=`echo -e "$APACHE_ENABLED" | wc -1` if [ -n "$APACHE_ENABLED" ]; then echo $SPACER echo "Apache enabled Sites: $APACHE_COUNT" echo "$APACHE_ENABLED" else echo $SPACER echo "There are no detectable nor delectable WebSites In Sight Blackbeard" fi
bash: syntax error near unexpected token `then

Is the problem with the code per se or with trying to use it from the shell rather than having it in a file and sourcing the file?

Entering this on the command line in Bash :

APACHE_ENABLED=`"$HTTPD_HOSTS_EN" | grep ".conf" | sed s/.conf//`
APACHE_COUNT=`echo -e "$APACHE_ENABLED" | wc -l`
if [ -n "$APACHE_ENABLED" ]; then
 echo $SPACER
 echo "Apache Enabled Sites: $APACHE_COUNT"
 echo "$APACHE_ENABLED" 
else
 echo $SPACER
 echo "There are no detected Apache Enabled Sites"
fi

produces this output:
APACHE_ENABLED=`"$HTTPD_HOSTS_EN" | grep ".conf" | sed s/.conf//`
APACHE_COUNT=`echo -e "$APACHE_ENABLED" | wc -l`
-bash: : command not found
[root@bryanserver ~]# APACHE_COUNT=`echo -e "$APACHE_ENABLED" | wc -l`
[root@bryanserver ~]# if [ -n "$APACHE_ENABLED" ]; then
>  echo $SPACER
>  echo "Apache Enabled Sites: $APACHE_COUNT"
>  echo "$APACHE_ENABLED"
> else
>  echo $SPACER
>  echo "There are no detected Apache Enabled Sites"
> fi  #

and hit enter and voila! this is the result: There are no detected Apache Enabled Sites [root@bryanserver ~]# and Bash is ready to do more work.

So, yes as Keith pointed out, I had mistaken an ell for a 1, and there are still some quirks, but it executes and issues a report. That's progress.

I am working here with some material from Charles Smith; he shared some scripts on GitHub; github.com/twohlix/HostingScripts/blob/master/listwww.

But I was using NotePad, and after editing some things, when I would put the text on the clipboard and unload it in Bash, what was I believe happening was I was getting the EOF issues. This SO question gave me an idea: bash EOF in if statement I used View / Show Line Endings in NotePad2, and copied my code, then pasted it into Bash, that worked.

3
  • 2
    I think you mean SPACER= not $SPACER= at the beginning of the line. Also, you need either newlines or semicolons to seperate commands in your if statement. Commented Aug 10, 2012 at 20:57
  • ...and this would probably be easier to debug if you first tried it using multiple lines. Commented Aug 10, 2012 at 20:58
  • Once you fix the syntax error(s), I think you'll find you want wc -l (lowercase letter ell) rather than wc -1 (digit one). Commented Aug 10, 2012 at 21:09

1 Answer 1

2

Things I see:

  • declaration of $SPACER shouldn't have the dollar sign
  • add semicolons ; between declarative statements

I think this is what you mean in a multi-line statement:

SPACER="#-------#"
APACHE_ENABLED=`ls -1 "$HTTPD_HOSTS-EN" | grep ".conf" | sed s/.conf//`
APACHE_COUNT=`echo -e "$APACHE_ENABLED" | wc -1`
if [ -n "$APACHE_ENABLED" ]; then 
    echo $SPACER;
    echo "Apache enabled Sites: $APACHE_COUNT";
    echo "$APACHE_ENABLED";
else 
    echo $SPACER;
    echo "There are no detectable nor delectable WebSites In Sight Blackbeard";
fi

In one line then, remember to add semicolons or the double ampersand && to separate statements:

SPACER="#-------#"; APACHE_ENABLED=`ls -1 "$HTTPD_HOSTS-EN" | grep ".conf" | sed s/.conf//`; APACHE_COUNT=`echo -e "$APACHE_ENABLED" | wc -1`; if [ -n "$APACHE_ENABLED" ]; then echo $SPACER; echo "Apache enabled Sites: $APACHE_COUNT"; echo "$APACHE_ENABLED"; else echo $SPACER; echo "There are no detectable nor delectable WebSites In Sight Blackbeard"; fi
Sign up to request clarification or add additional context in comments.

3 Comments

@Keith many thanks. as things turned out the problem was concerning the attempt to type in manually in bash what was meant to be a file, executed with command [ source filename ]. I have added some info which may be useful to others.
@MountainMan: You're thanking me for mzhang's answer; all I did was clean up the formatting.
@mzhang he's right, my bad. Thanks for getting the ball rolling :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.