simple shell script question

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

simple shell script question

bruce-83
trying to get the following to output
  pgrep -f 'bash' | wc -l

  the var $xx has the cmd...
  something's missing to allow the backtics to run the cmd..
  if the cmd is hadcoded.run, it works..

can't see what the homer simpson "doh" moment is!

thanks



#!/bin/bash
#
# ptest.sh
#
# use predefined variables to access passed arguments
#
# $1
aa=$1
aa='bash'
echo $aa " pp"
xx="pgrep -f '"${aa}"' | wc -l"
echo $xx   <<< this dsplays the test pgrep
echo `$xx`   <<  err msg
echo `${xx}`  << err msg...

## trying to get -- pgrep -f 'bash' | wc -l <<
## i'm getting a Usage: pgrep [....] err msg..
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple shell script question

Gordon Messmer-2
On 08/09/2017 05:47 PM, bruce wrote:
> aa='bash'
> xx="pgrep -f '"${aa}"' | wc -l"
> echo $xx   <<< this dsplays the test pgrep
> echo `$xx`   <<  err msg
> echo `${xx}`  << err msg...

eval "$xx"

or:

echo $(eval "$xx")

In the invocation you're trying to use, "|" is being passed as an
argument to pgrep, rather than creating a pipe between commands/processes.
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple shell script question

Kevin Martin-2
In reply to this post by bruce-83
unclear what you are trying to accomplish here.  If, for example, you are trying to pgrep for all processes that have 'bash' in the process name:
#!/bin/bash
aa='bash'
xx="pgrep -f ${aa} | wc -l"
echo $xx <<< shows the command in $xx
eval ${xx} <<< runs the command in $xx


---


Regards,

Kevin Martin

On Wed, Aug 9, 2017 at 7:47 PM, bruce <[hidden email]> wrote:
trying to get the following to output
  pgrep -f 'bash' | wc -l

  the var $xx has the cmd...
  something's missing to allow the backtics to run the cmd..
  if the cmd is hadcoded.run, it works..

can't see what the homer simpson "doh" moment is!

thanks



#!/bin/bash
#
# ptest.sh
#
# use predefined variables to access passed arguments
#
# $1
aa=$1
aa='bash'
echo $aa " pp"
xx="pgrep -f '"${aa}"' | wc -l"
echo $xx   <<< this dsplays the test pgrep
echo `$xx`   <<  err msg
echo `${xx}`  << err msg...

## trying to get -- pgrep -f 'bash' | wc -l <<
## i'm getting a Usage: pgrep [....] err msg..
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]


_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple shell script question

Rick Stevens-4
In reply to this post by bruce-83
On 08/09/2017 05:47 PM, bruce wrote:
> trying to get the following to output
>   pgrep -f 'bash' | wc -l
>
>   the var $xx has the cmd...
>   something's missing to allow the backtics to run the cmd..
>   if the cmd is hadcoded.run, it works..
>
> can't see what the homer simpson "doh" moment is!

It's kind of silly to pass that through "wc". Just use the "-c"
option to pgrep:

[root@prophead ~]# ps ax | grep bash | grep -v grep | wc -l
28
[root@prophead ~]# pgrep -f bash | wc -l
28
[root@prophead ~]# pgrep -c -f bash
28

I think PHP actually tacks on a blank line when executing the stuff in a
backtick, so the "wc" value will always be 1 greater than the normal
return. That's just a guess from doing a var_dump() on the output of the
command:

        [root@prophead ~]# php -a
        Interactive shell

        php > var_dump(`pgrep -f bash | wc -l`);
        string(3) "29
        "
        php > exit
        [root@prophead ~]#

> #!/bin/bash
> #
> # ptest.sh
> #
> # use predefined variables to access passed arguments
> #
> # $1
> aa=$1
> aa='bash'
> echo $aa " pp"
> xx="pgrep -f '"${aa}"' | wc -l"
> echo $xx   <<< this dsplays the test pgrep
> echo `$xx`   <<  err msg
> echo `${xx}`  << err msg...
>
> ## trying to get -- pgrep -f 'bash' | wc -l <<
> ## i'm getting a Usage: pgrep [....] err msg..
> _______________________________________________
> users mailing list -- [hidden email]
> To unsubscribe send an email to [hidden email]
>


--
----------------------------------------------------------------------
- Rick Stevens, Systems Engineer, AllDigital    [hidden email] -
- AIM/Skype: therps2        ICQ: 226437340           Yahoo: origrps2 -
-                                                                    -
-   To understand recursion, you must first understand recursion.    -
----------------------------------------------------------------------
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple shell script question

bruce-83
In reply to this post by bruce-83
hmmmm   update..

this works...
aa='bash'
echo $aa " pp"
xx= pgrep -f $aa | wc -l
echo $xx

however, I can't seem to figure out how to enclose the aa var in
single quotes within the pgrep.

thoughts/comments..

thanks




On Wed, Aug 9, 2017 at 8:47 PM, bruce <[hidden email]> wrote:

> trying to get the following to output
>   pgrep -f 'bash' | wc -l
>
>   the var $xx has the cmd...
>   something's missing to allow the backtics to run the cmd..
>   if the cmd is hadcoded.run, it works..
>
> can't see what the homer simpson "doh" moment is!
>
> thanks
>
>
>
> #!/bin/bash
> #
> # ptest.sh
> #
> # use predefined variables to access passed arguments
> #
> # $1
> aa=$1
> aa='bash'
> echo $aa " pp"
> xx="pgrep -f '"${aa}"' | wc -l"
> echo $xx   <<< this dsplays the test pgrep
> echo `$xx`   <<  err msg
> echo `${xx}`  << err msg...
>
> ## trying to get -- pgrep -f 'bash' | wc -l <<
> ## i'm getting a Usage: pgrep [....] err msg..
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple shell script question

Kevin Martin-2
is the end result meant to be able to pass some string ($1) to pgrep that get's counted?  knowing what you're trying to accomplish would make it easier to help.

---


Regards,

Kevin Martin

On Wed, Aug 9, 2017 at 8:24 PM, bruce <[hidden email]> wrote:
hmmmm   update..

this works...
aa='bash'
echo $aa " pp"
xx= pgrep -f $aa | wc -l
echo $xx

however, I can't seem to figure out how to enclose the aa var in
single quotes within the pgrep.

thoughts/comments..

thanks




On Wed, Aug 9, 2017 at 8:47 PM, bruce <[hidden email]> wrote:
> trying to get the following to output
>   pgrep -f 'bash' | wc -l
>
>   the var $xx has the cmd...
>   something's missing to allow the backtics to run the cmd..
>   if the cmd is hadcoded.run, it works..
>
> can't see what the homer simpson "doh" moment is!
>
> thanks
>
>
>
> #!/bin/bash
> #
> # ptest.sh
> #
> # use predefined variables to access passed arguments
> #
> # $1
> aa=$1
> aa='bash'
> echo $aa " pp"
> xx="pgrep -f '"${aa}"' | wc -l"
> echo $xx   <<< this dsplays the test pgrep
> echo `$xx`   <<  err msg
> echo `${xx}`  << err msg...
>
> ## trying to get -- pgrep -f 'bash' | wc -l <<
> ## i'm getting a Usage: pgrep [....] err msg..
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]


_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple shell script question

bruce-83
On Wed, Aug 9, 2017 at 9:52 PM, kevin martin <[hidden email]> wrote:
> is the end result meant to be able to pass some string ($1) to pgrep that
> get's counted?  knowing what you're trying to accomplish would make it
> easier to help.
>
> ---
>

-------------

hey kevin. sorry.

the test was to (1) replicate the results I get from the cmdline.
and to then (2) be able to pass a input/var to then generate the pgrep
count results

the initial cmdline pgrep  -- pgrep -f 'bash' | wc -l
 -- on the test box, this returns a "50" count

in testing using php/bash -- i get a "51"

#!/bin/bash -f
#
# ptest.sh
#

pgrep -f 'bash' | wc -l

------------------------------------

on the test centos box, there is no pgrep -c << option.. haven't
checked the fedora yet


however, if i replace "bash" with a procname that doesn't exist.. it
correctly returns "0"..

So, I was/am trying to wrap my head around what's going on.

thanks


>
> Regards,
>
> Kevin Martin
>
> On Wed, Aug 9, 2017 at 8:24 PM, bruce <[hidden email]> wrote:
>>
>> hmmmm   update..
>>
>> this works...
>> aa='bash'
>> echo $aa " pp"
>> xx= pgrep -f $aa | wc -l
>> echo $xx
>>
>> however, I can't seem to figure out how to enclose the aa var in
>> single quotes within the pgrep.
>>
>> thoughts/comments..
>>
>> thanks
>>
>>
>>
>>
>> On Wed, Aug 9, 2017 at 8:47 PM, bruce <[hidden email]> wrote:
>> > trying to get the following to output
>> >   pgrep -f 'bash' | wc -l
>> >
>> >   the var $xx has the cmd...
>> >   something's missing to allow the backtics to run the cmd..
>> >   if the cmd is hadcoded.run, it works..
>> >
>> > can't see what the homer simpson "doh" moment is!
>> >
>> > thanks
>> >
>> >
>> >
>> > #!/bin/bash
>> > #
>> > # ptest.sh
>> > #
>> > # use predefined variables to access passed arguments
>> > #
>> > # $1
>> > aa=$1
>> > aa='bash'
>> > echo $aa " pp"
>> > xx="pgrep -f '"${aa}"' | wc -l"
>> > echo $xx   <<< this dsplays the test pgrep
>> > echo `$xx`   <<  err msg
>> > echo `${xx}`  << err msg...
>> >
>> > ## trying to get -- pgrep -f 'bash' | wc -l <<
>> > ## i'm getting a Usage: pgrep [....] err msg..
>> _______________________________________________
>> users mailing list -- [hidden email]
>> To unsubscribe send an email to [hidden email]
>
>
>
> _______________________________________________
> users mailing list -- [hidden email]
> To unsubscribe send an email to [hidden email]
>
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple shell script question

bruce-83
hey guys...

appears to be user error... looks like i was on the wrong/older box..
on the updated centos/fed.. looks as i expected... at least for now!

thanks..


On Wed, Aug 9, 2017 at 10:07 PM, bruce <[hidden email]> wrote:

> On Wed, Aug 9, 2017 at 9:52 PM, kevin martin <[hidden email]> wrote:
>> is the end result meant to be able to pass some string ($1) to pgrep that
>> get's counted?  knowing what you're trying to accomplish would make it
>> easier to help.
>>
>> ---
>>
>
> -------------
>
> hey kevin. sorry.
>
> the test was to (1) replicate the results I get from the cmdline.
> and to then (2) be able to pass a input/var to then generate the pgrep
> count results
>
> the initial cmdline pgrep  -- pgrep -f 'bash' | wc -l
>  -- on the test box, this returns a "50" count
>
> in testing using php/bash -- i get a "51"
>
> #!/bin/bash -f
> #
> # ptest.sh
> #
>
> pgrep -f 'bash' | wc -l
>
> ------------------------------------
>
> on the test centos box, there is no pgrep -c << option.. haven't
> checked the fedora yet
>
>
> however, if i replace "bash" with a procname that doesn't exist.. it
> correctly returns "0"..
>
> So, I was/am trying to wrap my head around what's going on.
>
> thanks
>
>
>>
>> Regards,
>>
>> Kevin Martin
>>
>> On Wed, Aug 9, 2017 at 8:24 PM, bruce <[hidden email]> wrote:
>>>
>>> hmmmm   update..
>>>
>>> this works...
>>> aa='bash'
>>> echo $aa " pp"
>>> xx= pgrep -f $aa | wc -l
>>> echo $xx
>>>
>>> however, I can't seem to figure out how to enclose the aa var in
>>> single quotes within the pgrep.
>>>
>>> thoughts/comments..
>>>
>>> thanks
>>>
>>>
>>>
>>>
>>> On Wed, Aug 9, 2017 at 8:47 PM, bruce <[hidden email]> wrote:
>>> > trying to get the following to output
>>> >   pgrep -f 'bash' | wc -l
>>> >
>>> >   the var $xx has the cmd...
>>> >   something's missing to allow the backtics to run the cmd..
>>> >   if the cmd is hadcoded.run, it works..
>>> >
>>> > can't see what the homer simpson "doh" moment is!
>>> >
>>> > thanks
>>> >
>>> >
>>> >
>>> > #!/bin/bash
>>> > #
>>> > # ptest.sh
>>> > #
>>> > # use predefined variables to access passed arguments
>>> > #
>>> > # $1
>>> > aa=$1
>>> > aa='bash'
>>> > echo $aa " pp"
>>> > xx="pgrep -f '"${aa}"' | wc -l"
>>> > echo $xx   <<< this dsplays the test pgrep
>>> > echo `$xx`   <<  err msg
>>> > echo `${xx}`  << err msg...
>>> >
>>> > ## trying to get -- pgrep -f 'bash' | wc -l <<
>>> > ## i'm getting a Usage: pgrep [....] err msg..
>>> _______________________________________________
>>> users mailing list -- [hidden email]
>>> To unsubscribe send an email to [hidden email]
>>
>>
>>
>> _______________________________________________
>> users mailing list -- [hidden email]
>> To unsubscribe send an email to [hidden email]
>>
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simple shell script question

Ed Greshko-2
In reply to this post by bruce-83
On 08/10/2017 10:07 AM, bruce wrote:

> the test was to (1) replicate the results I get from the cmdline.
> and to then (2) be able to pass a input/var to then generate the pgrep
> count results
>
> the initial cmdline pgrep  -- pgrep -f 'bash' | wc -l
>  -- on the test box, this returns a "50" count
>
> in testing using php/bash -- i get a "51"
>
> #!/bin/bash -f
> #
> # ptest.sh
> #
>
> pgrep -f 'bash' | wc -l
>
> ------------------------------------
>
> on the test centos box, there is no pgrep -c << option.. haven't
> checked the fedora yet

You do realize that when you execute the shell script ptest.sh you create one more
process with "bash" in it?

So....

[egreshko@meimei ~]$ pgrep -f 'bash' | wc -l
4
[egreshko@meimei ~]$ ./pt.sh
5

is totally correct.


--
Fedora Users List - The place to go to speculate endlessly


_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]

signature.asc (849 bytes) Download Attachment
Loading...