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.
Why don't you read the Bash Reference Manual? Note especially printf formatarguments.. and the note that "The format is reused as necessary to consume all of the arguments.".
Why don't you read the Bash Reference Manual? Note especially printf formatarguments.. and the note that "The format is reused as necessary to consume all of the arguments.".
In other words, try printf '%s %s\n' "$@"
hi
thanks for help
i learn for the final exam
and this is a question from on of the exams that i am trying to resolve (there is no any solution)
we didnt learn the "printf" in class so i dont expect that they will demand from us to use it in the exam
Rather than point out anything wrong about that statement... I will just give you a tip that works for me.
If I ask someone for an answer, I usually forget it by the end of the day. If I go through the process of figuring it out, searching for it, trying to understand it and make it work through trial and error, I never forget it,.. because I've "learned" something.
What I am saying is that it would benefit you to do some searching and reading and experimenting.
Probably, no one here is going to help you do your homework or a test.
Rather than point out anything wrong about that statement... I will just give you a tip that works for me.
If I ask someone for an answer, I usually forget it by the end of the day. If I go through the process of figuring it out, searching for it, trying to understand it and make it work through trial and error, I never forget it,.. because I've "learned" something.
What I am saying is that it would benefit you to do some searching and reading and experimenting.
Probably, no one here is going to help you do your homework or a test.
hi
i dont have time to learn new commands in bash because i have alot of exams right now...
i solved all the bash question in this exams however this question i really dont have an idea
moreover, our lecturer told us that in the exam , they will ask only about some basic commands in bash that we learnt
i really appreciate the help of Nominal Animal and its really worked for me..
i only asked for another solution if someone can suggest me
ps when i have some free time i will search and learn more about bash
moreover, our lecturer told us that in the exam , they will ask only about some basic commands in bash that we learnt
Whoever taught you basic scripting without printf is an utter idiot who should be fired. I am not kidding, and I am not exaggerating.
It is like making people dig trenches with spoons, while everybody has a perfectly good combat shovel in their pack.
If you had bothered to put the command I showed you into a script -- two lines total, and the first line is obviously the shebang line -- you'd see how it perfectly and completely solves the assignment. You cannot get more basic than that.
As to printf, it is not only more powerful than echo, but at least as widely supported. In Bash and POSIX shells, it is built-in to the shell, and actually recommended for use instead of echo. printf works in just about every shell I've ever used -- and for the rest, there is the equivalent /usr/bin/printf command.
Not to mention that some form of printf is also found in just about all of the other common programming languages out there, including awk and perl. Once you learn the basics, your skill will be easily transferable.
Here's a good one-page explanation of printf in bash: printf
Anyway, to do it without printf, I'd simply use a for loop to loop through the input parameters. Copying the input parameters into a true array first would make it easier, by allowing you to use the shell's internal arithmetic to print two at a time. Or you could use shift to move through them directly.
BTW, while I do sympathize to some extent, complaining that you don't have time to study something new just comes across as whining. You aren't likely to get any sympathy from anyone saying such things. We're happy to guide posters towards the correct solutions, but nobody is obliged to supply whole solutions. It's much better for you in the long run if you learn how to do such things yourself anyway, as szboardstretcher said.
Anyway, to do it without printf, I'd simply use a for loop to loop through the input parameters. Copying the input parameters into a true array first would make it easier, by allowing you to use the shell's internal arithmetic to print two at a time. Or you could use shift to move through them directly.
HI
thanks you,
i will be happy to hear from you the way with for,
i read and understand about printf in the first link in this thread
i will be happy to hear a new way,
to be honest at the first time that i solved this problem i tired to solve it with for loop
however i had some problem to do it, because i didnt know how to print two elements in the same line
i=0
for i in $#; do
)var1=(i
var2=(i+1)
echo $var1 $var2
i=i+1
i=i+1
done;
It looks like you have the basic idea right, but the execution is all wrong.
a) "$#" spits out a single number, the total number of parameters, so your loop will only run once.
b) The parentheses are all in the wrong places, particularly line 3.
c) Arithmetic expansion is done inside $((..)) brackets anyway.
d) Variable expansions should always be quoted.
e) You try to increment (it needs to be done in an arithmetic context) the i variable at the end (twice, unnecessarily) , but the next iteration of the loop, if it ever occurred, would overwrite it anyway.
f) Good formatting makes code more readable and debuggable. Indent all sub-commands to the same level, and add some blank lines between related blocks of code.
If you want to use the parameters directly, simply print numbers one and two inside a while loop, followed by "shift 2" to move to the next two in the list on the next iteration. The test for the loop is whether there are parameters available to print.
Or if you want to make it a little more complex (but perhaps more robust), you need to first transfer the parameters into an actual array, allowing you to then manipulate the index values with shell arithmetic*. A c-style for loop that counts two at a time would likely be the best option for counting.
i=0
for i in $#; do
)var1=(i
var2=(i+1)
echo $var1 $var2
i=i+1
i=i+1
done;
It looks like you have the basic idea right, but the execution is all wrong.
a) "$#" spits out a single number, the total number of parameters, so your loop will only run once.
b) The parentheses are all in the wrong places, particularly line 3.
c) Arithmetic expansion is done inside $((..)) brackets anyway.
d) Variable expansions should always be quoted.
e) You try to increment (it needs to be done in an arithmetic context) the i variable at the end (twice, unnecessarily) , but the next iteration of the loop, if it ever occurred, would overwrite it anyway.
f) Good formatting makes code more readable and debuggable. Indent all sub-commands to the same level, and add some blank lines between related blocks of code.
If you want to use the parameters directly, simply print numbers one and two inside a while loop, followed by "shift 2" to move to the next two in the list on the next iteration. The test for the loop is whether there are parameters available to print.
Or if you want to make it a little more complex (but perhaps more robust), you need to first transfer the parameters into an actual array, allowing you to then manipulate the index values with shell arithmetic*. A c-style for loop that counts two at a time would likely be the best option for counting.
No, it is a working script that performs the task OP asked for.
It is, in fact, an experiment. It is not really obfuscated code; it is more of a puzzle. If the reader bothers to dissect it, they will find out about quite a few useful Bash programming techniques. At some point, the reader is very, very likely to have a light bulb moment, and realize how the entire question can be simplified. The final price is the simplification to a single line using a Bash/POSIX shell built-in (which I've already explicitly written out earlier in this thread).
As an experiment, it obviously failed. Even you did not bother to test it.
Since I've already stated the proper single-command solution in an earlier post, I just could not bring myself to describe a "while [ $# -gt 1 ]; do ... shift 2 ... done" loop either.
@ofer4: No malice was intended. Perhaps the tone of exasperation/frustration was too strong there, but as I said, if you ever bother to dissect that script, you'll find out about quite a few interesting Bash techniques.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.