Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
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.)
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..
@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
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.
Last edited by gajovy; 05-02-2012 at 11:26 PM.
Reason: emoticon instead of text
#!/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
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)
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.