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


All times are GMT -5. The time now is 10:56 AM.