pharo-users@lists.pharo.org

Any question about pharo is welcome

View all threads

OSSUnixSubprocess - not getting output from a command that pipes to grep

SM
Stewart MacLean
Mon, Jun 6, 2022 11:04 AM

Hi,

I have a command that works fine in terminal, and I'm able to retrieve the
output using OSUnixSubprocess.

However, when I grep the output in the command, I don't receive any output
from OSUnixSubprocess.

So I get output from:
"/Applications/Logic Pro X.app/Contents/MacOS/Logic Pro X" 2>&1

but not from:

"/Applications/Logic Pro X.app/Contents/MacOS/Logic Pro X" 2>&1 | grep
'==>'

However both work fine when the enclosing .command script is run directly
in the terminal.

I'm a bit out of my depth here, but I would have thought OSUnixSubprocess
would just retrieve the output from grep?

Is this a problem with my .command or is this a problem with how I'm using
OSUnixSubprocess. Code below:

Cheers,

Stewart

startUpLogic

Transcript cr; show: 'Starting up Logic...'.
logicOSProcess := OSSUnixSubprocess new.
logicReadProcess :=
[logicOSProcess
command: '/Users/stewart/Desktop/LogicFiltered.command';
redirectStdout; redirectStderr;
runAndWaitPollingEvery: (Delay forMilliseconds: 50)
doing: [: process : outStream : errorStream |
errorStream upToEnd ifNotEmpty:
[: value |
Transcript cr; show: value].
outStream upToEnd ifNotEmpty:
[: value |
Transcript cr; show: value]]
onExitDo: [: process : outStream : errStream  |
process closeAndCleanStreams.
self
log: 'Logic exited with: ',
process exitStatusInterpreter printString]] newProcess.

logicReadProcess
name: 'Logic Read Process';
priority: 50;  "Processor userInterruptPriority"
resume

---===============
I get output using OSSUnixSubprocess from this .command

#!/bin/sh

xattr -r -d com.apple.quarantine /Users/stewart/Desktop/Logic.command

cd "/Applications/Logic Pro X.app/Contents/MacOS/"

"/Applications/Logic Pro X.app/Contents/MacOS/Logic Pro X" 2>&1

---=======================
but not this .command

#!/bin/sh

xattr -r -d com.apple.quarantine

/Users/stewart/Desktop/LogicFiltered.command

cd "/Applications/Logic Pro X.app/Contents/MacOS/"

"/Applications/Logic Pro X.app/Contents/MacOS/Logic Pro X" 2>&1 | grep
'==>'

Hi, I have a command that works fine in terminal, and I'm able to retrieve the output using OSUnixSubprocess. However, when I grep the output in the command, I don't receive any output from OSUnixSubprocess. So I get output from: "/Applications/Logic Pro X.app/Contents/MacOS/Logic Pro X" 2>&1 but not from: "/Applications/Logic Pro X.app/Contents/MacOS/Logic Pro X" 2>&1 | grep '==>' However both work fine when the enclosing .command script is run directly in the terminal. I'm a bit out of my depth here, but I would have thought OSUnixSubprocess would just retrieve the output from grep? Is this a problem with my .command or is this a problem with how I'm using OSUnixSubprocess. Code below: Cheers, Stewart >>startUpLogic Transcript cr; show: 'Starting up Logic...'. logicOSProcess := OSSUnixSubprocess new. logicReadProcess := [logicOSProcess command: '/Users/stewart/Desktop/LogicFiltered.command'; redirectStdout; redirectStderr; runAndWaitPollingEvery: (Delay forMilliseconds: 50) doing: [: process : outStream : errorStream | errorStream upToEnd ifNotEmpty: [: value | Transcript cr; show: value]. outStream upToEnd ifNotEmpty: [: value | Transcript cr; show: value]] onExitDo: [: process : outStream : errStream | process closeAndCleanStreams. self log: 'Logic exited with: ', process exitStatusInterpreter printString]] newProcess. logicReadProcess name: 'Logic Read Process'; priority: 50; "Processor userInterruptPriority" resume ================================================ I get output using OSSUnixSubprocess from this .command #!/bin/sh # xattr -r -d com.apple.quarantine /Users/stewart/Desktop/Logic.command cd "/Applications/Logic Pro X.app/Contents/MacOS/" "/Applications/Logic Pro X.app/Contents/MacOS/Logic Pro X" 2>&1 ======================================================== but not this .command #!/bin/sh # xattr -r -d com.apple.quarantine /Users/stewart/Desktop/LogicFiltered.command cd "/Applications/Logic Pro X.app/Contents/MacOS/" "/Applications/Logic Pro X.app/Contents/MacOS/Logic Pro X" 2>&1 | grep '==>'
V
vinref@gmail.com
Mon, Jun 6, 2022 11:51 AM

Hi

I tried something similar on (X)ubuntu using this:

OSSUnixSubprocess new
	command: '/bin/ls';
	arguments: #('-la' '/tmp/' '|' '/usr/bin/grep unix');
	redirectStdout;
	runAndWaitOnExitDo: [ :process :outString  |
		outString inspect
	]

and looking at the output in xtrerm I got a message like this:

/bin/ls: cannot access '|': No such file or directory
/bin/ls: cannot access '/usr/bin/grep unix': No such file or directory.

So I think it regards anything after the first item in the arguments as a file or directory.

This means you will have to write a bash script instead. So I wrote a script called my_grep:

#!/bin/bash
cd $1
ls -la | grep unix

and called it with:

OSSUnixSubprocess new
	command: '/tmp/my_grep';
	arguments: #('/tmp');
	redirectStdout;
	runAndWaitOnExitDo: [ :process :outString  |
		outString inspect
	]

and it worked.

Vince

Hi I tried something similar on (X)ubuntu using this: ``` OSSUnixSubprocess new ``` ``` command: '/bin/ls'; ``` ``` arguments: #('-la' '/tmp/' '|' '/usr/bin/grep unix'); ``` ``` redirectStdout; ``` ``` runAndWaitOnExitDo: [ :process :outString | ``` ``` outString inspect ``` ``` ] ``` and looking at the output in xtrerm I got a message like this: ``` /bin/ls: cannot access '|': No such file or directory ``` ``` /bin/ls: cannot access '/usr/bin/grep unix': No such file or directory. ``` So I think it regards anything after the first item in the arguments as a file or directory. This means you will have to write a bash script instead. So I wrote a script called my_grep: ``` #!/bin/bash ``` ``` cd $1 ``` ``` ls -la | grep unix ``` and called it with: ``` OSSUnixSubprocess new ``` ``` command: '/tmp/my_grep'; ``` ``` arguments: #('/tmp'); ``` ``` redirectStdout; ``` ``` runAndWaitOnExitDo: [ :process :outString | ``` ``` outString inspect ``` ``` ] ``` and it worked. Vince
YC
Yanni Chiu
Mon, Jun 6, 2022 3:12 PM

The equivalent of running in a Terminal is to run it in a shell. Try the
following code:

OSSUnixSubprocess new
command: '/bin/bash';
arguments: #('-c' 'ls /');
redirectStdout;
runAndWaitOnExitDo: [ :process :outString  |
outString inspect
].

OSSUnixSubprocess new
command: '/bin/bash';
arguments: #('-c' 'ls / | grep bin');
redirectStdout;
runAndWaitOnExitDo: [ :process :outString  |
outString inspect
].

You’ll need to be careful to properly shell escape the shell command
arguments if they are not hard coded (especially things like file names
from users). HTH. —Yanni

On Mon, Jun 6, 2022 at 7:51 AM vinref@gmail.com wrote:

Hi

I tried something similar on (X)ubuntu using this:

OSSUnixSubprocess new

command: '/bin/ls';

arguments: #('-la' '/tmp/' '|' '/usr/bin/grep unix');

redirectStdout;

runAndWaitOnExitDo: [ :process :outString  |

	outString inspect

]

and looking at the output in xtrerm I got a message like this:

/bin/ls: cannot access '|': No such file or directory

/bin/ls: cannot access '/usr/bin/grep unix': No such file or directory.

So I think it regards anything after the first item in the arguments as a
file or directory.

This means you will have to write a bash script instead. So I wrote a
script called my_grep:

#!/bin/bash

cd $1

ls -la | grep unix

and called it with:

OSSUnixSubprocess new

command: '/tmp/my_grep';

arguments: #('/tmp');

redirectStdout;

runAndWaitOnExitDo: [ :process :outString  |

	outString inspect

]

and it worked.

Vince

The equivalent of running in a Terminal is to run it in a shell. Try the following code: OSSUnixSubprocess new command: '/bin/bash'; arguments: #('-c' 'ls /'); redirectStdout; runAndWaitOnExitDo: [ :process :outString | outString inspect ]. OSSUnixSubprocess new command: '/bin/bash'; arguments: #('-c' 'ls / | grep bin'); redirectStdout; runAndWaitOnExitDo: [ :process :outString | outString inspect ]. You’ll need to be careful to properly shell escape the shell command arguments if they are not hard coded (especially things like file names from users). HTH. —Yanni On Mon, Jun 6, 2022 at 7:51 AM <vinref@gmail.com> wrote: > Hi > > I tried something similar on (X)ubuntu using this: > > OSSUnixSubprocess new > > command: '/bin/ls'; > > arguments: #('-la' '/tmp/' '|' '/usr/bin/grep unix'); > > redirectStdout; > > runAndWaitOnExitDo: [ :process :outString | > > outString inspect > > ] > > > and looking at the output in xtrerm I got a message like this: > > /bin/ls: cannot access '|': No such file or directory > > /bin/ls: cannot access '/usr/bin/grep unix': No such file or directory. > > > So I think it regards anything after the first item in the arguments as a > file or directory. > > This means you will have to write a bash script instead. So I wrote a > script called my_grep: > > #!/bin/bash > > cd $1 > > ls -la | grep unix > > and called it with: > > OSSUnixSubprocess new > > command: '/tmp/my_grep'; > > arguments: #('/tmp'); > > redirectStdout; > > runAndWaitOnExitDo: [ :process :outString | > > outString inspect > > ] > > and it worked. > > > Vince > > >
SM
Stewart MacLean
Mon, Jun 6, 2022 10:39 PM

Hi Yanni,

Thanks for this. Actually I discovered the equivalent by using
shellCommand: instead of command:.

What an excellent utility!

Cheers,

Stewart

On Tue, Jun 7, 2022 at 3:12 AM Yanni Chiu yannix7db@gmail.com wrote:

The equivalent of running in a Terminal is to run it in a shell. Try the
following code:

OSSUnixSubprocess new
command: '/bin/bash';
arguments: #('-c' 'ls /');
redirectStdout;
runAndWaitOnExitDo: [ :process :outString  |
outString inspect
].

OSSUnixSubprocess new
command: '/bin/bash';
arguments: #('-c' 'ls / | grep bin');
redirectStdout;
runAndWaitOnExitDo: [ :process :outString  |
outString inspect
].

You’ll need to be careful to properly shell escape the shell command
arguments if they are not hard coded (especially things like file names
from users). HTH. —Yanni

On Mon, Jun 6, 2022 at 7:51 AM vinref@gmail.com wrote:

Hi

I tried something similar on (X)ubuntu using this:

OSSUnixSubprocess new

command: '/bin/ls';

arguments: #('-la' '/tmp/' '|' '/usr/bin/grep unix');

redirectStdout;

runAndWaitOnExitDo: [ :process :outString  |

	outString inspect

]

and looking at the output in xtrerm I got a message like this:

/bin/ls: cannot access '|': No such file or directory

/bin/ls: cannot access '/usr/bin/grep unix': No such file or directory.

So I think it regards anything after the first item in the arguments as a
file or directory.

This means you will have to write a bash script instead. So I wrote a
script called my_grep:

#!/bin/bash

cd $1

ls -la | grep unix

and called it with:

OSSUnixSubprocess new

command: '/tmp/my_grep';

arguments: #('/tmp');

redirectStdout;

runAndWaitOnExitDo: [ :process :outString  |

	outString inspect

]

and it worked.

Vince

Hi Yanni, Thanks for this. Actually I discovered the equivalent by using shellCommand: instead of command:. What an excellent utility! Cheers, Stewart On Tue, Jun 7, 2022 at 3:12 AM Yanni Chiu <yannix7db@gmail.com> wrote: > The equivalent of running in a Terminal is to run it in a shell. Try the > following code: > > OSSUnixSubprocess new > command: '/bin/bash'; > arguments: #('-c' 'ls /'); > redirectStdout; > runAndWaitOnExitDo: [ :process :outString | > outString inspect > ]. > > OSSUnixSubprocess new > command: '/bin/bash'; > arguments: #('-c' 'ls / | grep bin'); > redirectStdout; > runAndWaitOnExitDo: [ :process :outString | > outString inspect > ]. > > You’ll need to be careful to properly shell escape the shell command > arguments if they are not hard coded (especially things like file names > from users). HTH. —Yanni > > On Mon, Jun 6, 2022 at 7:51 AM <vinref@gmail.com> wrote: > >> Hi >> >> I tried something similar on (X)ubuntu using this: >> >> OSSUnixSubprocess new >> >> command: '/bin/ls'; >> >> arguments: #('-la' '/tmp/' '|' '/usr/bin/grep unix'); >> >> redirectStdout; >> >> runAndWaitOnExitDo: [ :process :outString | >> >> outString inspect >> >> ] >> >> >> and looking at the output in xtrerm I got a message like this: >> >> /bin/ls: cannot access '|': No such file or directory >> >> /bin/ls: cannot access '/usr/bin/grep unix': No such file or directory. >> >> >> So I think it regards anything after the first item in the arguments as a >> file or directory. >> >> This means you will have to write a bash script instead. So I wrote a >> script called my_grep: >> >> #!/bin/bash >> >> cd $1 >> >> ls -la | grep unix >> >> and called it with: >> >> OSSUnixSubprocess new >> >> command: '/tmp/my_grep'; >> >> arguments: #('/tmp'); >> >> redirectStdout; >> >> runAndWaitOnExitDo: [ :process :outString | >> >> outString inspect >> >> ] >> >> and it worked. >> >> >> Vince >> >> >>