Passing quoted arguments to C program in a shell script

随声附和 提交于 2021-01-27 23:41:19

问题


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

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

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

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).


回答1:


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


来源:https://stackoverflow.com/questions/20085303/passing-quoted-arguments-to-c-program-in-a-shell-script

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