Different in typing command and using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Different in typing command and using shell script
# 1  
Old 07-28-2006
Question Different in typing command and using shell script

Wondering why belows code works fine if i type in the command prompt but it seems not executed when i code them in a file with additional if-then-else statement.

Code:
#!/bin/sh

. $HOME/.profile

pid=`ps -ef | grep -w XELstnr | grep -v grep | awk {'print $2'}`

if [ $pid !='' ]
then
  echo "Stopping XELstnr....."
  kill -9 $pid
  echo "XELstnr stopped."
else
  echo "XELstnr is not running."

fi

exit


Please advise. Thanks.

Last edited by vino; 07-28-2006 at 05:14 AM.. Reason: Introduced code tags.
# 2  
Old 07-28-2006
The if-else construct should be

Code:
if [ ! -z $pid ]

rather than

Code:
if [ $pid != ' ' ]

# 3  
Old 07-28-2006
Vino, $pid must be between quotes :
Code:
if [ ! -z "$pid" ]

or
Code:
if [ -n "$pid" ]

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script not working but command works in command prompt

Hi everyone I have a problem with my script If I try directly this command /usr/bin/nice -n 19 mysqldump -u root --password="******" wiki_schneider -c | nice -n 19 gzip -9 > /point_de_montage/$(date '+%Y%m%d')-wiki-db.sql.gz It works But if I simply add this command in a script and... (8 Replies)
Discussion started by: picemma
8 Replies

2. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

3. Shell Programming and Scripting

In Shell Script Does Second Command Wait For First Command To Complete

Hi All, I have a question related to Shell scripting. In my shell script, I have following two commands in sequence: sed 's/^/grep "^120" /g' $ORIGCHARGEDAMTLIST|sed "s;$;| cut -f$FIELD_NO1 -d '|' | awk '{ sum+=\$1} END {printf (\"%0.2f\\\n\", sum/100)}' >$TEMPFILE mv $TEMPFILE $ORIGFILE... (3 Replies)
Discussion started by: angshuman
3 Replies

4. Shell Programming and Scripting

Repeat a command on linux without typing

Hi, I would like to run the clear command, for every 10 times I hit the enter button. Is there a way to track the number of times the enter button is hit and run the clear command? Thanks (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

When i am trying to execute export command within a shell script it is saying command not found.

I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found" My code is as follows: -------------------------- #!/bin/sh user="test" DIR="/bldtmp/"$user VIEW="test.view1" echo "TMPDIR before export... (4 Replies)
Discussion started by: dchoudhury
4 Replies

6. Shell Programming and Scripting

help with shell script: cp command not working, but mv command works...

Hello. I would like to ask your help regarding the cp command. We are using a cp command to create a back-up copy of our file but to no avail. It's just not working. We already checked the file and directory permissions and all seems correct. We have a script (ftp.script) which calls on... (1 Reply)
Discussion started by: udelalv
1 Replies

7. Shell Programming and Scripting

can anyone help with shell script command about searching word with grep command?

i want to search in the current directory all the files that contain one word for example "hello" i want to achieve it with the grep command but not with the grep * (2 Replies)
Discussion started by: aintour
2 Replies

8. Shell Programming and Scripting

Need to Write Shell Script based off of this shell command

I'm trying to read a bunch of log files and output the lines that contain particular strings. To accomplish this, I've been running the following from the command line: find . -name "*" | xargs grep " " | grep " " > output.txt Two grep statements are needed in case I'm looking for a... (3 Replies)
Discussion started by: Rally_Point
3 Replies

9. UNIX for Dummies Questions & Answers

Completing Command Entry without Typing

Hi, In my UNIX (KSH) sytem, in order to Complete Command Entry without Typing the whole path or name, I have to use ESC+\ where as I am used to press TAB key to do the same. Can anyone tell me where we need to change the settings of the keyboard combination so that when I press TAB key the file... (6 Replies)
Discussion started by: jisha
6 Replies

10. UNIX for Dummies Questions & Answers

Using the Esc key to complete command line typing

Dear Techs, In the past on a different box (HP) I use to be able to complete something I was typing by entering a portion of the filename in the pwd and I would hit the Esc key and it would match the rest of the filename. I did this without understanding how it was setup. Now I am on a new... (1 Reply)
Discussion started by: jxh461
1 Replies
Login or Register to Ask a Question