将参数从 Qt 传递到 shell 脚本文件
pass arguments from Qt to a shell script file
我想将一个字符串值从Qt代码传递到我的shell脚本文件,名为'part1_4_md'。这是我从Qt的代码:
void MainWindow::on_pushButton_clicked()
{
QString path = ui->lineEdit->text();
QProcess *process = new QProcess;
process->start("/bin/bash", QStringList() << "/home/kha/Unix/QT/part1_4_md");
process->waitForBytesWritten();
process->waitForFinished();
QString output(process->readAllStandardOutput());
ui->textEdit->setPlainText(output);
QTextCursor textCursor = ui->textEdit->textCursor();
textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
}
我创建了一个 lineEdit 框,当用户在该框中键入字符串值并按下按钮时,我需要将该值传递给我的 shell 脚本文件,并且 shell 脚本文件使用该值运行。这是我的 shell 脚本文件的样子:
#!/bin/bash
files=($*);
dates=();
for (( i = 0; i<${#files[@]}; i++ ))
do
dates[$i]=$(date -r ${files[i]} +"%Y%m%d");
done
字符串值应传递给"$*"以用作"files"数组的参数。请告诉我如何做到这一点。
您已经通过 QProcess::start 的参数将脚本作为参数传递给字符串列表中的/bin/bash
arguments
。您可以将更多参数附加到列表中,bash 应该能够path
解释为脚本的第一个参数。
QString path = ui->lineEdit->text();
QString script("/home/kha/Unix/QT/part1_4_md");
QProcess *process = new QProcess;
process->start("/bin/bash", QStringList() << script << path);
如果这不起作用,您可以使用-c
bash 选项。
QString path = ui->lineEdit->text();
QString script("/home/kha/Unix/QT/part1_4_md");
QString command_string = script + " " + path;
QString c_option("-c");
QProcess *process = new QProcess;
process->start("/bin/bash", QStringList() << c_option << command_string);
请注意,通过在 GUI 线程中等待进程来阻止 UI。您可以连接到 done 和readyReadStandardOutput 以及错误信号,并异步运行进程或在单独的线程中运行它。
相关文章:
- 如何通过cpp程序运行shell脚本
- 我适合在我的构建系统中使用 shell 脚本而不是AX_CHECK_COMPILE_FLAG吗?
- CMake 引用我在 ~/bin 中创建的随机 shell 脚本(即使我删除了它)?
- 从带有参数的C++程序中调用shell脚本
- 通过C++应用程序运行 shell 脚本始于 JAVA
- 当 PHP、C++ 和 shell 脚本尝试访问同一个文件时,如何避免错误?
- 在 c++ 中捕获 shell 脚本退出状态
- 如何将我的 cli 信息(变量等)共享到子进程,这是一个 bash shell 脚本
- 为什么 C++ 中的 shell 脚本代码不起作用?
- C++:在Linux shell脚本中运行GDB
- C++中的程序如何使用"系统"变量与 Linux 中的 shell 脚本交互?
- 并行gnu通过shell脚本传递参数
- 将参数从 Qt 传递到 shell 脚本文件
- 运行 shell 脚本时出错:"pipe call failed"
- 从c++程序运行shell脚本会自动将shell脚本的输出显示到控制台吗
- 从shell脚本运行c scrips
- C 程序需要STDIN的输入,但我想从Shell脚本中输送/发送这些输入
- 如何在C++中正确运行 shell 脚本
- Shell脚本终止程序,但导致无法写入输出文件
- 基于模板的Shell脚本生成命令