bash scripting homework question
Write a shell script to reverse the set of numbers given in command line except the last argument.
Display the last argument as it is in the end. See the output to get more details. The script can take any number of arguments as input. $ ./revbutlast.sh 1 2 3 4 5 The output is 4 3 2 1 5 $ ./revbutlast.sh 4 3 2 1 The output is 2 3 4 1 $ ./revbutlast.sh ./revbutlast.sh expects arguments $ ./revbutlast.sh 1 The output is 1 --- Without using any loop like while, for write the script and name it as revbutlast_withoutloop.sh |
haha, nice.
this is not urgent for us at all. you've not said please or thank you you've not even tried to hide that it's homework, and copied it verbatim. Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member. |
I recommend you start doing the homework. A better (answerable) question will occur to you once you do.
|
thank you guys..its ok its apppearing on google..
can anyone tell how is it done using Bash scripting |
it's appearing on google??? huh??
No, we can't. Go learn something for yourself. |
i mean its ok if the question appears on google...actually i wasted 4 hrs on it and developed a headache...pls help
|
So what do you have so far, and what is it doing/not doing?
|
Quote:
Since you're too lazy to even TRY to do your own homework or search for anything, I'll spoon-feed you a link: http://tldp.org/LDP/abs/html/ There. Should be AMPLE information to help you write your own shell script. Otherwise, as others have said, post what YOU have written so far, and tell us where you're stuck, and we will HELP you. You will NOT find anyone here to do your work for you, though. |
ACtually it is prohibited to use any loops...and i'm stuck as in if i take 5 aregments...how will sort them or where to start
|
i'm just a beginner and trying hard...its just a week since i touched shell scripting...working in a small company where i was given ths assignment..i finished many programs on my own....regarding this one if i get hint also,it wil help a lot
|
Who said anything about using loops?
Where are you stuck? You need to tell us where you're at, we are not going to write this for you, nor hold your hand through the process. We will provide advice about specific problems you run into while YOU write the script. Start here: http://tldp.org/LDP/abs/html/interna...es.html#APPREF |
I read your initial post and, due to very little linux scripting experience, wasn't even sure it was possible to do without a loop.
So I thought about what might be possible and googled and guess what? I wrote a script to do it and learned some bash in the process. It was quite fun. Your teacher set you this so you would learn commands. Those hours on google were only wasted if you learned nothing. Share what you have done, and what you know, and somebody may help. It helps nobody if someone writes this for you. I'll give you a tip: learn about different ways to access arguments within scripts. |
its not possible to go through all this in one nite....i have to subit tomo
|
They have asked you many times, post what you have done so far and show where you are stuck. Ask specific questions about where you are stuck
|
Quote:
However, if you post what you came up with in those four hours, we might be able to help with that. |
#!/bin/bash
if [ $# -eq 0 ] then echo "please enter some arguments" fi x=$# ---now how to proceed |
#!/bin/bash
if [ $# -eq 0 ] then echo "please enter some arguments" fi x=$# ---now how to proceed |
hint:rev
|
Quote:
If you bothered to look at the bash scripting tutorial I posted to you before (or if you showed ANY effort into looking things up before you came here), you'd have had your solution. Again, we will spoon-feed you an answer: http://tldp.org/LDP/abs/html/interna...s.html#ARGLIST It shows you how to read multiple command-line arguments, and print them out. And 'rev' was mentioned...look it up. |
Code:
x=$@ # copy parameters to variable x |
Quote:
Code:
$./test.sh 1 2 13 5 4 |
I know. Sed would be more appropriate.
|
I did it with rev too, but with more lines.
I'd be interested to know whether there's a way to do it without rev or any loops. |
This works without rev and loops the last number is also reversed but changing that is trivial.
Code:
echo 182 3 6 7 81 |tr ' ' '\n' | sed -n '1!G;h;$p' |tr '\n' ' ' ; echo |
I was just thinking: why not concatenate all of the arguments to a single line and then sort that line?
Googled for how to sort items on a single line and got this: http://www.unix.com/302353987-post2.html Sort has a switch (finding it is your problem) that will cause it to sort in numerical order rather than lexical order. You should know how to get all of the arguments on a single line from following this discussion alone. I've just told you how to solve this. You're welcome. |
All arguments on one line is trivial
Code:
x=$@ Code:
xargs -n 1 |
Code:
|
Could you please edit the thread title to be more descriptive and appropriate to your problem? Say, "bash: sorting the arguments"?
|
Your programs gives problems with more then 10 parameters. A little adaptation.
Code:
#!/bin/bash |
Uhm, whizje, are you sure you should be trying to actually give him a a complete answer in the form of code? Remember that this is homework.
|
No, but I like Gajovy solution very much and maybe studies the OP the solutions. (And it's a rather simple problem if you cheat homework with this you will only cheat on yourself when you get your exam.)
|
@suicidaleggrol:
x=$@ # copy parameters to variable x result=$(echo $x | rev | cut -d ' ' -f 2-) echo $result ${x##*' '} can u pls explain line by line |
@TBs:
i'm in training right now ....they give probs for us to solve so that we can get familiar with bash..if u cant why criticize..this forum is for help...i'm not asking anyone to do my homework..my intention is not such...and i've tried rev command..it reverses the whole line..but i want last argument to be same... i got the answer now..thanks to suicedaleggroll ---------- Post added 05-03-12 at 10:24 AM ---------- thank you so much suicedaleggroll...you've been a great help dude.. |
@suicedaleggroll the thing is when typing 1 2 3 54 5 we get>>>45 3 2 1 5...i dont the the number 54 to be reversed
|
@whizje: i know ddude.i'm not cheating...trying to learn new commands..i think all of u have good experience..i 'mm learning bash since 7 days...so understand if i'm stuck it takes forever...and thank you for ur concern
|
Quote:
_________________________________ arungala, to complete this task I suggest the following lecture: man bash use / to search and search for "parameter:\offset" man tac you'll probably find "man cat" helpful too man cut If this is a homework, I really suggest reading "man bash" a lot. If you have to write scripts for a company, find yourself another job before they fire you. |
i have 3 months..i think i wil learn lot more
|
A somewhat nicer output
Code:
#!/bin/bash |
Whizje my friend thank you...can u also explain this for better understanding of bash
x=$@ # copy parameters to variable x result=$(echo $x | rev | cut -d ' ' -f 2-) echo $result ${x##*' '} |
lmaooo @
Quote:
I have the least amount of lines so I win :d :d :d (jk) Quote:
|
Why are we bothering to ask people not to help others cheat when they just do it anyway?
|
no use explaining you guys..its complicated...thank you for helping anyways
|
Quote:
|
ok i agree you're the honest god of linux..happy?
|
Code:
X=$@ # copy parameters to string x |
Quote:
|
Quote:
Quote:
|
Quote:
Quote:
|
Quote:
|
Quote:
|
All times are GMT -5. The time now is 07:41 PM. |