Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

return values of bash scripts

46 views
Skip to first unread message

kc

unread,
Dec 20, 2011, 4:58:46 PM12/20/11
to
Hi,

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:
#!/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 ?

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 the script:

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


STRING: "CONTENT: /bin/sh bash bash crond

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

Many thanks.

Barry Margolin

unread,
Dec 21, 2011, 1:41:15 AM12/21/11
to
In article
<262db63a-fb5c-4b4c...@i6g2000vbh.googlegroups.com>,
kc <keec...@gmail.com> wrote:

> Hi,
>
> 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:
> #!/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 ?

What's the name of your script? Does it have "crond" in its name? Or
is "crond" one of the arguments? In either of those cases, the ps line
for the script itself will be matched by the grep.

Why not use: awk '$11 == "crond" {print $11}'

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

Make sure the ps|grep doesn't match any bash lines.

>
> 2) I get a different output from the same script if I run snmpwalk:
> part of the script:
>
> $PLACE.1.1) echo "string"; echo `bash /directory/crond.sh`; exit 0 ;;
>
>
> STRING: "CONTENT: /bin/sh bash bash crond
>
> Why are there 3 extra arguments printed this time. Would like to just
> output "crond".

Every script that has crond in its name or arguments is going to be
matched by the grep. You need to be more specific in what you match, if
you want to exclude these.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

DennisW

unread,
Dec 21, 2011, 3:28:33 AM12/21/11
to
Use more quotes.

The square brackets in your grep command are being stripped by the
shell.

#!/bin/bash
service='crond'
host=$(hostname)
content=$(ps auxw | grep '[c]rond' | awk '{print $11}')
echo "CONTENT: $content"

Also use $() instead of the archaic backticks.

Use pgrep if you have it.

Use lowercase or mixed case variable names. Only use all-caps variable
names for system-defined environment and shell variables.

Barry Margolin

unread,
Dec 21, 2011, 10:29:38 AM12/21/11
to
In article
<dfece208-6b8e-4399...@e2g2000vbb.googlegroups.com>,
While it's a good recommendation, it's not the problem this time. The
brackets would only be stripped if there's a file named "crond" in his
current directory.

imac:barmar $ echo [c]rond
[c]rond
imac:barmar $ touch crond
imac:barmar $ echo [c]rond
crond

When a glob doesn't match anything, it's left alone.

And even if the brackets were stripped, it wouldn't be the cause of this
symptom. The purpose of the "grep [c]rond" idiom is to avoid matching
the grep process, but that's not the problem he's having.

>
> #!/bin/bash
> service='crond'
> host=$(hostname)
> content=$(ps auxw | grep '[c]rond' | awk '{print $11}')
> echo "CONTENT: $content"
>
> Also use $() instead of the archaic backticks.
>
> Use pgrep if you have it.
>
> Use lowercase or mixed case variable names. Only use all-caps variable
> names for system-defined environment and shell variables.

kc

unread,
Dec 21, 2011, 11:01:57 AM12/21/11
to
On Dec 21, 10:29 am, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article
> <dfece208-6b8e-4399-b350-fc85d21a8...@e2g2000vbb.googlegroups.com>,
Thanks All, Awesome! The combination of using awk and mroe qoutes did
the trick.

Barry Margolin

unread,
Dec 21, 2011, 11:37:07 AM12/21/11
to
In article
<07c59657-6ee9-404c...@u6g2000vbg.googlegroups.com>,
kc <keec...@gmail.com> wrote:

> Thanks All, Awesome! The combination of using awk and mroe qoutes did
> the trick.

Could you show what you did? As I said, quoting wasn't your problem.
And you were already using awk.
0 new messages