return values of bash scripts

2011-12-20 Thread kc123

i,

Could someone please help me figure out why:
GNU bash, version 3.2.25(1)-release-(x86_64-redhat-linux-gnu)

1) Why when I do an echo, I get /bin/sh returned as well ?

For example, my script below called crond.sh:
#!/bin/bash

SERVICE='crond'
HOST=`hostname`

content=`ps auxw | grep [c]rond| awk '{print $11}'`
echo "CONTENT:" $content

and output is:
CONTENT: /bin/bash /bin/bash crond

Why are there 2 extra arguments printed (/bin/bash)?

Also how do I get rid of the "/bin/bash /bin/bash" and just have
"crond" as output ?

2) I get a different output from the same script if I run snmpwalk:
part of another snmp script which calls crond.sh:

$PLACE.1.1) echo "string"; echo `bash /directory/crond.sh`; exit 0 ;;

and output is:
STRING: "CONTENT: /bin/sh bash bash crond

Why are there 3 extra arguments printed this time. Would like to just
output "crond".

Many thanks. 
-- 
View this message in context: 
http://old.nabble.com/return-values-of-bash-scripts-tp33012795p33012795.html
Sent from the Gnu - Bash mailing list archive at Nabble.com.




Re: return values of bash scripts

2011-12-20 Thread Mike Frysinger
On Tuesday 20 December 2011 17:18:16 kc123 wrote:
> For example, my script below called crond.sh:
> ...
> content=`ps auxw | grep [c]rond| awk '{print $11}'`
> ...
> and output is:
> CONTENT: /bin/bash /bin/bash crond
> 
> Why are there 2 extra arguments printed (/bin/bash) ?

because you grepped your own script named "crond.sh"

make the awk script smarter, or use pgrep
-mike


signature.asc
Description: This is a digitally signed message part.


Re: return values of bash scripts

2011-12-20 Thread Bob Proulx
Mike Frysinger wrote:
> kc123 wrote:
> > For example, my script below called crond.sh:
> > ...
> > content=`ps auxw | grep [c]rond| awk '{print $11}'`
> > ...
> > and output is:
> > CONTENT: /bin/bash /bin/bash crond
> > 
> > Why are there 2 extra arguments printed (/bin/bash) ?
> 
> because you grepped your own script named "crond.sh"
> 
> make the awk script smarter, or use pgrep

You are using a system that supports various ps options.  The
equivalent of the BSD 'ps aux' is the SysV 'ps -ef'.  They are
similar.  But then instead of using 'ps aux' BSD style try not
printing the full path by using 'ps -e'.  You are matching your own
grep becuase it is in the argument list.

Then this can be made smarter by simply matching it as a string
instead of as a pattern.

  ps -e | awk '$NF=="crond"'

  ps -e | awk '$NF=="crond"{print$1}'

Bob


signature.asc
Description: Digital signature