LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > Lumak's Guide to Random Things
User Name
Password

Notices


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.
Rate this Entry

having fun with bash varaibles: indirect variable references

Posted 11-28-2009 at 01:38 PM by lumak
Updated 11-28-2009 at 02:59 PM by lumak

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.

Code:
VAR="hello"
VAL="how are you"
declare $VAR="$VAL"

echo $hello
# >> how are you
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.

Code:
eval echo '${'$VAR'}'
# >> how are you
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.
Attached Files
File Type: txt cgi_vars.txt (2.4 KB, 529 views)
Views 15021 Comments 2
« Prev     Main     Next »
Total Comments 2

Comments

  1. Old Comment
    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 lwasserm is offline
  2. Old Comment
    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 lumak is offline
 

  



All times are GMT -5. The time now is 09:56 PM.

Main Menu
Advertisement
Advertisement
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
Open Source Consulting | Domain Registration