在 shell 脚本中将带引号的参数传递给 C 程序 [英] Passing quoted arguments to C program in a shell script

查看:64
本文介绍了在 shell 脚本中将带引号的参数传递给 C 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C 程序main",它获取以下参数:

I have a C program "main" which gets the following parameters:

"a b c d ..." e f g

"a b c d ..." e f g

因为引用的原因,一共有4个参数.我有一个文本文件,每行都有这 4 个参数.我做了一个shell脚本来为每个参数运行C程序:

That's a total of 4 parameters, because of the quotation. I have a text file which each line has these 4 parameters. I made a shell script to run the C program for each of the parameters:

#!/bin/bash
while read line
do
    ./main "$line"
done < $1

问题是 C 程序将第一个被引用的参数识别为几个分隔的参数,就好像引用被忽略了一样.在我尝试过的许多事情中,值得一提的是,我尝试将文件中的每个引号更改为 \" 甚至从调用中删除引号 (./main $line).

The problem is that the C program is recognizing the first parameter, which is quoted, as several separated parameters, as if the quote was being ignored. Among the many things I've tried, it's worth mentioning that I tried changing each quotation in the file to \" and even remove the quotation from the call (./main $line).

推荐答案

#!/bin/bash
while read line
do
    eval set -- $line
    ./main "$@"
done < $1

这篇关于在 shell 脚本中将带引号的参数传递给 C 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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