我需要有关从批处理脚本(.bat)执行bash脚本的帮助,Qt应用程序在Windows中调用了这个脚本。
概述了一个问题:需要通过Qt应用程序将文件从传输到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%"你们能告诉我我在哪里搞错了吗?
谢谢和问候,
相同的
发布于 2013-05-04 09:13:19
我认为使用QProcess::QString( cmdstring)比将参数传递给Batchfile本身要容易得多。
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,并将批处理文件也包含在该文件夹中
发布于 2012-06-07 16:02:01
您不能每次运行调用waitForStarted()两次。如果waitForStarted()返回false,则从方法执行返回。因此,在if( ! waitForStarted()之后,您的进程正在运行。
发布于 2012-06-07 16:17:57
在花了很多小时之后,我意识到我需要在批处理脚本中为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%"https://stackoverflow.com/questions/10935367
复制相似问题