LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-02-2012, 05:25 PM   #31
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141

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

Last edited by whizje; 05-02-2012 at 05:34 PM.
 
1 members found this post helpful.
Old 05-02-2012, 10:51 PM   #32
arungala
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
@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
 
Old 05-02-2012, 10:54 PM   #33
arungala
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
@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..
 
Old 05-02-2012, 10:57 PM   #34
arungala
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
@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

Last edited by arungala; 05-02-2012 at 10:59 PM.
 
Old 05-02-2012, 11:01 PM   #35
arungala
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
@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
 
Old 05-02-2012, 11:25 PM   #36
gajovy
LQ Newbie
 
Registered: May 2012
Posts: 4

Rep: Reputation: Disabled
Quote:
Originally Posted by whizje View Post
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
 
Old 05-02-2012, 11:33 PM   #37
arungala
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
i have 3 months..i think i wil learn lot more
 
Old 05-03-2012, 02:07 AM   #38
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
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
 
1 members found this post helpful.
Old 05-03-2012, 02:57 AM   #39
arungala
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
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##*' '}
 
Old 05-03-2012, 03:37 AM   #40
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
lmaooo @
Quote:
Originally Posted by TB0ne View Post
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)

Last edited by cbtshare; 05-03-2012 at 03:46 AM.
 
Old 05-03-2012, 04:38 AM   #41
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Why are we bothering to ask people not to help others cheat when they just do it anyway?
 
1 members found this post helpful.
Old 05-03-2012, 04:40 AM   #42
arungala
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
no use explaining you guys..its complicated...thank you for helping anyways
 
Old 05-03-2012, 04:46 AM   #43
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by arungala View Post
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.
 
Old 05-03-2012, 04:48 AM   #44
arungala
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
ok i agree you're the honest god of linux..happy?
 
Old 05-03-2012, 05:32 AM   #45
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
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

Last edited by whizje; 05-03-2012 at 05:34 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting question, homework related cybergeek11235 Linux - General 4 10-12-2008 10:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration