Few shell commands doesn't work when I Invoke a script via QProcess in Qt

微笑、不失礼 提交于 2021-02-08 10:28:25

问题


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??


回答1:


You can try with sh command and some test arguments

QString script("/home/root/scripts/getInfo.sh arguments123456");
Process->start("/bin/sh ", QStringList() << script");


来源:https://stackoverflow.com/questions/44064474/few-shell-commands-doesnt-work-when-i-invoke-a-script-via-qprocess-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!