LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash scripting homework question (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-homework-question-942925/)

arungala 05-02-2012 09:17 AM

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

acid_kewpie 05-02-2012 09:24 AM

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.

dugan 05-02-2012 09:34 AM

I recommend you start doing the homework. A better (answerable) question will occur to you once you do.

arungala 05-02-2012 09:37 AM

thank you guys..its ok its apppearing on google..

can anyone tell how is it done using Bash scripting

acid_kewpie 05-02-2012 09:44 AM

it's appearing on google??? huh??

No, we can't. Go learn something for yourself.

arungala 05-02-2012 11:05 AM

i mean its ok if the question appears on google...actually i wasted 4 hrs on it and developed a headache...pls help

suicidaleggroll 05-02-2012 11:10 AM

So what do you have so far, and what is it doing/not doing?

TB0ne 05-02-2012 11:33 AM

Quote:

Originally Posted by arungala (Post 4668615)
i mean its ok if the question appears on google...actually i wasted 4 hrs on it and developed a headache...pls help

Spell out your words. And do you really expect us to believe that you spent 4 hours searching on Google, and did not come across ONE bash scripting tutorial???

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.

arungala 05-02-2012 11:42 AM

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

arungala 05-02-2012 11:51 AM

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

suicidaleggroll 05-02-2012 11:53 AM

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

273 05-02-2012 11:53 AM

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.

arungala 05-02-2012 11:54 AM

its not possible to go through all this in one nite....i have to subit tomo

jkirchner 05-02-2012 11:57 AM

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

dugan 05-02-2012 11:57 AM

Quote:

Originally Posted by arungala (Post 4668649)
its not possible to go through all this in one nite....i have to subit tomo

That's too bad for you.

However, if you post what you came up with in those four hours, we might be able to help with that.

arungala 05-02-2012 11:59 AM

#!/bin/bash
if [ $# -eq 0 ]
then
echo "please enter some arguments"
fi
x=$#

---now how to proceed

arungala 05-02-2012 12:01 PM

#!/bin/bash
if [ $# -eq 0 ]
then
echo "please enter some arguments"
fi
x=$#

---now how to proceed

whizje 05-02-2012 12:26 PM

hint:rev

TB0ne 05-02-2012 12:46 PM

Quote:

Originally Posted by arungala (Post 4668655)
Code:

#!/bin/bash
if [ $# -eq 0 ]
then
echo "please enter some arguments"
fi
x=$#


If it took you FOUR HOURS to do this, you're in trouble. Also, you say you're working for a 'small company', but I cannot think of ANY reason why a 'company' would need a shell script to do what you're asking for. This is homework, so please be honest about it.

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.

whizje 05-02-2012 01:21 PM

Code:

x=$@  # copy parameters to variable x
result=$(echo $x | rev | cut -d ' ' -f 2-)
echo $result ${x##*' '}
# echo $x : send x to standard output
# rev : reverse line
# cut -d ' ' -f 2- : delimiter is space -f 2- is print from field 2 to end
# print $result is reversed line except first field
# ${x##*' '} remove all fields from $x except last and print it.


suicidaleggroll 05-02-2012 01:25 PM

Quote:

Originally Posted by whizje (Post 4668728)
Code:

x=$@  # copy parameters to variable x
result=$(echo $x | rev | cut -d ' ' -f 2-)
echo $result ${x##*' '}


That can't handle double-digit numbers, FYI

Code:

$./test.sh 1 2 13 5 4
5 31 2 1 4


whizje 05-02-2012 01:29 PM

I know. Sed would be more appropriate.

273 05-02-2012 01:29 PM

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.

whizje 05-02-2012 02:10 PM

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
81 7 6 3 182
bash-4.1$


dugan 05-02-2012 02:19 PM

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.

whizje 05-02-2012 02:43 PM

All arguments on one line is trivial
Code:

x=$@
but the
Code:

xargs -n 1
is a nice one !

gajovy 05-02-2012 02:56 PM

Code:


#!/bin/bash

if [ $# -gt 0 ]; then
        TOREV=$( eval echo \$$# $@ )
        echo "$TOREV " | tac -s" "  | cut -d" " -f2-
        exit 0
else
        echo "please enter some args" >> /dev/stderr
        exit 1
fi


dugan 05-02-2012 05:10 PM

Could you please edit the thread title to be more descriptive and appropriate to your problem? Say, "bash: sorting the arguments"?

whizje 05-02-2012 05:12 PM

Your programs gives problems with more then 10 parameters. A little adaptation.

Code:

#!/bin/bash

if [ $# -gt 0 ]; then
        echo "${@:$#} $@ "| tac -s" " | cut -d " "  -f 2-
        exit 0
else
        echo "please enter some args" >> /dev/stderr
        exit 1
fi


dugan 05-02-2012 05:19 PM

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.

whizje 05-02-2012 05:25 PM

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

arungala 05-02-2012 10:51 PM

@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

arungala 05-02-2012 10:54 PM

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

arungala 05-02-2012 10:57 PM

@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

arungala 05-02-2012 11:01 PM

@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

gajovy 05-02-2012 11:25 PM

Quote:

Originally Posted by whizje (Post 4668857)
Your programs gives problems with more then 10 parameters. A little adaptation.

Code:

#!/bin/bash

if [ $# -gt 0 ]; then
        echo "${@:$#} $@ "| tac -s" " | cut -d " "  -f 2-
        exit 0
else
        echo "please enter some args" >> /dev/stderr
        exit 1
fi


Thanks whizje, I've totally forgoten about variable offsets.


_________________________________

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.

arungala 05-02-2012 11:33 PM

i have 3 months..i think i wil learn lot more

whizje 05-03-2012 02:07 AM

A somewhat nicer output
Code:

#!/bin/bash
if [ $# -gt 0 ]  # $# is number of parameters if number of parameters is greater then 0 the then part is chosen.
  then
    echo -en "\n ${@:$#} ${@:1:$#-1} " | tac -s" "
# echo output string
# -e see \n as a newline
# -n delete newlines in string
# "\n is a newline
# ${@:$#} string is the $# (number of arguments) of $@ (arguments) As number of arguments is 3 and the arguments are 1 2 34 string becomes 34
# ${@:1:$#-1} string is argument 1 until number of arguments minus 1 As number of arguments is 3 and the arguments are 1 2 34 string becomes 1 2
# in between \n,${@:$#} and ${@:1:$#-1} are spaces and after ${@:1:$#-1} ia a space
# we now have he string it only has to be reversed string is "\n 34 1 2 "
# | pipe symbol use output previous command (echo -en "\n ${@:$#} ${@:1:$#-1} ") as input for next command (tac -s" ")
# tac -s" " reverse the string use space as word separator "\n 34 1 2 " becomes "2 1 34 \n " \n is not printed but interpreted as a newline.
    exit 0
  else
    echo "please enter some args" >> /dev/stderr
    exit 1
fi


arungala 05-03-2012 02:57 AM

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##*' '}

cbtshare 05-03-2012 03:37 AM

lmaooo @
Quote:

Originally Posted by TB0ne (Post 4668695)
If it took you FOUR HOURS to do this, you're in trouble. Also, you say you're working for a 'small company', but I cannot think of ANY reason why a 'company' would need a shell script to do what you're asking for. This is homework, so please be honest about it.

didn't take 4hrs but this works well :) ...I guess there is a number of ways to do these homework type stuff,just study up on positional parameters and you will be good.

I have the least amount of lines so I win :d :d :d (jk)


Quote:

#!/bin/bash

num=${@:? Please enter some number}

last=${@:$#}
rest=$(echo ${@/$last} | tac -s" ")
echo $(echo $rest $last)

acid_kewpie 05-03-2012 04:38 AM

Why are we bothering to ask people not to help others cheat when they just do it anyway?

arungala 05-03-2012 04:40 AM

no use explaining you guys..its complicated...thank you for helping anyways

acid_kewpie 05-03-2012 04:46 AM

Quote:

Originally Posted by arungala (Post 4669226)
no use explaining you guys..its complicated...thank you for helping anyways

Well I hope that sums things up for everyone who spoonfed someone their answers and has no idea what the code they are going to submit even does... well done.

arungala 05-03-2012 04:48 AM

ok i agree you're the honest god of linux..happy?

whizje 05-03-2012 05:32 AM

Code:

X=$@ # copy parameters to string x
echo $x # output $x
| take output of previous commando as input for next command
rev # reverse string
cut -d ' ' -f 2 #use  space as delimiter start field 2 of string until end
result =$( ) # create string and put result in result .
echo # output strings
$result # inverted string  minus first parameter
${x##*' '} # delete from string  x from beginning (*' ') words followed by space effectively this is all parameters except last


cbtshare 05-03-2012 05:36 AM

Quote:

Originally Posted by acid_kewpie (Post 4669222)
Why are we bothering to ask people not to help others cheat when they just do it anyway?

Yea your right, I wouldn't normally help with home work stuff, but I read through and saw solutions already presented and just decided it'll be nice little exercise to try.

dugan 05-03-2012 09:09 AM

Quote:

Originally Posted by acid_kewpie (Post 4669222)
Why are we bothering to ask people not to help others cheat when they just do it anyway?

Especially when these people knew that they only had to wait two days before posting their own attempts:

Quote:

Originally Posted by arungala (Post 4668649)
its not possible to go through all this in one nite....i have to subit tomo


suicidaleggroll 05-03-2012 10:40 AM

Quote:

Originally Posted by arungala (Post 4669006)
@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

Quote:

Originally Posted by arungala (Post 4669012)
@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

Did you even read my post? That's not my code, I was quoting whizje and pointing out the exact same thing you just said here.

whizje 05-03-2012 11:43 AM

Quote:

Originally Posted by acid_kewpie (Post 4669222)
Why are we bothering to ask people not to help others cheat when they just do it anyway?

Because if you ask something you can also expect a no sometimes, else you don't have to ask. But in general I agree with you.

whizje 05-03-2012 12:41 PM

Quote:

Originally Posted by cbtshare (Post 4669175)

I have the least amount of lines so I win :d :d :d (jk)

It aren't the least amount of lines but it's a elegant solution. And I prefer elegance above shortness.


All times are GMT -5. The time now is 07:41 PM.