当我通过Qt中的QProcess调用脚本时,很少有Shell命令不起作用 [英] Few shell commands doesn't work when I Invoke a script via QProcess in Qt

查看:108
本文介绍了当我通过Qt中的QProcess调用脚本时,很少有Shell命令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行某些命令的shell脚本,当我使用终端调用该脚本时,它工作正常,但是当我使用QProess调用该脚本时,很少有命令无法正常工作.

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.

这是脚本

#!/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"

这是我使用QProcess的方式

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");

如果我在shell中运行此脚本,效果很好,请获取name和age的值.但是,当我使用上述方法从Qt调用脚本时,我遇到了cut命令的错误,首先3个echo效果很好,然后我得到了cut命令的错误,因为 cut:命令未找到,然后什么都没有因为cut命令失败,所以出现echo命令.是什么原因呢?我提供了该过程的外壳程序,但仍然存在这些错误?

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

推荐答案

您可以尝试使用 sh 命令和一些测试参数

You can try with sh command and some test arguments

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

这篇关于当我通过Qt中的QProcess调用脚本时,很少有Shell命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆