2

I am new to shell scripting. I was trying to write a script to which is throwing an unexpected end of file error. I have check other solutions on stackoverflow, however I coul not solve the here. Any help would be appreciated. My script is

if [ "$(/etc/init.d/tomcat7 status)" == " * Tomcat servlet engine is not running." ]; then /etc/init.d/tomcat7 start; fi

As suggested I tried creating this in linux and now I get an error [: * Tomcat servlet engine is not running.: unexpected operator

4
  • 3
    Did you create this script on the linux system or did you create it on Windows and then transferred the file? Commented Mar 24, 2013 at 11:24
  • This looks normal. Are you sure this is all of your script? Commented Mar 24, 2013 at 11:24
  • Yes this is all of the script. However as Joni said I created this on windows and then transferred it to linux. Commented Mar 24, 2013 at 11:28
  • == is not an operator for the [ command. Either use a single = or use [[ a == b ]] Commented Mar 24, 2013 at 17:16

3 Answers 3

2

Since you created the file on Windows your original problem was most likely due to a mismatch of line-break characters. Windows encodes line-breaks as CR-LF, while Linux/Unix uses just LF and Mac OS just CR. You can fix that with e.g.

recode ibmpc..latin1 your.sh

Also you should specify the interpreter in the first line of the script:

#!/bin/bash

Your script should probably look somewhat like this:

#!/bin/bash
if [ "$(/etc/init.d/tomcat7 status)" == " * Tomcat servlet engine is not running." ]; then
  /etc/init.d/tomcat7 start
fi
Sign up to request clarification or add additional context in comments.

Comments

2

As you wrote the script on windows most probably the problem is the different encoding of line-end characters \r\n on windows while a single \n on linux.

Try using dos2unix on your script and run it again.

Comments

0

Solved this looking at [ :Unexpected operator in shell programming.

I had to run it as bash instead of sh.

Comments

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.