|
-
February 13th, 2011, 11:53 PM
#1
[RESOLVED] Passing program arguments
This is probably more of a unix bash shell question, but I didn't know where else to post. My apologies if this is the wrong forum for this question.
Here is a file with the following contents (4 lines, let's call the file f)...
hello
a b c
“d e f”
'x y z'
Note the double and single quotes on some of the lines.
Here's a C++ program (let's call it nargs)...
Code:
#include <iostream>
int main (int argv, char* argc [])
{
std::cout << “\n argv = “ << argv << std::endl;
}
1) Executing the following command from the bash shell will output the following...
$ while read line; do printf “$line “; done < f
hello a b c “d e f” 'x y z'
2) Executing the following command from the bash shell will output the following...
$ nargs hello a b c “d e f” 'x y z'
argv = 7
3) Executing the following command from the bash shell will output the following...
$ nargs $(while read line; do printf “$line “; done < f)
argv = 11
What is going on here? I know the shell is performing some kind of translation regarding the quotes, but why the inconsistency? In #1, the quotes are present in the output. In #2, I've physically typed the quotes, just like they've appeared in #1. In #3, the quotes are obviously gone since the C++ program received 11 arguments, vice 7 in #2.
I've also tried using backslashes between the spaces instead of quotes. This doesn't work either. Is there a way to get around this problem? I haven't run across any shell settings to solve this.
-
February 14th, 2011, 07:50 AM
#2
Re: Passing program arguments
Try:
Code:
cat f | xargs nargs
-
February 15th, 2011, 03:20 PM
#3
Re: Passing program arguments
 Originally Posted by Marc G
Try:
Code:
cat f | xargs nargs
Thank you for the response. Although my post wasn't my exact scenario, I tried to simplify it as much as possible to get to the root of the problem. And yes, your answer not only solved my post, but applying the same idea to my actual problem was solved as well. Thanks again.
I do have another problem related to a very similar idea using xargs, but since no C++ coding is involved, I'll go and post it on a unix forum.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|