Problem with bash shell script program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with bash shell script program
# 1  
Old 08-08-2011
Problem with bash shell script program

Hi,

This is my program.
Code:
#!/bin/bash
today=`date +"%b-%d-%Y"`
SERVICE="pbxconnect.php"
if [ ps ax | grep -v grep | grep $SERVICE ]; then
  echo "pbx program is running"
else
  nohup php pbxconnect.php > logpbx-$today.txt &
fi

On executing using "sh myprogram.sh" , i get the following error.
Code:
myprogram.sh: line 4: [: missing `]'

My path to bash is fine since i get "/bin/bash" when i did whereis bash.

Please help me out what is wrong with this progam.

Regards,
Suresh.

Last edited by Scott; 08-08-2011 at 07:18 AM.. Reason: Added code tags
# 2  
Old 08-08-2011
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples! You also got a PM with a guide.


The commands will not be executed since either backticks ´ or dollar and brackets $(...) are missing, as well as surrounding double quotation marks "":
Code:
if [ "$(ps ax | grep -v grep | grep $SERVICE)" ]; then

# 3  
Old 08-08-2011
Oh and another one: You don't have to call it with
Code:
sh myprogram.sh

since you are already in a shell and ask to spawn a bash. Leave the sh out, just use ./myprogram.sh.
# 4  
Old 08-08-2011
It is working, thanks a lot

Hi zaxxon,

Thanks a lot. I was wondering for hours what is wrong with the program.

It is working fine.

Regards,
Suresh.
# 5  
Old 08-08-2011
Hi Zaxxon,

You can modify this line and try it works fine.

In the if loop whatever the grep pattern you are using it should within `(backticks) which is an intepreter for command execution otherwise it doesn't do command execution
Code:
if [ "`ps ax | grep -v grep | grep $SERVICE`" ]; then


Last edited by Scott; 08-08-2011 at 07:19 AM.. Reason: Code tags
# 6  
Old 08-08-2011
One more help

Hi zaxxon,

I need one more help. I want the progam "myprogram.sh" to run every one minute to check the php program is running or not. I set the cron like this:
Code:
*/1 * * * * /var/www/html/callreport/myprogram.sh

But the cron is not working, but if i run it manually it is working fine. I think it is to do with adding path to bash shell, but i don't know how to add the path in the cron.

Regards,
Suresh.

Last edited by Scott; 08-08-2011 at 07:19 AM.. Reason: Code tags
# 7  
Old 08-08-2011
Hi,

You can try to edit in crontab ( adding path to the crontab)

Code:

Example--->
Code:
crontab -e
*/1 * * * * /home/rsankar/Ravi/scripting/test.sh

Save--> :wq!
Code:
crontab: installing new crontab
 
crontab -l
*/1 * * * * /home/rsankar/Ravi/scripting/test.sh

Once it is scheduled you will get a mail like this "You have new mail in /var/spool/mail/rsankar".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pass RegEx to java program in bash script

I can't seem to get this right. I've tried it every way imaginable using every trick I see on stackexchange and such. No luck. So nothing major here, something like: #!/bin/bash SEARCH="ARG1 ARG2 '((^EXACT$)|(.*InTheMiddle*)|(^AtBeginning*))'" java -cp /my/class/path MyClassName $SEARCH... (3 Replies)
Discussion started by: stonkers
3 Replies

2. Shell Programming and Scripting

Help with Bash Shell Script Spell Checker program.

I have a majority of this problem done but seem to be struggling on the last couple of steps. Here is the whole problem to help you guys get a better understanding. Write a shell script that implements a simple spell checker. The filename you will use for your script will be your Z-id followed... (1 Reply)
Discussion started by: DsmRacer2k14
1 Replies

3. UNIX for Dummies Questions & Answers

Running a C/C++ program and/or bash script from a server

I wish to be able to give to a client the opportunity to : 0) Turn one of my ubuntu computers into a webserver 1) See a webpage after visiting a url where an external user/client can set a couple of variables (e.g. Number1= ?, Number2=?) 2) By pressing "run" the program runs on my machine 3)... (1 Reply)
Discussion started by: frad
1 Replies

4. Programming

Putting bash script in C program

suppose i have a bash script: #!/bin/bash echo "hello" echo "how are you" echo "today" how can i put the entire script above into a basic c program? i do not want to translate the bash code to a c code. i want C to run the bash code. is this possible? i found this on the... (15 Replies)
Discussion started by: SkySmart
15 Replies

5. Shell Programming and Scripting

Problem while Invoke Shell Script function from Java Program

Hi, I have create a Shell Script, with one function. I want to call the script file in Java Program. It working fine. but the problem is the function in the Shell Script is not executed. Please suggest me, Regards, Nanthagopal A (2 Replies)
Discussion started by: nanthagopal
2 Replies

6. Shell Programming and Scripting

Bash script to start program and answer prompts?

I'm trying to write a script the simplifies the execution of a program: After starting the program (sh ~/.mfix/model/make_mfix) I am prompted four times for options: Do you need SMP version? (y/n) Do you need DMP version? (y/n) Do you need debug version? (y/n) Force re-compilation of... (2 Replies)
Discussion started by: lanew
2 Replies

7. UNIX for Dummies Questions & Answers

Bash script to execute a program to rename files

I just can't figure it out , so please just give me a pice of advise how to: The existing Linux program foo2bar takes as its only argument the name of a single foo file and converts it to an appropriately-named bar file. Provide a script that when executed will run foo2bar against all foo... (4 Replies)
Discussion started by: raymen
4 Replies

8. Shell Programming and Scripting

bash script to check if a program is running

I'm a bit new to bash programming and I was assigned the job of writing a script that will check to see if a program server is running and to restart the program if it is not up. The script is supposed to check the program every hour (which I have looked up and I believe I know how to do) and send... (3 Replies)
Discussion started by: mcknz
3 Replies

9. Homework & Coursework Questions

Run Program from Bash CGI-Script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This is a problem I am having with my 2 semester senior project. I have a LAMP server running Ubuntu 9.10 with... (8 Replies)
Discussion started by: JMooney5115
8 Replies

10. Shell Programming and Scripting

problem accessing Multiple Variables from C Program to a Shell script

program name--test #!/bin/bash output1=`/home/user/a.c` output2=`/home/user/a.c` k=`$output1 + 1` m=`$output2 + 1` echo $k echo $m --------------------------------------------------------------------------- prgram name--a.c #include<stdio.h> int main() (1 Reply)
Discussion started by: sameworld1980
1 Replies
Login or Register to Ask a Question