0

I have a shell script like this.

line="$@" # get the complete first line which is the complete script path 
name_of_file = ${line%.*}
file_extension = ${line##*.}
if [ $file_extension == "php"]
then
ps aux | grep -v grep | grep -q "$line" || ( nohup php -f "$line" > /var/log/iphorex/$name_of_file.log & ) 
fi
if [ $file_extension == "java"]
then
ps aux | grep -v grep | grep -q "$line" || ( nohup java -f "$name_of_file" > /var/log/iphorex/$name_of_file.log & )
fi

here line variable has values like /var/www/dir/myphp.php or /var/www/dir/myjava.java.

The purpose of shell script is to check if these processes are already running and if not i try to run them.I get the following errors.

name_of_file: command not found
file_extension: command not found
[: missing `]'
[: missing `]' 

Any ideas?

4
  • For the bracket issues you need spaces between the close quote " and the close bracket ] like the following... "php" ] Commented Mar 11, 2011 at 13:57
  • What shell? Does the script start with a #!? Commented Mar 11, 2011 at 13:58
  • then i get name_of_file: command not found file_extension: command not found [: ==: unary operator expected [: ==: unary operator expected Commented Mar 11, 2011 at 13:59
  • (off topic): you can replace ps and series of greps with simply pgrep. For example: pgrep "$line" > /dev/null || your_cmd Commented Mar 11, 2011 at 14:24

3 Answers 3

3

Firstly, the shell processor treats the line:

name_of_file = ${line%.*}

as the execution of the command:

name_of_file

with the parameters:

= ${line%.*}

you need to write it as:

name_of_file=${line%.*}

This makes it into a variable=value. You need to repeat this for the file_extension = line as well.

Secondly, the if:

if [ $file_extension == "php"]

has exactly the same parsing problem, you must have a space before the trailing ], because otherwise the parser thinks you're checking if $file_extension is equal to the string: "php]"

if [ $file_extension == "php" ]
Sign up to request clarification or add additional context in comments.

Comments

1

delete the spaces first, maybe this will help...

name_of_file=${line%.*}
file_extension=${line##*.}

EDIT
Try this:

if [ $file_extension="php" ]
..
if [ $file_extension="java" ]

1 Comment

no help ` name_of_file: command not found infinity.sh: line 8: file_extension: command not found infinity.sh: line 9: [: ==: unary operator expected infinity.sh: line 13: [: ==: unary operator expected ` i got this error
1

The other answers are right that the problem in your script lies in stray spaces in your variable assignments and [ .. ] statements.

(off-topic. FYI)

I took the liberty of refactoring your script (untested!) just to highlight some alternatives, namely:

  • using pgrep instead of ps aux | grep .....
  • using case

-

#!/bin/bash
line="$@" # get the complete first line which is the complete script path 
name_of_file=${line%.*}

pgrep "$line" > /dev/null && exit # exit if process running

case "${line##*.}" in # check file extension
    php)
        nohup php -f "$line" > /var/log/iphorex/$name_of_file.log &
        ;;
    java)
        nohup java -f "$name_of_file" > /var/log/iphorex/$name_of_file.log &
        ;;
esac

1 Comment

@Chin:thanks for the extra stuff..looks more clean this way:)

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.