ranjez
ranjez

Reputation: 349

shell script error

I have the following line in a shell script:

if [ -f /etc/init.d/tomcat6 && ps -C java|grep -qs 'java' ]; then

which throws up the following error when I try to run it:

line 12: [: missing `]'

I have a feeling that this is an encoding issue as I've been editing the file in Notepadd++ on a windows xp pc, I've ensured I've set the encoding to encode in UTF-8 without BOM and that all the line endings are linux style yet I still receive this error.

Can anyone help?

Thanks

Upvotes: 0

Views: 106

Answers (3)

Sjoerd
Sjoerd

Reputation: 75599

The syntax for and is -a.

You need to run ps -C java|grep -qs 'java', it is currently evaluated as an expression. Try this:

if [ -f /etc/init.d/tomcat6 -a $(ps -C java|grep -qs 'java') ]; then

Upvotes: 1

z3pr4h
z3pr4h

Reputation: 196

Try

if [ -f /etc/init.d/tomcat6 ] && ps -C java | grep -qs 'java'; then
    ...
fi

[ is basically an alias for the test command. test does not know anything about an argument ps. Alternatively you may use test explicitely (just to clarify syntax):

if test -f /etc/init.d/tomcat6  && ps -C java | grep -qs 'java'; then
    ...
fi

If you use [ instead of test, you are forced to end the expression with ].

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798666

The && ends your [ command.

if [ -f /etc/init.d/tomcat6 ] && ps -C java | grep -qs 'java'; then

Upvotes: 1

Related Questions