[SOLVED] C Program in Bash :Passing Newline in Command Line Arguments
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C Program in Bash :Passing Newline in Command Line Arguments
Hi,
I have a C program under Ubuntu linux that processes data from a text file.The text file actually contains float values seperated by newlines.The program reads this text file on startup and processes the numbers in it.
However it would be great if i could just pass the actual numbers in the text file as arguments to the program directly.
These numbers are values from a website which are seperated by newline.Is there a way that i can select this numbers directly from the website and then paste them as arguments to my C program.
I know of tr ,but can it be used for this task in any way.By using 'tr' and 'here' document i was able to change the newline to space.But i cannot give the output to my c program.
Can anybody help me on this.
thanks in advance
tux
yes,i can modify the program for that.What it currently does is ,it reads numbers from file and pass them for processing.But instead if i can give numbers seperated by newline itself in command line,i could remove the file reading portion and process the numbers directly.At that time i couldnt think of anyway other than to store the values in a file and read it from there.Thats why i introduced the file reading part.It is better if i can avoid that intermediate step.
Hello I think i have found a solution.It is as follows.I have to include the tr portion in back quotes.Then after the 'here document' start, i paste the numbers from the web.After that i have to give % and then end the back quote.Then i am calling my program with the variable 'a'.It works.
Once you have this working you can find an alternate way to retrieve the data from the website, maybe wget or Perl or some such ... maybe even from within your application which you pass the web address
Ultimately the answer will rely on how much work you wish to do and perhaps consideration of whether other users may use it or in fact how often you may use it?
I am also little confused about why the question is confusing.
Normally when we run a "c program with command line arguments" from command line in bash,we do as follows
bashprompt$./prog.o 1 2 3 4
But my queston is "what happens when the 1 2 3 4 are seperated by newline" like following
bashprompt$./prog.o 1
2
3
4
Here the program will only take 1 and the bash prompt will report error for other args as "unknown command."
The website from where i take data contain these numbers in a column seperated by newline.When I copy and paste it to bash prompt,it will take only the first value.That was the issue.I have only found a workaround using 'tr' as mentioned in my previous post.I hope there are more elegant solutions to it,as I have heard that Bash can pass arguments like this to programs.
{You can try copying the above post itself and pasting after an echo in the bash prompt to see what my problem is!!!!}
what I want to do is to convert the newlines in the arguments to "spaces" before passing it to my program
And you are more than welcome to do so, but what NevemTeve and i have tried to tell you is that the C program can be made (or already does know how) to deal with newlines, the only diffrence
is how you pass it to your program.
Your example is inaccurate because prior to pasting, if you wish to pass new line separated data:
Code:
1
2
3
4
to your program, you must, like you would pass it to any other program or script, quote the data to maintain the newlines.
So your example should look like:
Code:
bashprompt$ ./prog.o '1
2
3
4'
Now the program will see this as a contiguous string which happens to be separated by new lines.
Thanks.It is very simple, it seems.I may have missed that method first when i tried with my program.I am not sure now.I have to test it later in my linux box and will comment.
Well hoping that it will work I want to comment that "This is exactly why i posted the question here.I know that there was some simple solution and I was missing it".
The suggestion of you in post 8 using xargs worked well.(I havent tested the others).Thankyou verymuch. However the '--' after -d also gets passed as an argument to the program.But when I removed the -- everything worked as expected.Thankyou again.
<code>
$ xargs myecho -d --
<paste the data here -- mouse-middle-button / Shift+Insert>
<Ctrl+D>
myecho -debug: 4 arguments:
1: '1'
2: '2'
3: '3'
4: '4'
</code>
grail,
the passing of args by '' didnt go as I expected in the program.In that case the program gets all the arguments, but they still contain newlines.SO the program needs to process and remove them.I needed something that replaces newlines with spaces before passing to the program.The solution using xargs mentioned above worked fine.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.