FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Redhat > Fedora User

 
 
LinkBack Thread Tools
 
Old 10-17-2011, 07:39 PM
Mike Wright
 
Default OT: need bash help

Hi all,

I'm trying to write a bash script and having problems.

If I execute this:

ps x | grep mongod | wc -l

it returns a value.

OTOH, if I execute this:

LINES = ps x | grep mongod | wc -l

it returns "command not found".

How does one assign the output of a command to an environment variable?

TIA,
Mike Wright
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 10-17-2011, 07:43 PM
Larry Brower
 
Default OT: need bash help

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 10/17/2011 02:39 PM, Mike Wright wrote:
> Hi all,
>
> I'm trying to write a bash script and having problems.
>
> If I execute this:
>
> ps x | grep mongod | wc -l
>
> it returns a value.
>
> OTOH, if I execute this:
>
> LINES = ps x | grep mongod | wc -l
>
> it returns "command not found".
>
> How does one assign the output of a command to an environment variable?
>
> TIA,
> Mike Wright


I think you are looking for:


LINES=$(ps x | grep mongod | wc -l)

-----BEGIN PGP SIGNATURE-----

iQIcBAEBCgAGBQJOnIVZAAoJEF1Xw4ZWTEoJHhoP/ArT7LZCFrjkhGS+ECi47rWK
xEelC7IC5oyZ/++LAp7dOimhLT3dV9B5UJxqffdXl4zxTbx+TrxulZhEjPG1vGz +
j1VfH9kPI3TawjQqO5soH0reWONfNDa0YIrl44CTnW+Drt9rqX l4c9tlNDfGilW2
zkj1zDCbegTdlVWlkiYvicWwFNfDx4qO0oh8hTKsCwqEhOMeL+ swsnaKRhWn6Qke
tIV+zwUXiRTtBOaYvwd0KPJrZb6P8yfHSh2cDWkzpwHSH8t4pP vEgE8Z5rYTgy0M
AbSFOuMdotbgWyfHNMQHQNesHHAIipIr7OyYnVQ/rEM5pe4nKMtOknaSxonOkPcM
hNFwJNL2gOUL+xf77BO5jqu6YKw31+lkliuR6kiY/UYGOfN5vJFOxfz7R8x6Rde7
A5oo0Ta76BN75q0obNZiS+Rj+fn5cuIZyy657cDccLwK9VSF+v lAfhBZ1qb88nAr
ss9FvGwLhJeoxPw+nnFxlPAD0byl78TwbXEoQMDfLzwlPCHFAA qqVARlJcFtEmjG
yjEc4d5Jlps7MckD41FNBTkGQLXkyh78zpq82G7x0BRE/yluOAcZ9AZAjWTaToND
YD9kfT2kM86NxDzKvYHZPxqRpmzk8z+25j6oHNmwcjTY8Fe6kc WflriPXdSOvyxI
xdA/4NJzmb9wwP0OQzIz
=AcJ1
-----END PGP SIGNATURE-----
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 10-17-2011, 07:45 PM
Mike Wright
 
Default OT: need bash help

On 10/17/2011 12:39 PM, Mike Wright wrote:
> Hi all,
>
> I'm trying to write a bash script and having problems.
>
> If I execute this:
>
> ps x | grep mongod | wc -l
>
> it returns a value.
>
> OTOH, if I execute this:
>
> LINES = ps x | grep mongod | wc -l
>
> it returns "command not found".
>
> How does one assign the output of a command to an environment variable?

Aaargh! Doesn't like the spaces around the = sign.

LINES=`ps x | grep mongod | wc -l` is a way to write it.

Sorry for the noise :/
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 10-17-2011, 07:46 PM
Emmett Culley
 
Default OT: need bash help

On 10/17/2011 12:39 PM, Mike Wright wrote:
> Hi all,
>
> I'm trying to write a bash script and having problems.
>
> If I execute this:
>
> ps x | grep mongod | wc -l
>
> it returns a value.
>
> OTOH, if I execute this:
>
> LINES = ps x | grep mongod | wc -l
>
> it returns "command not found".
>
> How does one assign the output of a command to an environment variable?
>
> TIA,
> Mike Wright
You can use:

LINES=`ps x | grep mongod | wc -l`

(using back ticks to bracket the command)

Emmett
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 10-17-2011, 07:54 PM
Pete Travis
 
Default OT: need bash help

Try wrapping your command to make a subshell:


LINES=$(ps x| grep mongod | wc -l)


HTH,


Pete

On Oct 17, 2011 1:39 PM, "Mike Wright" <mike.wright@mailinator.com> wrote:
Hi all,



I'm trying to write a bash script and having problems.



If I execute this:



* ps x | grep mongod | wc -l



it returns a value.



OTOH, if I execute this:



* LINES = ps x | grep mongod | wc -l



it returns "command not found".



How does one assign the output of a command to an environment variable?



TIA,

Mike Wright

--

users mailing list

users@lists.fedoraproject.org

To unsubscribe or change subscription options:

https://admin.fedoraproject.org/mailman/listinfo/users

Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 10-17-2011, 08:06 PM
Pete Travis
 
Default OT: need bash help

Backticks for a subshell are 'depricated' though the convention is still in wide use.* I use the $() method because `su - user 'command --opt="foo"'` and such can get a bit confusing,* and the alternative is much easier to pick out of a mass of text.


On Oct 17, 2011 1:45 PM, "Mike Wright" <mike.wright@mailinator.com> wrote:
On 10/17/2011 12:39 PM, Mike Wright wrote:

> Hi all,

>

> I'm trying to write a bash script and having problems.

>

> If I execute this:

>

> * * ps x | grep mongod | wc -l

>

> it returns a value.

>

> OTOH, if I execute this:

>

> * * LINES = ps x | grep mongod | wc -l

>

> it returns "command not found".

>

> How does one assign the output of a command to an environment variable?



Aaargh! *Doesn't like the spaces around the = sign.



LINES=`ps x | grep mongod | wc -l` is a way to write it.



Sorry for the noise :/

--

users mailing list

users@lists.fedoraproject.org

To unsubscribe or change subscription options:

https://admin.fedoraproject.org/mailman/listinfo/users

Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 10-17-2011, 10:33 PM
Ian Malone
 
Default OT: need bash help

On 17 October 2011 20:45, Mike Wright <mike.wright@mailinator.com> wrote:
> On 10/17/2011 12:39 PM, Mike Wright wrote:
>> Hi all,
>>
>> I'm trying to write a bash script and having problems.
>>
>> If I execute this:
>>
>> * * ps x | grep mongod | wc -l
>>
>> it returns a value.
>>
>> OTOH, if I execute this:
>>
>> * * LINES = ps x | grep mongod | wc -l
>>
>> it returns "command not found".
>>
>> How does one assign the output of a command to an environment variable?
>
> Aaargh! *Doesn't like the spaces around the = sign.
>
> LINES=`ps x | grep mongod | wc -l` is a way to write it.
>

There's quite a good explanation of that somewhere, I think in the
bash manual. It boils down to:
X=A
is an assignment (to X)
X =A
is a command (run X, argument, '=A')
X= A
is an assignment (X="") followed by a command (A) run with that
assignment in effect (normally you need to export the value for it to
affect the child process).

Exercise left for the reader as to what
X = A
is.
--
imalone
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 10-18-2011, 12:10 AM
g
 
Default OT: need bash help

On 10/17/2011 07:39 PM, Mike Wright wrote:
> Hi all,
>
> I'm trying to write a bash script and having problems.

you seem to have several replies that can give you what you want.

here are a couple links that i have found helpful;

http://tldp.org/LDP/abs/html/
http://www.faqs.org/docs/abs/HTML/index.html

hth.
--

peace out.

tc.hago,

g
.

*please reply "plain text". "html text" are deleted*

****
in a free world without fences, who needs gates.
**
help microsoft stamp out piracy - give linux to a friend today.
**
to mess up a linux box, you need to work at it.
to mess up an ms windows box, you just need to *look* at it.
**
The installation instructions stated to install Windows 2000 or better.
So I installed Linux.
**
learn linux:
'Rute User's Tutorial and Exposition' http://rute.2038bug.com/index.html
'The Linux Documentation Project' http://www.tldp.org/
'LDP HOWTO-index' http://www.tldp.org/HOWTO/HOWTO-INDEX/index.html
'HowtoForge' http://howtoforge.com/
****

--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 

Thread Tools




All times are GMT. The time now is 07:14 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org