2012-06-07 248 views
3

我需要從由Windows內部的Qt應用程序調用的batch scrip(.bat)執行bash腳本的幫助。從.bat文件執行bash腳本,.bat文件由QT應用程序調用

問題概述:我需要通過Qt應用程序將文件從Windows傳輸到Linux,並在Linux內運行bash腳本來執行幾個命令。

我到目前爲止所做的事情:我可以通過Qt應用程序成功地將文件從Windows傳輸到Linux機器。 Qt的應用程序調用該

void Qt_intro101::on_file_upgrade_clicked() 
{ 
    QFileInfo fileInfo(ui->selected_file->text()); 
    if(!fileInfo.exists()) 
    { 
     QMessageBox::information(this, tr("Information"), 
          tr("Unable to find file for upgrading!")); 
     return; 
    } 

    // copying update 
    QString fileName = fileInfo.absoluteFilePath(); 

     //Check if cmd.exe is present in Clients system and is located at correct path 
     QFileInfo cmdFile("C:\\Windows\\system32\\cmd.exe"); 
     if(!cmdFile.exists()) 
     { 
      QMessageBox::information(this, tr("Information"), 
            tr("Failed to find the cmd.exe ... Check cmd.exe is  installed and is in C:\\Windows\\system32\\ !")); 
      return; 
     } 

    QStringList arguments ; 
    arguments << " /c" <<"c:\\temp\\upgradeTesting\\test.bat"<< fileName ; 
    QProcess *process = new QProcess(this); 
    process->start(cmdFile.absoluteFilePath(), arguments) ; 
    if(!process->waitForStarted()) 
    { 
      QMessageBox::information(this, tr("Information"), 
          tr("Failed to start the process for upgrading!")); 
      return; 
     } 


      QMessageBox::information(this, tr("Information"), 
          tr("Please wait while system is upgrading .. click Ok to exit this box")); 
     qDebug() << fileName ; 
     process->waitForFinished() ; 
     qDebug() << process->readAllStandardOutput(); 
     QMessageBox::information(this, tr("Information"), 
          tr("Upgradation is successful.. Please restart the system")) ; 
      process->deleteLater(); 

    } 

我已經writen批處理腳本(蝙蝠),它執行命令等

pscp -pw "lol" "%TARGET_UPDATE%" [email protected]"%TARGET_IP%":"%BASE_DIR%"/ 

爲了執行文件

例如傳送批處理文件bash腳本通過以下批處理文件是批處理文件中的命令

putty -pw "lol" -m test-update.sh [email protected]"%TARGET_IP%" 

我甚至試過類似的東西

C:\\Program Files\\putty.exe -pw "lol" -m test-update.sh [email protected]"%TARGET_IP%" 

難道你們,請讓我知道我在哪裏犯錯?

感謝和問候,
山姆

回答

0

每次運行你不能叫waitForStarted()兩次。如果waitForStarted()返回false,則表示正在從方法執行返回。所以在if(! waitForStarted()之後你的程序正在運行。

+0

是你的權利英寸我忘記更改以更改代碼。我現在會推薦它。但仍然無法運行 – samantha

+0

花了很多時間之後,我意識到我需要在批處理腳本中提供完整的路徑名,以便Qt執行它。沒有QT它工作正常,但如果我想通過QT應用程序運行批處理文件,那麼我想我需要給出完全合格的bash腳本的路徑名 – samantha

0

花了很多小時後,我意識到我需要在批處理腳本中提供完整的路徑名以供Qt執行。沒有QT,它工作正常,但如果我想通過QT應用程序運行批處理文件,那麼我想我需要給出完全合格的bash腳本的路徑名。 代替例如

putty.exe -pw "lol" -m C:\\temp\\upgradingTest\\test-update.sh [email protected]"%TARGET_IP%" 

putty -pw "lol" -m test-update.sh [email protected]"%TARGET_IP%" 
2

我覺得它更容易使用QProcess中::執行(即QString cmdstring) 比你還可以傳遞參數給批處理文件本身

測試。 cpp

QFileInfo cmdFile("C:\\Windows\\system32\\cmd.exe"); 
QProcess *process = new QProcess(this); 
process->execute(cmdFile.absoluteFilePath() + " /c helloparam.bat \"my param\" "); 
process->waitForFinished() ; 
qDebug() << process->readAllStandardOutput(); 

helloparam.bat

@echo off 
echo hello %1 

信息:要在你的IDE測試這讓你運行從發佈文件夾application.exe舒爾具有批處理文件也該文件夾