0

Hi I have a shell script which executes some commands, when I invoke that script using terminal it works fine but when I invoke the script using a QProess few commands doesn't works well.

Here is the script

#!/bin/bash

echo "Invoking the script"
euid=$1
if [ $# -ne 1 ]; then
echo "Arguments missing"
      exit 1
fi

echo "arg 1: $1"
data=$1;
name=$(echo $data | cut -b 1-7)
age= $(echo $data | cut -b  10-11)
echo "$name"
echo "$age"

Here is the way I use QProcess

// Environment setup
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
candidateProcess->setProcessEnvironment(env);
QString script("/home/root/scripts/getInfo.sh");
candidateProcess->start("/bin/bash", QStringList() << script << qwertyuand21");

If I run this script in shell It works fine, get the value of name and age. But when I invoke the script from Qt using the above stated method I get error for cut command, First 3 echo works well, then I get error for the cut command as cut : command not found and then nothing comes up for the echo commands because cut command failed. What is the reason for that? I have provided shell for the process but still these errors??

newbie
  • 64
  • 3
  • 1
    It looks like the `QProcess` is running with a wrong or incomplete environment -- specifically the `PATH` variable. Try doing [`Process->setProcessEnvironment(QProcessEnvironment::systemEnvironment())`](http://doc.qt.io/qt-5/qprocess.html#setProcessEnvironment) before invoking `QProcess::start`. Note: it would be more helpful if you showed the *real* code you're using to create/start the `QProcess`. – G.M. May 19 '17 at 08:15
  • This is the real code, – newbie May 19 '17 at 08:28
  • Then you're not supplying enough arguments to the script and it will exit before it even gets to the point of invoking `cut` -- right? – G.M. May 19 '17 at 08:31
  • I added this `// Environment setup QProcessEnvironment env =QProcessEnvironment::systemEnvironment(); candidateProcess->setProcessEnvironment(env);` But still same error. – newbie May 19 '17 at 08:52
  • @G.M is there any other way? – newbie May 19 '17 at 12:39
  • Supply the absolute path to `cut` IE `/usr/bin/cut` in your script or fix the PATH in your script to explicitly add `/usr/bin` to your PATH. Chances are, PATH is getting wiped out by Qt and your script is searching for `cut` in the `pwd`. – Jim May 20 '17 at 04:06

1 Answers1

0

You can try with sh command and some test arguments

QString script("/home/root/scripts/getInfo.sh arguments123456");
Process->start("/bin/sh ", QStringList() << script");
Mandar
  • 1,006
  • 11
  • 28