Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
return values of bash scripts
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
kc  
View profile  
 More options Dec 20 2011, 4:58 pm
Newsgroups: gnu.bash
From: kc <keecc...@gmail.com>
Date: Tue, 20 Dec 2011 13:58:46 -0800 (PST)
Local: Tues, Dec 20 2011 4:58 pm
Subject: return values of bash scripts
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Dec 21 2011, 1:41 am
Newsgroups: gnu.bash
From: Barry Margolin <bar...@alum.mit.edu>
Date: Wed, 21 Dec 2011 01:41:15 -0500
Local: Wed, Dec 21 2011 1:41 am
Subject: Re: return values of bash scripts
In article
<262db63a-fb5c-4b4c-9ca6-35a751f4b...@i6g2000vbh.googlegroups.com>,

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 ***


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
DennisW  
View profile  
 More options Dec 21 2011, 3:28 am
Newsgroups: gnu.bash
From: DennisW <dennistwilliam...@gmail.com>
Date: Wed, 21 Dec 2011 00:28:33 -0800 (PST)
Local: Wed, Dec 21 2011 3:28 am
Subject: Re: return values of bash scripts
On Dec 20, 3:58 pm, kc <keecc...@gmail.com> wrote:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Dec 21 2011, 10:29 am
Newsgroups: gnu.bash
From: Barry Margolin <bar...@alum.mit.edu>
Date: Wed, 21 Dec 2011 10:29:38 -0500
Local: Wed, Dec 21 2011 10:29 am
Subject: Re: return values of bash scripts
In article
<dfece208-6b8e-4399-b350-fc85d21a8...@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.

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
kc  
View profile  
 More options Dec 21 2011, 11:01 am
Newsgroups: gnu.bash
From: kc <keecc...@gmail.com>
Date: Wed, 21 Dec 2011 08:01:57 -0800 (PST)
Local: Wed, Dec 21 2011 11:01 am
Subject: Re: return values of bash scripts
On Dec 21, 10:29 am, Barry Margolin <bar...@alum.mit.edu> wrote:

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Dec 21 2011, 11:37 am
Newsgroups: gnu.bash
From: Barry Margolin <bar...@alum.mit.edu>
Date: Wed, 21 Dec 2011 11:37:07 -0500
Local: Wed, Dec 21 2011 11:37 am
Subject: Re: return values of bash scripts
In article
<07c59657-6ee9-404c-83dc-73b4ae899...@u6g2000vbg.googlegroups.com>,

 kc <keecc...@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.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »