From: Moody on
whats the easiest way to invoke a subshell in /usr/bin/sh

I have requirement when I need to execute a command with multiple
piped output and then use its returned value.

tried:

counter=`ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
CU="$p_counter" ' BEGIN { x=0;} /CU/ { x=x++; } END { print x } ' `

above works very well within same shell but it has a problem that my
current number of processes including an unknown number of current
processes is also included.

counter=$( ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
CU="$p_counter" ' BEGIN { x=0;} /CU/ { x=x++; } END { print x }
' )

this doesn't work..

I need to execute this piped command in a separate shell using sh

Regards,
BB
From: Moody on
On Jan 8, 6:22 pm, Moody <nasir.mahm...(a)gmail.com> wrote:
> whats the easiest way to invoke a subshell in /usr/bin/sh
>
> I have requirement when I need to execute a command with  multiple
> piped output and then use its returned value.
>
> tried:
>
> counter=`ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
> CU="$p_counter"  ' BEGIN { x=0;}  /CU/ { x=x++; }  END { print x } ' `
>
> above works very well within same shell but it has a problem that my
> current number of processes including an unknown number of current
> processes is also included.
>
> counter=$( ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
> CU="$p_counter"  ' BEGIN { x=0;}  /CU/ { x=x++; }  END { print x }
> '  )
>
> this doesn't work..
>
> I need to execute this piped command in a separate shell using sh
>
> Regards,
> BB


`man sh `


(list)
Execute list in a sub-shell.



I've already tried like below

$ (echo $$)
17317
$ echo $$
17317
$


both shells have same pid, which I understand as same shell and not a
subshell..


From: OldSchool on
On Jan 8, 8:22 am, Moody <nasir.mahm...(a)gmail.com> wrote:
> whats the easiest way to invoke a subshell in /usr/bin/sh
>
> I have requirement when I need to execute a command with  multiple
> piped output and then use its returned value.
>
> tried:
>
> counter=`ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
> CU="$p_counter"  ' BEGIN { x=0;}  /CU/ { x=x++; }  END { print x } ' `
>
> above works very well within same shell but it has a problem that my
> current number of processes including an unknown number of current
> processes is also included.
>
> counter=$( ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
> CU="$p_counter"  ' BEGIN { x=0;}  /CU/ { x=x++; }  END { print x }
> '  )
>
> this doesn't work..
>
> I need to execute this piped command in a separate shell using sh
>
> Regards,
> BB

try

counter=$( ps -ef | grep -v grep | grep "the_process" | wc -l )

note: some "ps" commands will also find named processes when given the
correct switches. for example, HPUX wil do:

UNIX95= ps -C <command> -o pid=

which will list the PID of anything running with the basename of
<command> and will omit the header... the "wc -l" will give you the
total number of lines found, which is what it appears you want
From: Jon LaBadie on
OldSchool wrote:
> On Jan 8, 8:22 am, Moody <nasir.mahm...(a)gmail.com> wrote:
>> whats the easiest way to invoke a subshell in /usr/bin/sh
>>
>> I have requirement when I need to execute a command with multiple
>> piped output and then use its returned value.
>>
>> tried:
>>
>> counter=`ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
>> CU="$p_counter" ' BEGIN { x=0;} /CU/ { x=x++; } END { print x } ' `
>>
>> above works very well within same shell but it has a problem that my
>> current number of processes including an unknown number of current
>> processes is also included.
>>
>> counter=$( ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
>> CU="$p_counter" ' BEGIN { x=0;} /CU/ { x=x++; } END { print x }
>> ' )
>>
>> this doesn't work..
>>
>> I need to execute this piped command in a separate shell using sh
>>
>> Regards,
>> BB
>
> try
>
> counter=$( ps -ef | grep -v grep | grep "the_process" | wc -l )
>
> note: some "ps" commands will also find named processes when given the
> correct switches. for example, HPUX wil do:
>
> UNIX95= ps -C <command> -o pid=
>
> which will list the PID of anything running with the basename of
> <command> and will omit the header... the "wc -l" will give you the
> total number of lines found, which is what it appears you want

Some coders eliminate the need for an extra grep by putting one letter
of "the_process" as a character class. Ex. if sendmail were "the_process"
you might do:

counter=$(ps -ef | grep "[s]endmail" | wc -l)

As grep can do the counting also, this can be simplified to:

counter=$(ps -ef | grep -c "[s]endmail")

Do you really need the "f" option of ps -ef?

If your system has pgrep, you could also use:

counter=$(pgrep "the_process" | wc -l)
From: Janis Papanagnou on
Moody wrote:
> whats the easiest way to invoke a subshell in /usr/bin/sh

sh your_shell_program_file

sh -c 'your_shell_commands'

( your_shell_commands_or_file )

printed_result=$( your_shell_commands_or_file )

>
> I have requirement when I need to execute a command with multiple
> piped output and then use its returned value.

You've already been given some answers elsethread, but you may want
to reduce some of the processes and fix your awk command as well...

>
> tried:
>
> counter=`ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
> CU="$p_counter" ' BEGIN { x=0;} /CU/ { x=x++; } END { print x } ' `

The x=0 is unnecessary and /CU/ matches just a literal "CU" string,
but since you defined a variable CU=... I suppose you wand to test
against the contents in the variable, which would result in...

counter=$( ps -ef | /usr/xpg4/bin/awk -v CU="$p_counter" '
/the_process/ && !/grep/ && $0 ~ CU { x++ } END { print x }' )


Janis

>
> above works very well within same shell but it has a problem that my
> current number of processes including an unknown number of current
> processes is also included.
>
> counter=$( ps -ef|grep -v grep|grep "the_process"|/usr/xpg4/bin/awk -v
> CU="$p_counter" ' BEGIN { x=0;} /CU/ { x=x++; } END { print x }
> ' )
>
> this doesn't work..
>
> I need to execute this piped command in a separate shell using sh
>
> Regards,
> BB