Results 1 to 10 of 12
How can one access the 'n'th word in the command line argument
ie. let there be an integer stored in a variable called 'n'.
I want to access the 'n'th ...
- 06-21-2007 #1Just Joined!
- Join Date
- Jun 2007
- Location
- India
- Posts
- 16
How to access the last word in the command line argument
How can one access the 'n'th word in the command line argument
ie. let there be an integer stored in a variable called 'n'.
I want to access the 'n'th word in the command line argument
Is there any way to do it??
thanx in advance
- 06-21-2007 #2Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
You can call in a variable by using in a script $1, $2, $3...for passed variables 1,2 and 3 respectively.
Is it that you want to check for the last variable passed or you know the specific count of the final variable/argument?
- 06-21-2007 #3Just Joined!
- Join Date
- Jun 2007
- Location
- India
- Posts
- 16
no i want to know how to access an arbitrary element in the command line arguments.
For eg. Let us say that somehow out of the blues we get an integer which is between 1 & $# let this integer be stored in a variable called 'm'. The task is to access this 'm'th word in the command line.
- 06-21-2007 #4Just Joined!
- Join Date
- Jun 2007
- Location
- India
- Posts
- 16
I dont want to use loops for this purpose.
- 06-21-2007 #5Linux User
- Join Date
- Jun 2007
- Posts
- 318
How about this:
m=3
tmp="wrd=\"\$$m\""
eval $tmp
echo "$wrd"
- 06-21-2007 #6Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
There is a long thread related to this at: Finding the last command line argument (bash) - LinuxQuestions.org
The beginning title was "last argument", but there are many solutions to getting any argument you desire ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 06-22-2007 #7Just Joined!
- Join Date
- Jun 2007
- Location
- India
- Posts
- 16
Thanks a lot fellas for ur help.....
i liked the solution of vsemaska....
- 06-22-2007 #8Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
If you decide to use that style, I suggest you generalize it by adding braces. Otherwise an expression beyond 9 becomes a problem:
Which produces:Code:#!/bin/sh # @(#) s1 Demonstrate value of braces. set -- a1x a2x a3x a4x a5x a6x a7x a8x a9x a10x a11x a12x a13x set -- one two three four five six seven eight nine ten eleven twelve thirteen m=3 tmp="wrd=\"\$$m\"" eval $tmp echo "$wrd" m=13 tmp="wrd=\"\$$m\"" eval $tmp echo "$wrd" tmp="wrd=\"\${$m}\"" eval $tmp echo "$wrd" exit 0
Best wishes ... cheers, drlCode:% ./s1 three one3 thirteen
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 06-24-2007 #9Just Joined!
- Join Date
- Jun 2007
- Location
- India
- Posts
- 16
How to get the 'n'th word in a line
Let us assume that there is a line:
line="He is a very good boy"
Now I wanna get the 'n'th word of this line?
ie. if the value of n=3 then we should get the 3rd word of this line, which is "a"
in this case.
And once again no loops please...
thanx in advance.....
- 06-25-2007 #10Linux Newbie
- Join Date
- Jul 2004
- Posts
- 143
just assign the line to an array and you can retrieve the word whatever you want...
array=(He is a very good boy)
or
line="He is a very good boy"
array=($line)
echo ${array[3]} --> very


Reply With Quote