开发者

shell script error

I have a shell script like this.

line="$@" # get the complete first line which is the co开发者_高级运维mplete 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?


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" ]


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" ]


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜