How to use NSTask run terminal commands in loop in consistent environment?(如何在一致的环境中循环使用 NSTask 运行终端命令?)
问题描述
我想使用 NSTask 来模拟终端运行命令.代码如下.它可以循环获取输入并返回过程输出.
I want to use NSTask to simulate the Terminal to run commands. The codes as follows. It can get input in loop and return the process output.
我的问题是:当一个循环结束时,运行环境恢复到初始化状态.比如默认运行路径是/Users/apple
,我运行cd/
把路径改成/
,然后运行pwd
,它返回 /Users/apple
而不是 /
.
My question is: when a loop is end, the running environment restore to the initialization state. For example, the default running path is /Users/apple
, and I run cd /
to change the path to /
, and then run pwd
, it return the /Users/apple
rather than the /
.
那么如何使用 NSTask
来完全模拟终端呢?
So how can I use NSTask
to simulate the Terminal completely ?
推荐答案
cd
和 pwd
是 shell 内置命令.如果你执行任务
cd
and pwd
are shell built-in commands. If you execute the task
无法将更改后的工作目录返回给调用进程.如果要设置变量MYVAR=myvalue
,也会存在同样的问题.
there is no way of getting the changed working directory back to the calling process. The same problem exists if you want to set variables MYVAR=myvalue
.
您可以尝试分别解析这些行并更新环境.但是像
You could try to parse these lines separately and update the environment. But what about multi-line commands like
您无法通过将每一行发送到单独的 NSTask
进程来模拟这一点.
You cannot emulate that by sending each line to separate NSTask
processes.
你唯一能做的就是用 NSTask
启动一个single /bin/sh
进程,并将所有输入行提供给该过程的标准输入.但是你不能使用 readDataToEndOfFile
来读取输出,而必须异步读取(使用 [[pipe fileHandleForReading] waitForDataInBackgroundAndNotify]
).
The only thing you could do is to start a single /bin/sh
process with NSTask
, and feed all the input lines to the standard input of that process. But then you can not use readDataToEndOfFile
to read the output, but you have to read asynchronously (using [[pipe fileHandleForReading] waitForDataInBackgroundAndNotify]
).
简而言之:您只能通过运行(单个)shell 来模拟终端.
So in short: you can simulate the Terminal only by running a (single) shell.
添加:也许您可以使用以下内容作为您的应用程序的起点.(我省略了所有的错误检查.)
ADDED: Perhaps you can use the following as a starting point for your app. (I have omitted all error checking.)
这篇关于如何在一致的环境中循环使用 NSTask 运行终端命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!