首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >要从.bat文件和.bat文件中执行bash脚本,QT应用程序将调用

要从.bat文件和.bat文件中执行bash脚本,QT应用程序将调用
EN

Stack Overflow用户
提问于 2012-06-07 15:53:16
回答 3查看 5.8K关注 0票数 3

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

概述了一个问题:需要通过Qt应用程序将文件从传输到Linux,然后在中运行bash脚本来执行几个命令。

到目前为止所做的工作:可以通过Qt应用程序成功地将文件从windows传输到Linux。Qt应用程序调用传输该文件的批处理文件。

示例

代码语言:javascript
复制
 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),它执行命令如下

代码语言:javascript
复制
  pscp -pw "lol" "%TARGET_UPDATE%" squire@"%TARGET_IP%":"%BASE_DIR%"/ 

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

代码语言:javascript
复制
  putty -pw "lol" -m test-update.sh squires@"%TARGET_IP%"

我甚至尝试过这样的方法

代码语言:javascript
复制
  C:\\Program Files\\putty.exe -pw "lol"  -m test-update.sh squires@"%TARGET_IP%"

你们能告诉我我在哪里搞错了吗?

谢谢和问候,

相同的

EN

回答 3

Stack Overflow用户

发布于 2013-05-04 09:13:19

我认为使用QProcess::QString( cmdstring)比将参数传递给Batchfile本身要容易得多。

test.cpp

代码语言:javascript
复制
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

代码语言:javascript
复制
@echo off
echo hello %1

信息:要在IDE中测试这一点,请在发布文件夹中运行application.exe,并将批处理文件也包含在该文件夹中

票数 2
EN

Stack Overflow用户

发布于 2012-06-07 16:02:01

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

票数 0
EN

Stack Overflow用户

发布于 2012-06-07 16:17:57

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

代码语言:javascript
复制
 putty.exe -pw "lol" -m C:\\temp\\upgradingTest\\test-update.sh squires@"%TARGET_IP%"

而不是

代码语言:javascript
复制
   putty -pw "lol" -m test-update.sh squires@"%TARGET_IP%"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10935367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档