【问题标题】:To execute bash script from .bat file and .bat file is called by QT application从 .bat 文件和 .bat 文件执行 bash 脚本由 QT 应用程序调用
【发布时间】:2012-06-07 15:53:16
【问题描述】:

我需要有关从 Windows 中的 Qt 应用程序调用的批处理脚本(.bat)执行 bash 脚本的帮助。

问题概述: 我需要通过 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();

  }

我编写了一个批处理脚本(.bat),它执行类似的命令

  pscp -pw "lol" "%TARGET_UPDATE%" squire@"%TARGET_IP%":"%BASE_DIR%"/ 

通过批处理文件执行 bash 脚本如下是批处理文件中的命令

  putty -pw "lol" -m test-update.sh squires@"%TARGET_IP%"

我什至尝试过类似的东西

  C:\\Program Files\\putty.exe -pw "lol"  -m test-update.sh squires@"%TARGET_IP%"

请各位大佬告诉我我哪里出错了?

感谢和问候,
山姆

【问题讨论】:

    标签: qt bash batch-file


    【解决方案1】:

    我认为使用 QProcess::execute(QString cmdstring) 更容易 你也可以将参数传递给批处理文件本身

    test.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,并将批处理文件也在该文件夹中

    【讨论】:

      【解决方案2】:

      您不能每次运行两次调用waitForStarted()。如果 waitForStarted() 返回 false,则表示您正在从方法执行中返回。所以在if( ! waitForStarted() 之后你的进程正在运行。

      【讨论】:

      • 是的,你是对的。我忘记更改以更改代码。我现在会修改它。但它仍然无法运行
      • 花了很多小时后,我意识到我需要在批处理脚本中给出完整的路径名,以便 Qt 执行它。没有 qt 它可以正常工作,但是如果我想通过 QT 应用程序运行批处理文件,那么我想我需要提供 bash 脚本的完全限定路径名
      【解决方案3】:

      花了很多小时后,我意识到我需要在批处理脚本中给出完整的路径名,以便 Qt 执行它。没有 qt 它可以正常工作,但是如果我想通过 QT 应用程序运行批处理文件,那么我想我需要提供 bash 脚本的完全限定路径名。 例子

       putty.exe -pw "lol" -m C:\\temp\\upgradingTest\\test-update.sh squires@"%TARGET_IP%"
      

      而不是

         putty -pw "lol" -m test-update.sh squires@"%TARGET_IP%"
      

      【讨论】:

        猜你喜欢
        • 2011-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多