Problem with positional variables in BASH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with positional variables in BASH
# 1  
Old 12-02-2011
Problem with positional variables in BASH

Hello, my problem is simple & I searched a lot but I couldn't find anything about it:

Basically I'd like to pass $i to a variable, $i being the positional variable; but it is unknown in the beginning so I can't do it like eg. myvar=$3, it HAS to be the "i"..
First, I tried myvar=$($i) (because $i is the number of i (ex 5), and not the positional variable ($5) so we would need in theory another $ to get to the positional variable), doesn't work
I tried this also, though I'm not sure if it's correct:
Code:
myvar=${!($[2+$a])}

(in my code, i'd like myvar to get the (2+$a) number for the positional variable).

Thanks for any help, good day
Moderator's Comments:
Mod Comment Please try to use code tags!

Last edited by vbe; 12-02-2011 at 10:53 AM..
# 2  
Old 12-02-2011
You could use eval. For example:
Code:
# cat x.sh
#!/bin/ksh
z=1
x=$(( $z + 2 ))
y=$(eval echo \${${x}})
echo $y
# ./x.sh 1111 2222 3333
3333

This User Gave Thanks to CarloM For This Post:
# 3  
Old 12-02-2011
or array (bash example)
Code:
#!/bin/bash
#example usage:  ./thisfile.shl 1 2 3
echo "Parameters are $@"
declare -a parms=("$@")
i=2
echo ${parms[i]}

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 12-02-2011
It's best to avoid eval if at all possible. If you don't consider its implications very carefully, it can be used to inject arbitrary code inside your program.

You can't directly substitute into a variable name like that, no, but if you're willing to do it in two steps, this will work in BASH or new KSH:

Code:
N=$((2+A))
echo "${!N}"

These 2 Users Gave Thanks to Corona688 For This Post:
# 5  
Old 12-02-2011
Thanks for the replies; I'll try the ideas tonight..
With arrays it's doable (I thought of that too), but it complicates the code since I only need to do it for 2 parameters. I didn't know about eval, but in any case Coronas' seems to be the most simple, so is this code correct like this?

Code:
y=$[2+$i]
myval="${!y}"

..or do I need a $ for y? like :
Code:
myval="${!$y}"

# 6  
Old 12-02-2011
For anyone reading Corona's code and not knowing bash's
Code:
${!pattern}

:
${!N} uses the ! pattern matching operator that expands to any variable with the
same name as the value of the variable, in this case N is 2. So, it finds all environment variables with the name "2", or $2.
These 2 Users Gave Thanks to jim mcnamara For This Post:
# 7  
Old 12-02-2011
Quote:
Originally Posted by timmyyyyy
Thanks for the replies; I'll try the ideas tonight..
With arrays it's doable (I thought of that too), but it complicates the code since I only need to do it for 2 parameters. I didn't know about eval, but in any case Coronas' seems to be the most simple, so is this code correct like this?

Code:
y=$[2+$i]
myval="${!y}"

..or do I need a $ for y? like :
Code:
myval="${!$y}"

Neither's correct, actually. The example as I gave it is the version you need to use.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Positional Parameters Arguments/Variables when using dot (.)

Hi, Is there a special positional variables for when using the dot (.)? Scripts are as below: $: head -100 x.ksh /tmp/y.ksh ==> x.ksh <== #!/bin/ksh # . /tmp/y.ksh 1234 abcd echo "yvar1 = $yvar1" echo "yvar2 = $yvar2" ==> /tmp/y.ksh <== #!/bin/ksh (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

Problem with variables and bash script

From the command line: dions-air:scripts dion$ ls -l /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX -rw-r--r-- 1 dion staff 157934 7 Jun 06:55 /Users/dion/Library/Application Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCXworks... (2 Replies)
Discussion started by: dionbl
2 Replies

4. Shell Programming and Scripting

How to call a bash script with positional parameters?

Hi, I have a script which will be executed using the below command, bin/nutch crawl urls -dir /data/test/ bin/nutch - Script file crawl, urls, /data/test/ - Parameters -dir - Option The above script should executed from a shell script named test.sh. I have the below code to execute... (2 Replies)
Discussion started by: vel4ever
2 Replies

5. UNIX for Dummies Questions & Answers

Print Positional variables

Hi, I have to use the vaious kind of filters based on various fields in the input file like - count occurence of cases where "TRK-GRP" = 169 or like "ADDR-DIG" = 80080. I don;t know the positional variable for all below fields. Please help. Input File : +++ BEST 12-05-27 15:06:49 MDI 3478... (2 Replies)
Discussion started by: vanand420
2 Replies

6. Shell Programming and Scripting

Bash Positional Parameters Question

In a Bash script I used getopts command to let a user does something regards to the selected options. The question is: How do you find out what is the name of the file that user inserted in the command line like the following: The good part is this file is always the last argument in the... (2 Replies)
Discussion started by: bashily
2 Replies

7. Shell Programming and Scripting

Passing variables problem - Bash

I have a following problem: #!/bin/bash NUM=`cat accounts | wc -l`; for i in {1..$NUM} do account=`awk "NR==$i" accounts`; echo -e "\nAccount: $account\n"; sudo ./backup_maildir $account; done "accounts" is a file with regular e-mail addresses, one in each line.... (2 Replies)
Discussion started by: bobanpetrovic
2 Replies

8. Shell Programming and Scripting

problem using variables in bash script

I am using variable to give the location of the file I am using but I get error. Here is the code: LogFile=/tmp/log.email echo -e "could not close the service - error number $error \n" > $LogFile well this is not all the code but is enough because the problem start when I try to use the... (3 Replies)
Discussion started by: programAngel
3 Replies

9. Shell Programming and Scripting

Setting "default" positional parameters (in bash)

Hi, I have a script that processes the positional parameters provided on the command line, or - if none are provided - uses some defaults instead. I've currently got it written as follows, which works like a charm, but I was wondering if there is a different/other/better/... way of doing... (2 Replies)
Discussion started by: pvdb
2 Replies

10. Shell Programming and Scripting

AWK: replace single positional character given variables

I already have accomplished this task using sed and arrays, but since I get the variable using awk, I figured I'd ask this question and maybe I can get a cleaner solution using strictly awk.. I just can't quite grasp it in awk. Story: I'm automating the (re)configuration of network interfaces,... (3 Replies)
Discussion started by: System Shock
3 Replies
Login or Register to Ask a Question