swiftã§å¤é¨ã³ãã³ãå®è¡
qiita.com ã«ç§»å
é ã«#!/usr/bin/swift
ã£ã¦æ¸ãã¦ããã¨
chmod a+x hoge.swift ./hoge.swift #swift hoge.swiftã§ãèµ·åã§ãã
ã£ã¦æãã§ã¹ã¯ãªããã¿ããã«èµ·åã§ãããã¨ãç¥ã£ãã®ã§ãåå¼·ãã¦ãæå ã«ããperlã¹ã¯ãªãããswift翻訳ãã¦ãã ãã®éã®ã¡ã¢ã
perlã®
my $status = system "ls hoge/" #ret-code my $ret = `ls hoge/ ` #output
ããããã®ãswiftã§ã©ãæ¸ãã°ãããã ã£ãï¼ã¨æã£ã¦èª¿ã¹ãã
OSXã¢ããªã¯ã»ã¨ãã©æ¸ãããã¨ãªãã®ã§ã使ã£ããã¨ãªãã£ããã©NSTaskã§ã§ããã¿ããã
task.standardOutput
(NSPipe)ã§å¤é¨ã³ãã³ãã®åºåãåãåããã¨ãã§ããã
ã«ã¬ã³ããã£ã¬ã¯ããªãæå®ãã¦å®è¡ãããå ´åã¯ãtask.currentDirectoryPath
ã«ã»ãããã¦ããã°ããã
func stdOutOfCommand(cmd: String, arguments args: [String], currentDirPath currentDir: String? = nil) -> String { var task: NSTask = NSTask() task.launchPath = cmd task.arguments = args if currentDir != nil { task.currentDirectoryPath = currentDir! } let pipe: NSPipe = NSPipe() task.standardOutput = pipe task.launch() let out: NSData = pipe.fileHandleForReading.readDataToEndOfFile() let outStr: String? = NSString(data: out, encoding: NSUTF8StringEncoding) as? String return outStr == nil ? "" : outStr! } var ret = stdOutOfCommand("/bin/ls", arguments: ["hoge/"])
対話(å
¥åãè¦æ±ãã)å½¢å¼ã®å ´åã¯ãwaitForDataInBackgroundAndNotify()
ã§ããã¯ã°ã©ã¦ã³ãå¾
æ©ã¨éç¥ããããã«ãã¦ã
NSNotificationCenterã§NSFileHandleDataAvailableNotification
ãåãåãããã«ããå¿
è¦ãããã
taskã®çµäºã¾ã§å¾
ã¡ããå ´åã¯task.waitUntilExit()
ã§å¾
ã¤ã
çµäºã¹ãã¼ã¿ã¹ã¯task.terminationStatus
ã§åå¾ã§ããã
ä¸ã®ä¾ã ã¨ãå
¥åã®å¶å¾¡ã¯NSFileHandle.fileHandleWithStandardInput()
ã使ã£ã¦ããã
flushãã¦ãªãã£ãããaddObserverã®objectãinPipeã«ãã¦ã¦ã¯ã¾ã£ã...orz
func scriptWithCmd(cmd: String, arguments args: [String], currentDirPath currentDir: String? = nil) -> Int32 { //set task let input: NSFileHandle = NSFileHandle.fileHandleWithStandardInput() let inPipe: NSPipe = NSPipe() let outPipe: NSPipe = NSPipe() let task: NSTask = NSTask() task.launchPath = cmd task.arguments = args if currentDir != nil { task.currentDirectoryPath = currentDir! } task.standardOutput = outPipe task.standardInput = inPipe task.launch() //notification input.waitForDataInBackgroundAndNotify() outPipe.fileHandleForReading.waitForDataInBackgroundAndNotify() NSNotificationCenter.defaultCenter().addObserverForName(NSFileHandleDataAvailableNotification, object: input, queue: nil, usingBlock : { (notification: NSNotification!) in let inData: NSData = input.availableData if inData.length > 0 { inPipe.fileHandleForWriting.writeData(inData) input.waitForDataInBackgroundAndNotify() } else { inPipe.fileHandleForWriting.closeFile() } } ) NSNotificationCenter.defaultCenter().addObserverForName(NSFileHandleDataAvailableNotification, object: outPipe.fileHandleForReading, queue: nil, usingBlock: { (notification: NSNotification!) in let outData: NSData = outPipe.fileHandleForReading.availableData let outStr: NSString = NSString(data: outData, encoding: NSUTF8StringEncoding)! print(outStr) fflush(__stdoutp) outPipe.fileHandleForReading.waitForDataInBackgroundAndNotify() } ) task.waitUntilExit() return task.terminationStatus }
ä»ã®è¨èªã§å¤é¨ã³ãã³ãå¼ã³åºãç°¡æã¡ã¢
以åå°ã調ã¹ã¦ããã¤
Perl
exec()
ããããã©æ»ã£ã¦ããªãã®ã§system()
ã使ãã
ããã¯ã¯ã©ã¼ã便å©.
my $status = system "ls hoge/" #ret-code my $ret = `ls hoge/ ` #output
C
以åperlã ã¨é ãããã®ã§ãC/C++ã§ãããã¨ãã¦è²ã ãã£ã¦ããæã®ã¡ã¢
- system()
#include <stdlib.h> int ret = system("ls hoge/");
- popen()/pclose()
POSIXã ã¨popen/pclose
ã¨ã使ãã.
#include <stdio.h> #include <stdlib.h> #include <err.h> #define BUF 256 int main (void) { FILE *fp; char buf[BUF]; char *cmd = "/bin/ls hoge/"; if ((fp=popen(cmd,"r")) == NULL) { err(EXIT_FAILURE, "%s", cmd); } while(fgets(buf, BUF, fp) != NULL) { fputs(buf, stdout); } pclose(fp); return 0; }
- exec*()
popenã ã¨å
¥åºåãåæã«ã§ããªãã®ã§ãpipe()
, dup()
, fork()
, exec*()
ãããã使ãå¿
è¦ãããã
C++
PStreams(POSIX)ã使ããã
Java
ProcessBuilderã使ãã¿ããã ãã ã·ã§ã«ãç´æ¥è§£éããã³ãã³ãã¯ç´æ¥ã¯å®è¡ã§ããªãï¼
åè
- Javaå¤é¨ããã»ã¹èµ·åã¡ã¢(Hishidama's Java Process Memo)
- Java SE 7ã®ProcessBuilderã§ãªãã¤ã¬ã¯ã