Script programming question

Script programming question

Post by Alex Koga » Wed, 12 Jun 2002 01:46:42



Hi, I'm new at this and I need help. I'm trying to write a little
script that will check for a particular process and let me know if it's
running or not.
I use the following line to identify the process id by name:

ps -ef | awk '/DAS/ && \!/awk/ {print $2}'

I can run this at commandline just fine, but when I try to assign the
result of this to a variable I have a problem:


/awk/: Event not found.

Can someone suggest what am I doing wrong? I played around with different
quote marks and escape characters and didn't achieve anything.

Thank you for all your help.
Alex.

Below is the script I'm working on:

----------------------------------------
!#sh

PROC_EXISTS=`ps -ef | awk '/DAS/ && \!/awk/ {print $2}'`
if [ $PROC_EXISTS -gt 0 ]; then
   echo "still alive!"
else
   echo "it is dead..."
fi
----------------------------------------

 
 
 

Script programming question

Post by Barry Margoli » Wed, 12 Jun 2002 04:10:40




>Hi, I'm new at this and I need help. I'm trying to write a little
>script that will check for a particular process and let me know if it's
>running or not.
>I use the following line to identify the process id by name:

>ps -ef | awk '/DAS/ && \!/awk/ {print $2}'

>I can run this at commandline just fine, but when I try to assign the
>result of this to a variable I have a problem:


>/awk/: Event not found.

Are you sure you didn't forget the '\' before '!'?

Quote:>Can someone suggest what am I doing wrong? I played around with different
>quote marks and escape characters and didn't achieve anything.

>Thank you for all your help.
>Alex.

>Below is the script I'm working on:

>----------------------------------------
>!#sh

That should be "#!/bin/sh".

Quote:>PROC_EXISTS=`ps -ef | awk '/DAS/ && \!/awk/ {print $2}'`

You don't need to escape the ! in Bourne shell, only in C shell.

Quote:>if [ $PROC_EXISTS -gt 0 ]; then

If no process was found, $PROC_EXISTS will be empty, so the above line will
become:

if [ -gt 0 ]; then

which makes no sense.  Try:

if [ -n "$PROC_EXISTS" ]; then

Quote:>   echo "still alive!"
>else
>   echo "it is dead..."
>fi
>----------------------------------------

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Script programming question

Post by Alex Koga » Wed, 12 Jun 2002 05:27:28


Thank you, I tried that and here's what I get:

if the process is running I  get "proc23.sh: test: ] missing" error.
if it's not running: "proc23.sh: test: argument expected" error.

Here's my script:

#!/bin/sh

PROC_EXISTS=`ps -ef | awk '/DAS/ && !/awk/ {print $2}'`

if [ -n "$PROC_EXISTS"]; then

   echo "Yahoooo... mgr is still alive!"
else
   echo "Shit... mgr is dead!"
fi




> >Hi, I'm new at this and I need help. I'm trying to write a little
> >script that will check for a particular process and let me know if it's
> >running or not.
> >I use the following line to identify the process id by name:

> >ps -ef | awk '/DAS/ && \!/awk/ {print $2}'

> >I can run this at commandline just fine, but when I try to assign the
> >result of this to a variable I have a problem:


> >/awk/: Event not found.

> Are you sure you didn't forget the '\' before '!'?

> >Can someone suggest what am I doing wrong? I played around with different
> >quote marks and escape characters and didn't achieve anything.

> >Thank you for all your help.
> >Alex.

> >Below is the script I'm working on:

> >----------------------------------------
> >!#sh

> That should be "#!/bin/sh".

> >PROC_EXISTS=`ps -ef | awk '/DAS/ && \!/awk/ {print $2}'`

> You don't need to escape the ! in Bourne shell, only in C shell.

> >if [ $PROC_EXISTS -gt 0 ]; then

> If no process was found, $PROC_EXISTS will be empty, so the above line will
> become:

> if [ -gt 0 ]; then

> which makes no sense.  Try:

> if [ -n "$PROC_EXISTS" ]; then

> >   echo "still alive!"
> >else
> >   echo "it is dead..."
> >fi
> >----------------------------------------

> --

> Genuity, Woburn, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

--
Alex Kogan


Phone : 312/926-7329
Fax : 312/926-4220

 
 
 

Script programming question

Post by Barry Margoli » Wed, 12 Jun 2002 05:54:12




Quote:>Thank you, I tried that and here's what I get:

>if the process is running I  get "proc23.sh: test: ] missing" error.
>if it's not running: "proc23.sh: test: argument expected" error.

>if [ -n "$PROC_EXISTS"]; then

You're missing the space before the ']', so it's being appended to the
value of $PROC_EXISTS.

Sounds like you need to go back and review some basic scriting syntax.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Script programming question

Post by Shah » Wed, 12 Jun 2002 14:06:50


try:
if [ AA$PROC_EXISTS = AA ] ; then
   echo "still alive"
else
   echo $PROC_EXISTS
fi

> Thank you, I tried that and here's what I get:

> if the process is running I  get "proc23.sh: test: ] missing" error.
> if it's not running: "proc23.sh: test: argument expected" error.

> Here's my script:

> #!/bin/sh

> PROC_EXISTS=`ps -ef | awk '/DAS/ && !/awk/ {print $2}'`

> if [ -n "$PROC_EXISTS"]; then

>    echo "Yahoooo... mgr is still alive!"
> else
>    echo "Shit... mgr is dead!"
> fi




> > >Hi, I'm new at this and I need help. I'm trying to write a little
> > >script that will check for a particular process and let me know if it's
> > >running or not.
> > >I use the following line to identify the process id by name:

> > >ps -ef | awk '/DAS/ && \!/awk/ {print $2}'

> > >I can run this at commandline just fine, but when I try to assign the
> > >result of this to a variable I have a problem:


> > >/awk/: Event not found.

> > Are you sure you didn't forget the '\' before '!'?

> > >Can someone suggest what am I doing wrong? I played around with different
> > >quote marks and escape characters and didn't achieve anything.

> > >Thank you for all your help.
> > >Alex.

> > >Below is the script I'm working on:

> > >----------------------------------------
> > >!#sh

> > That should be "#!/bin/sh".

> > >PROC_EXISTS=`ps -ef | awk '/DAS/ && \!/awk/ {print $2}'`

> > You don't need to escape the ! in Bourne shell, only in C shell.

> > >if [ $PROC_EXISTS -gt 0 ]; then

> > If no process was found, $PROC_EXISTS will be empty, so the above line will
> > become:

> > if [ -gt 0 ]; then

> > which makes no sense.  Try:

> > if [ -n "$PROC_EXISTS" ]; then

> > >   echo "still alive!"
> > >else
> > >   echo "it is dead..."
> > >fi
> > >----------------------------------------

> > --

> > Genuity, Woburn, MA
> > *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
> > Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Script programming question

Post by those who know me have no need of my nam » Wed, 12 Jun 2002 17:42:32


in comp.unix.programmer i read:

Quote:>if [ AA$PROC_EXISTS = AA ] ; then

if PROC_EXISTS contains more than one `word' this will fail.

--
bringing you boring signatures for 17 years

 
 
 

Script programming question

Post by Barry Margoli » Thu, 13 Jun 2002 00:33:08




>try:
>if [ AA$PROC_EXISTS = AA ] ; then
>   echo "still alive"
>else
>   echo $PROC_EXISTS
>fi

Why is that better than my suggestion:

if [ -n "$PROC_EXISTS" ]; then

As the other poster pointed out, you *still* need to quote it to handle
multi-word $PROC_EXISTS.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Script programming question

Post by Shah » Thu, 13 Jun 2002 20:07:16


sorry if you understand that this approach is better - it is just an
approach.
further more it won't work if there is more than one process qualify
the regexp in the awk statment. (need to pipe something like awk '
{print $1} ' in the original script.

cheers




> >try:
> >if [ AA$PROC_EXISTS = AA ] ; then
> >   echo "still alive"
> >else
> >   echo $PROC_EXISTS
> >fi

> Why is that better than my suggestion:

> if [ -n "$PROC_EXISTS" ]; then

> As the other poster pointed out, you *still* need to quote it to handle
> multi-word $PROC_EXISTS.

 
 
 

1. /bin/script - programming questions

        I am not sure where to ask this - if someone knows of a better
newsgroup, I am all ears.

        I am trying to write a program that is almost identical to
script, but which can run other programs. The only source code I can
find for script is the stuff from BSD 4.4. The questions:

        1) The BSD 4.4 version is instructive, but it uses one routine,
"login_tty" that I cannot find on any other system (including Ultrix,
SunOS 4.X, Linux and BSD/OS). I don't have access to a BSD 4.4 system.
What does it do? I am guessing that it does the equivalent of
dup2(slave,0),dup2(slave,1),dup2(slave,2), but that's a guess. Anyone
got any clues?

        2) It seems that all the Unix script(1)'s (SunOS 4+5, Irix 4+5,
OSF, etc.) use the 3-process model of script except HP-UX 10.0. It
seems to use a 2-process model (in which, I assume, it does a lot of
selects). Which model is better?

        3) Does anyone have access to script.c from any of the Linux
versions?

        Any help or comments appreciated.

   ____________________________________________________________________

  | Systems Programmer              University of California, Davis    |
  | Graduate School of Management & GIS Center                         |

--
   ____________________________________________________________________

  | Systems Programmer              University of California, Davis    |
  | Graduate School of Management & GIS Research Lab

2. Add route using route(4)

3. network connection betwen NE2000 + 3c900

4. script programming question

5. HELP: Firewalling and OpenBSD

6. Question about programs reading scripts

7. backup - the reason

8. script questions - passing argument as argument to another program

9. exists any program (script) to port csh scripts to ksh

10. Help for script with programs controlling other programs needed

11. pick script in The UNIX Programming Environment (The UNIX Programming Environment )

12. running a program from within a script then returning to the script