2

I want to do the "echo" command, which is used to show the variable content in the linux terminal, using the GUI. I wrote this using qProcess, but it only printed $SHELL as output.

QString cmd = "echo $SHELL";
QProcess *process = new QProcess;
process->start(cmd);
process->waitForBytesWritten();
process->waitForFinished();
qDebug() << process->readAll();`

Qt code output:$SHELL ........................................

Terminal command: [intern2atlas SETUP]$ echo $SHELL

output:/bin/tcsh

elif mutlu
  • 83
  • 5

2 Answers2

0

use QProcessEnvironment or qgetenv or similar to access environment variables. If what you want to do really needs to invoke a shell, pass that process first (e.g. sh). Neither echo nor QProcess are a shell and thus won't interpret the environment variable $SHELL for you.

milianw
  • 5,164
  • 2
  • 37
  • 41
0

This is a example of code. Pls try with this:

   env.insert("TERM", "xterm");
    processo.setProcessEnvironment(env);

    //comando 1 - limpar a pasta
    comando = "echo";
    args.clear();
    args<<"$TERM";
    fullCommand = comando;
    for(int i = 0; i < args.count(); i++)
        fullCommand += " " + args.at(i);
    processo.start(comando,args,QIODevice::ReadOnly);
    processo.waitForFinished();
    stdOut = processo.readAllStandardOutput();
    stdError = processo.readAllStandardError();
    ui->commandTextEdit->append(fullCommand);
    ui->stdOutTextEdit->append(stdOut);
    ui->stdErrorTextEdit->append(stdError);

Refer: http://www.qtcentre.org/threads/58131-Execute-system-command-from-QProcess

Chandana Kumara
  • 2,485
  • 1
  • 22
  • 25