Dynamic variables with two variables - linux

I have a bash scripting question. I am trying to create the following code, however, it does not work properly. This leads me to wonder if it is even possible. I hope someone on here can figure it out.
MYSQL_DB_MyDatabase_USERS_username_PASSWORD=Password
DATABASE_NAME=MyDatabase
DATABASE_USER=username
DATABASE_PASS_TEMP=MYSQL_DB_${DATABASE_NAME}_USERS_${DATABASE_USER}_PASSWORD
echo $DATABASE_PASS_TEMP
I would want the output to be "Password" (the original variable). I am getting an error and I know I have incorrect syntax. I have googled around and found that using one variable in the variable name, one would use echo "${!DATABASE_PASS_TEMP}", but that does not work when using two dynamic variables. Thanks in advance for any help you can give.

Use BASH's variable indirection:
echo "${!DATABASE_PASS_TEMP}"
Password

Using an associative array would tidy things up some:
declare -A MYSQL_DB_PASSWORDS=(
[MyDatabase,username]=Password
)
DATABASE_NAME=MyDatabase
DATABASE_USER=username
DATABASE_PASS_TEMP=${MYSQL_DB_PASSWORDS["$DATABASE_NAME","$DATABASE_USER"]}
echo "$DATABASE_PASS_TEMP"
Password

Related

What is the Lua "replacement" for the pre_exec command in Conky files?

I'm not great at programming, but I was trying to fiddle around with a conky_rc file I liked that I found that seemed pretty straight-forward.
As the title states, I have now learned that the previous command of pre_exec has been long removed and superseded by Lua.
Unfortunately, I cannot seem to find anything directly related to this other than https://github.com/brndnmtthws/conky/issues/62. The thread https://github.com/brndnmtthws/conky/issues/146 references it, and its "solution" states: Basically, there is no replacement and you should use Lua or use a very large interval and execi.
I have found a few more threads that all include the question as to why this function was discontinued, but with no actual answers. So, to reiterate mine, I have absolutely no knowledge of Lua (I've heard of it before, and I've now added a few websites to look at tomorrow since I have spent most of the evening trying to figure out this Conky thing), and I'll probably just give up and do the execi option (my computer can handle it but, I just think it's so horribly inefficient).
Is there an appropriate Lua option? If so, would someone please direct me to either the manual or wiki for it, or explain it? Or is the "proper" Lua solution this?
#Vincent-C It's not working for your script is because the function
ain't getting call. from the quick few tests I did, it seem
lua_startup_hook need the function to be in another file that is
loaded using lua_load, not really sure how the hook function thingy
all works cause I rather just directly use the config as lua since it
is lua.
Basically just call the io.popen stuff and concat it into conky.text
conky.text = [[ a lot of stuff... ${color green} ]];
o = io.popen('fortune -s | cowsay', 'r') conky.text = conky.text ..
o:read('*a')
The comment by asl97 on the first page you cited appears to provide an answer, but a bit of explanation would probably help.
asl97 provides the following general purpose Lua function to use as a substitute for $pre_exec, preceded by a require statement to make io available for use by the function:
require 'io'
function pre_exec(cmd)
local handle = io.popen(cmd)
local output = handle:read("*a")
handle:close()
return output
end
Adding this block of code to your conky configuration file will make the function available for use therein. For testing, I added it above the conky.config = { ... } section.
Calling the Lua pre_exec function will return a string containing the output of the command passed to it. The conky.text section from [[ to ]] is also a string, so it can then be conactenated to the string returned by pre_exec using the .. operator, as shown in the usage section provided by asl97.
In my test, I did the following silly bit, which worked as expected, to display "Hello World!" and the output of the date function with spacing above and below each at the top of my conky display:
conky.text = pre_exec("echo; echo Hello World!; echo; date; echo")..[[
-- lots of boring conky stuff --
]]
More serious commands can, of course, be used with pre_exec, as shown by asl97.
One thing that asl97 didn't explain was how to provide how to concatenate so that the pre_exec output is in the middle of the conky display rather than just the beginning. I tested and found that you can do it like the following:
conky.text = [[
-- some conky stuff --
]]..pre_exec("your_important_command")..[[
-- more conky stuff --
]]

Variables assignment better approach

I wrote a shell script(beginner), which works fine but it includes a number of parameters.
I assign the value to them as show below.
url=$2
name=$3
ipadd=$5
netmask=$6
vlanid=$4
vlname=$7
Is there is any better approach, I can use ?
Thanks.
You can use read instead of multiple assignments:
f=$'\6' # or any other control character
IFS=$f read -d'' -r _ url name vlanid ipadd netmask vlname _ < <(printf "%s$f" "$#")
_ will ignore $1 and anything after $8.
The only way I would see really doing a better job would be to change to a --flag=value setup, if only to not make the order of arguments as important.
./myscript.sh --url=http://www.example.com --ip=10.42.56.23 --netmask=24
This would then require parsing each argument for the --flag part, then if it is found splitting the variable at the = and setting the value of your real internal value. Worth it for something you are shipping out to users, but maybe not so much for something you are using for yourself.

Unbound variable during url printing in bash/shell

I have 2 variables that I want to use to derive a 3rd variable:
export REGION_NAME=phx
export phx_url=https://www.google.com
I am trying to do the following:
echo "$((${REGION_NAME}_url))"
And I get the following error:
-sh: https://www.google.com: syntax error in expression (error token is "://www.google.com")
All I am trying to do is to derive an environment variable from an other one but it does not work simple like that. I think it has to be escaped and could not find anything online.
Thanks in advance for the help.
$((...)) is arithmetic expansion. You didn't mean that. Try normal variable expansion (with indirection) instead.
REGION_NAME=phx
phx_url=https://www.google.com
R_VAR=${REGION_NAME}_url
echo "${!R_VAR}"
One possible solution is using eval like:
var="phx"
eval "${var}_url='some'"
echo $phx_url #prints "some"
But, I not recommending this (because the eval could be pretty dangerous).
Instead of use associative arrays (aka hash variable), like:
declare -A urls
var="phx"
urls[$var]="some2"
echo "${urls[phx]}" #prints "some2"

BASH variable values as new defined variables

I don't know exactly how to ask this in English, but I want to have the value of a variable as a new variable...
The script also has a loop with increasing numbers, and in the end I want to have the variables VAR1, VAR2 etc.
I'm trying this:
COUNT=$(echo 1)
DEFINE=$(echo VAR$COUNT)
$DEFINE=$(echo gotcha!)
When I try this way, I have this error message:
~/script.sh: line n: VAR1=gotcha!: command not found
I played a bit around with brackets and quotation marks, but it didn't work... any solutions?
The problem is that bash expects a command as a result of expansions, not an assignment. VAR1=gotcha! is not a command, hence the error.
It would be better to use an array:
COUNT=$(echo 1)
VAR[COUNT]='gotcha!'
echo ${VAR[COUNT]}
I guess $(echo 1) stands for a more complex command, otherwise you can just use COUNT=1.
You can use declare to create such a "dynamic" variable, but using an array is probably a better choice.
COUNT=1
DEFINE="VAR$COUNT"
declare "$DEFINE=gotcha"

How to construct a comma-separated string in Bourne shell?

I'm not sure how to do this but I figured I would ask here.. I'm trying to create a string of specific environment variables such that:
$A = "foo"
$B = "bar"
$C = "baz"
would give "foo, bar, baz"
Unfortunately, it doesn't seem that the Bourne shell supports arrays, which would have made these easily solvable. The other way I'm trying to solve this is by directly inserting my own variable called $COMMA after each environment variable, however I am getting syntax errors so I'm not sure how to do this correctly. Would appreciate any advice here, thanks!
Your variables shouldn't start with $ unless you want the value of them (this isn't perl or php...)
A=foo
B=bar
C=baz
echo $A,$B,$C
or even:
A=foo B=bar C=baz echo $A,$B,$C
will give you a comma seperated list of the variables you defined.

Resources