我如何使用NSTask运行一个.sh文件并得到它的输出? [英] How would I run an .sh file using NSTask and get its output?
问题描述
我需要运行一个.sh文件并获取它的输出。我还需要查看文件的设置。
I need to run an .sh file and get its output. I need to see the setup of the file as well.
.sh文件只需通过终端运行一个java应用程序。
The .sh file simply runs a java app through terminal.
任何想法?我真的坚持这个.....
Any ideas? I'm truly stuck on this.....
Elijah
server.sh文件: / p>
The server.sh file:
echo Starting Jarvis Program D.
ALICE_HOME=.
SERVLET_LIB=lib/servlet.jar
ALICE_LIB=lib/aliceserver.jar
JS_LIB=lib/js.jar
# Set SQL_LIB to the location of your database driver.
SQL_LIB=lib/mysql_comp.jar
# These are for Jetty; you will want to change these if you are using a different http server.
HTTP_SERVER_LIBS=lib/org.mortbay.jetty.jar
PROGRAMD_CLASSPATH=$SERVLET_LIB:$ALICE_LIB:$JS_LIB:$SQL_LIB:$HTTP_SERVER_LIBS
java -classpath $PROGRAMD_CLASSPATH -Xms64m -Xmx128m org.alicebot.server.net.AliceServer $1
我当前的代码:
NSTask *server = [NSTask new];
[server setLaunchPath:@"/bin/sh"];
[server setArguments:[NSArray arrayWithObject:@"/applications/jarvis/brain/server.sh"]];
NSPipe *outputPipe = [NSPipe pipe];
[server setStandardInput:[NSPipe pipe]];
[server setStandardOutput:outputPipe];
[server launch];
NSMutableString *outputString = [NSMutableString string];
while ([outputString rangeOfString:@"Jarvis>"].location == NSNotFound) {
[outputString appendString:[[[NSString alloc] initWithData:[[outputPipe fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding] autorelease]];
NSRunAlertPanel(@"", outputString, @"", @"", @"");
}
NSRunAlertPanel仅用于检查输出。现在我的代码是冻结,甚至没有到达警报面板。
The NSRunAlertPanel is just for checking the output. Now my code is freezing and not even getting to the alertpanel.
推荐答案
查看此问题。
有几个事情在您的脚本中固定:
There are a couple of things that should be fixed in your script:
- 脚本应以
shebang开头。还要确保
脚本的可执行位设置。 - 因为环境变量是相对于shell脚本目录设置的,所以需要确保脚本目录是当前目录。
- 您需要导出应该对Java进程可见的环境变量。
-
exec
可用运行Jetty的Java可执行文件替换shell进程。
- The script should begin with a shebang. Also make sure that the script has its executable bit set.
- Because the environment variables are set up relative to the shell script directory, you need to make sure that the script directory is the current directory.
- You need to export the environment variables that should be visible to the Java process.
- In the last line you can use
exec
to replace the shell process with the Java executable that runs Jetty.
以下是脚本的修订版本:
Here is a revised version of your script:
#!/bin/sh
echo Starting Jarvis Program D.
cd "`dirname \"$0\"`"
export ALICE_HOME=.
export SERVLET_LIB=lib/servlet.jar
export ALICE_LIB=lib/aliceserver.jar
export JS_LIB=lib/js.jar
# Set SQL_LIB to the location of your database driver.
export SQL_LIB=lib/mysql_comp.jar
# These are for Jetty; you will want to change these if you are using a different http server.
export HTTP_SERVER_LIBS=lib/org.mortbay.jetty.jar
export PROGRAMD_CLASSPATH=$SERVLET_LIB:$ALICE_LIB:$JS_LIB:$SQL_LIB:$HTTP_SERVER_LIBS
exec java -classpath $PROGRAMD_CLASSPATH -Xms64m -Xmx128m org.alicebot.server.net.AliceServer $1
在目标中调用shell脚本-C具有多个参数:
Invoking the shell script in Objective-C with multiple arguments:
NSTask *server = [NSTask new];
[server setLaunchPath:@"/bin/sh"];
[server setArguments:[NSArray arrayWithObjects:@"/applications/jarvis/brain/server.sh", @"argument", nil]];
...
这篇关于我如何使用NSTask运行一个.sh文件并得到它的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!