You are not logged in.

#1 2011-03-04 08:42:26

jonazdnb
Member
Registered: 2011-02-23
Posts: 7

Bash, merge variable contents to new variable

I'm a real newb at bash, been at this for hours now trying to figure it out, really tired so maybe this is easy to figure out, don't hate on me. smile

tc1="sda"
tc2="sdb"
tc3="sdc"
tc4="sdd"

VOL="${tc$1}"

DEV="/dev/"
TCVOL="$DEV$VOL"
echo "$TCVOL"

The first user input $1 is gonna be a number.
I want that number to be applied to a new var after "tc", that var is then gonna be one of the "tcX" vars.
I then somehow want the contents of the "tcX" var (for example "sda") to be put into the $VOL var.

I've tried like a million different ways to make the $VOL var, but can't figure it out.

My goal is:
If user input is "1", the script should echo "/dev/sda".
If user input is "2", the script should echo "/dev/sdb".
And so on.

Offline

#2 2011-03-04 08:56:41

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash, merge variable contents to new variable

Variable substitution only works on one variable at a time: vscollect=tc$1, and its syntax is ${!vscollect}.

Offline

#3 2011-03-04 09:02:35

jonazdnb
Member
Registered: 2011-02-23
Posts: 7

Re: Bash, merge variable contents to new variable

Thank you so much!
The "!" did the trick!

Last edited by jonazdnb (2011-03-04 09:02:58)

Offline

Board footer

Powered by FluxBB