Like in other programming language, is there a way of running linux shell command in Pharo smalltalk or a simple script ? I would like to have my Pharo image running a script that should be able to automate a tasks and return it to some value. I looked at almost all the documentation around and I couldn't find anything related. Maybe It does not allow such functionality.
相关问题
- How to get the return code of a shell script in lu
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Invoking Mirth Connect CLI with Powershell script
- Error building gcc 4.8.3 from source: libstdc++.so
Pharo does allow the OS interaction. The best way, in my eyes, is to use
OSProcess
(as MartinW already suggested).Those that think it is a duplicate are missing this part:
There is nothing about return value in the invoking shell commands from squeak or pharo
To get a return value you would do it the following way:
If you print out the above code you will get most probably a
0
as success.If you do an obvious error:
You will get
~= 0
value in my case512
.Edit adding more details to cover more ground
I agree with eMBee that a statement
is rather vague. I'm adding information about I/Os.
As you may know there are three basic IO:
stdin
,stdout
, andstderr
. These you need to interact with shell. I'll add these examples first then I'll get back to your description.Each of them is represented by instance of
AttachableFileStream
in Pharo. For the abovecommand
you will getinitialStdIn
(stdin
),initialStdOut
(stdout
),initialStdError
(stderr
).To write into the terminal from Pharo:
stdout and stderr (you stream string into terminal)
Check your shell you should see the output there.
stdin - to get what you typed
If you want to grab an output from the command into Pharo a resonable way is to use
PipeableOSProcess
which, as apparent from his name, can be used in conjunction with pipes.Simple example:
More complex example:
I like the use of
outputAndError
because of typos. If you have an incorrect command you will get the error message:In this case
'/bin/sh: cot: command not found'
That is about it.