从 .bat 文件和 .bat 文件执行 bash 脚本由 QT 应用程序调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10935367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:27:13  来源:igfitidea点击:

To execute bash script from .bat file and .bat file is called by QT application

qtbashbatch-file

提问by samantha

I need help regarding executing bash script from batch scrip(.bat) which is called by Qt application inside Windows.

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

Overview of a problem:I need to transfer files from Windows to Linux box via Qt application and run bash script inside linux to execute few commands.

问题概述:我需要通过 Qt 应用程序将文件从 Windows 传输到 Linux box,并在 linux 中运行 bash 脚本来执行一些命令。

What I have done so far :I can successfully transfer the files from windows to Linux box via Qt application. The Qt application calls the batch file which transfers the file

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

example

例子

 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();

  }


I have writen a batch script(.bat) which executes command like

我写了一个批处理脚本(.bat),它执行像

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

For executing bash script via batch file below is the command inside batch file

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

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

i have even tried something like that

我什至尝试过类似的事情

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

Could you guys please let me know where am i making mistake?

你们能不能让我知道我哪里出错了?

thanks and regards,
Sam

谢谢和问候,
山姆

回答by lx222

I think it is easier to use QProcess::execute(QString cmdstring) Than you can also pass arguments to the Batchfile itself

我认为使用 QProcess::execute(QString cmdstring) 比您还可以将参数传递给 Batchfile 本身更容易

test.cpp

测试.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

Info: To test this in your IDE make shure that you are running your application.exe from the release folder and have the batch file also in that folder

信息:要在您的 IDE 中对此进行测试,请确保您正在从发布文件夹运行 application.exe 并将批处理文件也放在该文件夹中

回答by Kamil Klimek

You can't call waitForStarted()twice per run. If waitForStarted()returns false you're returning from method execution. So after if( ! waitForStarted()your process is running.

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

回答by samantha

After spending many hours , I realised i need to give full path name in the batch script for Qt to execute it. Without qt it works fine but if I want to run batch file via QT app then I guess I need to give fully qualified path name of the bash script. example

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

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

instead of

代替

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