2014-01-26 39 views
1

我想通過中的system()函數運行nodejs程序。我可以在終端運行bash命令/usr/local/bin/node ~/some_folder/xml2js.js,但是當我跑了這一點:Nodejs和UNIX新手:在cpp程序中運行node命令

int main(int argc, const char * argv[]) 
{ 

    // insert code here... 
    //system("/usr/local/bin/node ~/some_folder/xml2js.js"); 
    system("~/some_folder/run.sh"); 
    std::cout << "Hello, World!\n"; 
    return 0; 
} 

它告訴我node: command not found。 的run.sh低於:

#! /bin/bash 
node ./xml2js.js 

是否有任何其他的方式來調用UNIX其他程序?以及如何獲得命令的輸出到標準輸出?

回答

0

嘗試編寫該腳本的絕對路徑。它應該工作。

假設用戶的個人文件夾位於/home,您可以嘗試system("/home/$(whoami)/some_folder/run.sh")

另外,該腳本是否具有執行權限?另外,請檢查系統的返回碼。

+0

我很抱歉,但我忘了告訴'run.sh'是什麼。 – zsf222

+0

您是否嘗試在腳本和'system()'中使用完整路徑? – Alex

+0

另外,如果您不想使用完整路徑,請在腳本中嘗試'node xml2js.js'(不帶./)。 – Alex