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

Maxim Dsouza
  • 1,507
  • 3
  • 19
  • 32

3 Answers3

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.

mikyra
  • 10,077
  • 1
  • 40
  • 41
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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

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

I had to run it as bash instead of sh.

Community
  • 1
  • 1
Maxim Dsouza
  • 1,507
  • 3
  • 19
  • 32