Using QProcess to run a Shell script file

246 Views Asked by At

I've compiled a C++ program (using G++) and it generated a file that I'll call shellScript.sh.

Using bash.exe from Windows Subsystem for Linux (WSL), I can run it by going to the directory where shellScript.sh is in and typing in ./shellScript.sh.

Instead of having to do all of that manually, is it possible to have a runShellScript button, which when clicked uses the QProcess class to open up (and keep open) a CMD or PowerShell window with a command entered into it that automatically runs shellScript.sh. The file, shellScript.sh is located here: QCoreApplication::applicationDirPath() + "/shellScript.sh"

Hyde's suggestion that I tried to implement

runPowerShellScript.bat (runs runBashScript.ps1)

@echo off
powershell -ExecutionPolicy RemoteSigned -File %~dp0runBashScript.ps1
pause

runBashScript.ps1 (runs shellScript.sh)

wsl -e ./shellScript.sh

If I double click runPowerShellScript.bat on Windows Explorer that runs runBashScript.ps1, which runs shellScript.sh with no problems whatsoever

helloWorld.bat (used for test purposes)

@echo off
echo Hello world!

runBatchScript button code (runs Batch scripts) I tested it and it ran helloWorld.bat. In a new Command Prompt window, I saw the message "hello world"

#include <QString>
#include <QStringList>
#include <QProcess>

void MainWindow::on_runBatFile_clicked()
{

    QString filePath =  QCoreApplication::applicationDirPath() + "/helloWorld.bat";
    QStringList arguments;
    arguments << "/C";
    arguments << "start";
    arguments << filePath;
    arguments << "/K";

    QProcess proc;
    proc.start("cmd.exe", arguments);
    proc.waitForFinished();


}

The runBatFile button nonetheless failed to run runPowerShellScript.bat how I wanted it to. Looking at the images below, it fails to run a .bat file that runs a PowerShell script file for reasons unknown to me

What I saw What I see

What I was hoping to see What I was hoping to see

runShellScript button code (Almost works as I was hoping. It opens a PowerShell window up, puts it into Bash / WSL mode, and goes to the directory we want. I just don't know what I need to add to the code to make it run shellScript.sh from here. I know that if I manually inputted ./shellScript.sh that would run it but again, I would like this to be done through code (upon clicking a button) if possible) )

#include <QString>
#include <QStringList>
#include <QProcess>

void MainWindow::on_runShellScript_clicked()
{
// Can use either bash or wsl, it doesn't matter
    QString commandA = "wsl";
    QString commandB = "bash";

    QString filePath =  QCoreApplication::applicationDirPath() + "/runBashScript.ps1";
    QStringList arguments;
    arguments << "/C";
    arguments << "start";
    arguments << "powershell";
    arguments << commandA;

    QProcess proc;

    proc.setWorkingDirectory(QCoreApplication::applicationDirPath());
    proc.start("powershell.exe", arguments);
    proc.waitForFinished();

}
0

There are 0 best solutions below