How to achieve the variable value declared in two level - shell

I have a Unix variable like below:
emp_tbl=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28
Now I have created another variable like below:
tablename=emp_tbl
Now I want to see the value 1,2,3,... using $($tablename) but I am getting error in it:
~$>emp_tbl=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28
~$>tablename=emp_tbl
~$>echo $($tablename)
-bash: emp_tbl: command not found

You need indirection:
echo ${!tablename}
Read the documentation on shell parameter expansion yet again (reminder to self: do thou likewise).
Your attempt using $($tablename) fails because the $(...) notation is command substitution, and the value in $tablename is interpreted as the command name and the command with the name emp_tbl could not be found.

Related

How to avoid "command not found" in a bash parameter expansion?

I wrote the following bash script:
${MY_FLAG:=true}
${LOG_FILE:="something.log"}
I am trying to assign true to MY_FLAG and the string "something.log" to LOG_FILE. I use parameter expansions because I want to set these variables only if they were not set already.
The problem is that MY_FILE becomes true but LOG_FILE throws an error:
script.sh: line 2: something.log: command not found
I could not find a way to assign the string as is, I tried with different options, simple quotes, and echoing it but nothing did the trick for me.
The parameters will always expand to a value, so you'll have to use them in a context where such an argument is ignored. Conveniently, : aka true does this:
: "${LOG_FILE:="something.log"}"
It only happens to work for your ${MY_FLAG:=true} because true (as discussed) is a valid command. If you run the script with MY_FLAG=date ./yourscript then you'll see that it actually runs date instead of just assigning a default.

Expanding variables before bash call/exec

I know the basics of Bash but often miss the nuance and I'm having a problem using it to achieve what I had hoped would be a rather simple problem:
If I have the following in a bash script, which works exactly as I'd want it to:
cbType=`echo $configuration | jsawk -a 'return _.where(this,{name: "reference_data"})'`
It takes $configuration -- which is a JSON string -- and identifies the array element where name is "reference_data" and returns that object/hash definition only. Please note that this does use the very handy jsawk utility but it has been designed to be exhibit good command-line behaviour.
The problem is that when I remove the hard-coded "reference-data" with a variable it seems to not be able to reference the scope of the variable. So for instance, ...
myVar="\"reference_data\""
cbType=`echo $configuration | jsawk -a 'return _.where(this,{name: $myVar})'`
Does not work and instead returns a jsawk error of:
jsawk: js error: ReferenceError: $myVar is not defined
Is there anything I can do to enforce that first the variable is expanded, and then the command string is executed?
Declared variables won't be expanded if it's not within double quotes. So put your code inside double quotes instead of single quotes.
myVar="\"reference_data\""
cbType=$(echo "$configuration" | jsawk -a "return _.where(this,{name: $myVar})")

Variation on a Variable Variable in Bash

I am looking for a way to add a string to a variable name in bash and evaluate this as a new variable. Example:
#!/bin/bash
file="filename"
declare ${file}_info="about a file"
declare ${file}_status="status of file"
declare ${file}_number="29083451"
echo ${${file}_info} # <-- This fails with error: "bad substitution"
Is there a way to do this?
I'm not actually implementing this in any production code anywhere. I just want to know if there is some way to create a variable name using a variable and a string. It seemed like an interesting problem.
Also note that I am not asking about bash indirection, which is the use of ${!x} to evaluate the contents of a variable as a variable.
You aren't asking about indirection, but that's what can help you:
info=$file\_info
echo ${!info}

Substitute a bash script variable twice

I would like to know if I can substitute a variable twice.
For example:
#global variable
TEST_SERV_EXT=""
#variables become from myconf.sh
TEST_SERV_EXT_FO='foo01'
TEST_SERV_EXT_BR='bar01'
I want dynamically construct those last two and assign them in TEST_SERV_EXT.
I tried something like this ${$TEST_SERV_COMP} but I'm getting "bad substitution" message.
I need something like php's feature "$$" or tcl's subst command.
Regards,
thandem
TEST_SERV_COMP=TEST_SERV_EXT_FO
TEST_SERV_EXT=${!TEST_SERV_COMP}
Look for indirect expansion in the bash manual.

Strange shell behaviour

Here is a simple bash script:
a="asd"
b="qf"
echo "$a.$b"
echo "$a_$b"
It's output is:
asd.qf
qf
Why the second line is not "asd_qf" but "qf"?
Because you haven't defined a variable named a_. For that second printout to work, use:
echo "${a}_$b"
Your second echo displays the value of variable $a_ which is unset.
Use echo "${a}_$b"
The shell has rules about what can go in a variable name, and $a_ is interpreted as the variable named a_ (there is no variable with that name so its value is empty).
You can always add braces to be explicit. In this case, ${a}_$b will clearly identify what the variable name is and the result will be what you expect.

Resources