本文介绍了是否可以在Pharo smalltalk中编写shell命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像其他编程语言一样,是否可以在Pharo smalltalk或简单脚本中运行linux shell命令?我想让我的Pharo图像运行一个脚本,该脚本应该能够自动执行任务并将其返回到某个值.我查看了几乎所有文档,但找不到任何相关内容.也许它不允许这种功能.

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.

推荐答案

Pharo允许操作系统交互.在我看来,最好的方法是使用OSProcess(如已经建议的 MartinW 一样).

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:

从squeak或pharo调用shell命令的返回值没有任何意义

要获取返回值,您可以通过以下方式进行操作:

To get a return value you would do it the following way:

command := OSProcess waitForCommand: 'ls -la'.
command exitStatus.

如果打印出上面的代码,您很可能会获得0作为成功.

If you print out the above code you will get most probably a 0 as success.

如果您犯了一个明显的错误:

If you do an obvious error:

command := OSProcess waitForCommand: 'ls -la /dir-does-not-exists'.
command exitStatus.

在我的情况下,您将获得~= 0512.

You will get ~= 0 value in my case 512.

我同意eMBee的声明

I agree with eMBee that a statement

相当模糊.我正在添加有关I/O的信息.

is rather vague. I'm adding information about I/Os.

您可能知道有三个基本IO:stdinstdoutstderr.这些您需要与Shell进行交互.首先添加这些示例,然后回到您的描述.

As you may know there are three basic IO: stdin, stdout, and stderr. These you need to interact with shell. I'll add these examples first then I'll get back to your description.

每个元素都由Pharo中的AttachableFileStream实例表示.对于上面的command,您将获得initialStdIn(stdin),initialStdOut(stdout),initialStdError(stderr).

Each of them is represented by instance of AttachableFileStream in Pharo. For the above command you will get initialStdIn (stdin), initialStdOut (stdout), initialStdError (stderr).

要将来自 Pharo的终端写入:

To write into the terminal from Pharo:

  1. stdout stderr (您将字符串流式传输到终端)

  1. stdout and stderr (you stream string into terminal)

| process |

process := OSProcess thisOSProcess.
process stdOut nextPutAll: 'stdout: All your base belong to us'; nextPut: Character lf.
process stdErr nextPutAll: 'stderr: All your base belong to us'; nextPut: Character lf.

检查您的外壳,您应该在那里看到输出.

Check your shell you should see the output there.

  1. stdin -获取您输入的内容

| userInput handle fetchUserInput |

userInput := OSProcess thisOSProcess stdIn.
handle := userInput ioHandle.
"You need this in order to use terminal -> add stdion"
OSProcess accessor setNonBlocking: handle.
fetchUserInput := OS2Process thisOSProcess stdIn next.
"Set blocking back to the handle"
OSProcess accessor setBlocking: handle.
"Gets you one input character"
fetchUserInput inspect.

如果您想从 命令捕获 Pharo中获取输出,一种合理的方法是使用PipeableOSProcess,从他的名字可以明显看出,可以在PipeableOSProcess中使用它.与管道结合.

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.

简单的例子:

| commandOutput |

commandOutput := (PipeableOSProcess command: 'ls -la') output.
commandOutput inspect.

更复杂的示例:

| commandOutput |

commandOutput := ((PipeableOSProcess command: 'ps -ef') | 'grep pharo') outputAndError.
commandOutput inspect.

由于错别字,我喜欢使用outputAndError.如果您输入的命令不正确,则会收到错误消息:

I like the use of outputAndError because of typos. If you have an incorrect command you will get the error message:

| commandOutput |

commandOutput := ((PipeableOSProcess command: 'ps -ef') | 'grep pharo' | 'cot') outputAndError.
commandOutput  inspect.

在这种情况下'/bin/sh: cot: command not found'

就是这样.

这篇关于是否可以在Pharo smalltalk中编写shell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 12:26