OK I don't really have a good title yet but I figure I can post works in progress and other tips I've come across or other interesting things.
having fun with bash varaibles: indirect variable references
Well I recently started messing around with bash scripts as cgi scripts and was wondering how to pass variables in and what not.
Well it turns out, Apache will define the variable $QUERY_STRING which will contain everything after the first question mark. You can then pars this string to define variables. The general format is "name1=val1&name2=val2". It turns out you can actually declare variables in bash using 'declare' and parameter expansion.
unfortunately, I couldn't find a very intuitive way to do the opposite. That is, get the value of a variable name if the name is given as text... But this seemd to do the job well enough.
In that example, $VAR is expanded to "hello" then eval will execute echo as if you had actually typed "echo ${hello}" on the console! if you remove the 'echo' it will be as if you typed "`$hello`" and in this case say "bash: how: command not found" which may or may not be your intention.
Anyways... I'm cleaning up a sample cgi script that utilizes this and will upload it when it's done.
Well it turns out, Apache will define the variable $QUERY_STRING which will contain everything after the first question mark. You can then pars this string to define variables. The general format is "name1=val1&name2=val2". It turns out you can actually declare variables in bash using 'declare' and parameter expansion.
Code:
VAR="hello" VAL="how are you" declare $VAR="$VAL" echo $hello # >> how are you
Code:
eval echo '${'$VAR'}' # >> how are you
Anyways... I'm cleaning up a sample cgi script that utilizes this and will upload it when it's done.
Total Comments 2
Comments
-
Not sure I understand everything you are trying to do but this may interest you:
Code:$ GREETING=HELLO $ HELLO=howdy $ echo ${!GREETING} howdy
Posted 11-29-2009 at 08:16 PM by lwasserm -
How did I miss that one?!
Yeah, that's somewhat of what I was looking for. Would have helped to read the bash manual more closely >.o
Oddly enough, Linux in a nutshell 5th edition didn't explain the ${!name[@]} and neither explains just ${!name}.
Either way, I completely forgot the usefulness of what I was trying to do with it... something along the lines of parsing out the $QUERY_STRING, storing them as bash variables, then later referencing them by a text string.
Anyway, you would still have to do any text processing to "name" first then store it in another variable and reference it as ${!newvar}
e.g.
name="var1 var2"
var1="foo"
var2="bar"
echo ${!${name% var2}}
bash: ${!${name% var2}}: bad substitution
var3=${name% var2}
echo $var3
>> var1
echo ${!var3}
>> foo
BASH MANUAL
Parameter Expansion
...
${!prefix*}
${!prefix@}
Expands to the names of variables whose names begin with prefix, separated by the first character of
the IFS special variable.
${!name[@]}
${!name[*]}
If name is an array variable, expands to the list of array indices (keys) assigned in name. If name
is not an array, expands to 0 if name is set and null otherwise. When @ is used and the expansion
appears within double quotes, each key expands to a separate word.Posted 12-19-2009 at 03:26 AM by lumak