LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-01-2007, 12:25 AM   #1
R3N3G4D3
LQ Newbie
 
Registered: Apr 2003
Distribution: Ubuntu 7.04, RedHat 4.4
Posts: 22

Rep: Reputation: 15
bash script arguments


I wrote a bash script that takes in an undefined number of arguments. As part of the script, I want to iterate through these variables. The problem is that I need the script to take the argument corresponding to the current iteration value. I tried "echo $$k" where $k is the iterator and received an answer equal to value stored in k, and tried "echo $"$k"" and received $ followed by variable stored in k, I also tried a bunch of other things that returned unsuccessful results, interestingly "echo $$$k" produces a large unexplainable number, a memory pointer???

So for example, say I have the following:
Code:
./script testing 123

#!/bin/bash
#script:
#desired output: testing
k=1
echo $$k #output: 1
echo $"$k" #output: $1
echo \$"$k" #output: $1
I realize I could take $@ and cut it up into separate strings using space as divider, but I really want to know if my original way of doing it is possible? Thanks.
 
Old 11-01-2007, 01:49 AM   #2
student04
Member
 
Registered: Jan 2004
Location: Georgia
Distribution: OS X, CentOS
Posts: 669

Rep: Reputation: 34
Cool question. Here's what I found.

Doing a "shift" in the script will remove the first argument in the argument list until none are left.

Code:
#!/bin/bash

echo "Parameters: ${@}"
echo "P1 = $1"

echo ""
echo "Shifting..."
shift
echo "Parameters: ${@}"
echo "P1 = $1"

echo ""
echo "Shifting..."
shift
echo "Parameters: ${@}"
echo "P1 = $1"
So, you could technically just shift each time you go through and use $1 (if you don't care about accessing the args anymore). Or, do a for loop on $@.

But a direct answer to your question, I'm not sure.

Read a bit in here, maybe it'll say more...

http://www.tldp.org/LDP/abs/html/int...s.html#ARGLIST

-AM
 
Old 11-01-2007, 04:37 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
edit: this isn't what you mean is it?

you aren't being very clear, have you a simple example?



use for
Code:
#!/bin/bash

# "$@" preserves spaces
for thing in "$@"; do
    echo $thing
done
Code:
$ 1.sh hello there old 'fellow me lad'
hello
there
old
fellow me lad
$

Last edited by bigearsbilly; 11-01-2007 at 04:39 AM.
 
Old 11-01-2007, 06:04 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by R3N3G4D3 View Post
but I really want to know if my original way of doing it is possible? Thanks.
Yes. An example is by means of the eval command. You want to interpret $1 where 1 itself is a variable? You simply do
Code:
eval echo \$$k
the first $ sign is escaped to let literal interpretation by eval, therefore the command executed is exactly
Code:
echo $1
for k equal to 1. Another way can be using arrays, if available in your version of bash. Just assign arguments to array's elements and retrieve them with the index k, as in
Code:
#!/bin/bash
args=("$@")
k=2
echo ${args[$k]}
In this case, just take in mind that array elements are zero-based.

Last edited by colucix; 11-01-2007 at 06:17 AM.
 
Old 11-01-2007, 10:40 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Yet another way ("YAW" ?), perhaps the easiest, is indirect expansion:
Code:
for X in "$@"
do
  echo ${!X}   # echos the value of the variable X
done
For more info, search the bash man page for "exclamation point".


BTW & FWIW, combining the 2 techniques will finally give me double indirection:
Code:
$ K=k1 ;  A=K ;  Z=A
$ eval echo \$${!Z}
k1
 
Old 11-01-2007, 11:16 AM   #6
R3N3G4D3
LQ Newbie
 
Registered: Apr 2003
Distribution: Ubuntu 7.04, RedHat 4.4
Posts: 22

Original Poster
Rep: Reputation: 15
Thanks for the replies guys, the "eval echo \$$k" command did exactly what I needed. I'm sure other solutions work too, but that is the easiest modification to my script.

Last edited by R3N3G4D3; 11-01-2007 at 11:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 script - variable arguments indikabandara19 Linux - Newbie 4 05-21-2007 11:40 PM
can awk see bash script arguments ? sharapchi Programming 7 12-14-2006 09:03 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 09:24 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 06:23 PM
bash arguments stuckinhell Programming 6 08-13-2004 06:10 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration