How to use a passed parameter to call its value as a global variable in Bash script? [duplicate] - bash

This question already has an answer here:
Using variable value as variable name in bash
(1 answer)
Closed 2 years ago.
I am trying to write a function which will take 2 parameters. 1st a color name, 2nd text to be printed.
I have also declared variables for colors as global variables. I want to expand the values by using 1st parameter string.
For now I am using switch case which is the worst way to do it I believe.
Thank you in advance
red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'
Print() {
# I want to use 1st parameter to call the variables above
# i.e. if red is passed, then i want value of red which is '\e[1;31m'
printf $((${1}))
printf "$2"
printf $end
}
function call
Print red "string"

You are referring to indirect parameter expansion.
Print() {
printf ${!1}
printf "$2"
printf $end
}
However, a safer way to write this is with
Print() {
printf '%s%s%s' "${!1}" "$2" "$end"
}
This ensures you get the expected output even if one of the two arguments (the second in particular) contains a %.
Depending on how many other contexts use your color variables, I would move the escape handling into Print itself, so that you can simply define red=31, for example.
Print() {
printf '\033[1;%sm%s\033[0m' "${!1}" "$2"
}

You have to make bash interpret a string as a variable with ${!...}.
#!/bin/bash
red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'
Print() {
# I want to use 1st parameter to call the variables above
# i.e. if red is passed, then i want value of red which is '\e[1;31m'
echo -n ${!1}
printf "$2"
printf $end
}
Print "$#"

Try indirect expansion: What is indirect expansion? What does ${!var*} mean?
Here's your function with indirect expansion:
red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'
Print() {
# I want to use 1st parameter to call the variables above
# i.e. if red is passed, then i want value of red which is '\e[1;31m'
echo "${!1}$2$end"
}
Print red hello

Related

give an array to a function, exclamation mark?

I am trying to give an array to a function. I finish to success to arrive to this solution :
test_arr() {
local a="${!1}"
for i in ${a[#]}
do
echo $i
printf '\n'
done
}
arr=("lol 1" "lol 2" "lol 3");
test_arr arr[#]
However there is two issues with that : there is a copy via the local variable. So I would be able to use $1 directly in the for loop, and I do not understand the purpose of ${!1}. What does mean the !?
Another problem is that for my shell, there is 6 elements instead of 3
If you want to just pass values of an array to a function, you can do this:
test_arr() {
for i in "$#"; do
echo $i
printf '\n'
done
:
}
arr=("lol 1" "lol 2" "lol 3")
test_arr "${arr[#]}"
"${arr[#]}" will pass all values properly delimited to the function where we can access them through $# (all arguments).
! you've asked about is used for indirect reference. I.e. "${!1}" is not value of the first argument, but value of the variable whose name is what the value of the first argument was.
I could have missed something, but it seems like wanting to combine indirection and access all items of indirectly referenced array at the same time would be asking a little too much from shell so I've conjured mighty eval (good reason to start being cautious) to help us out a bit. I've hacked this which allows you to pass array name to a function and then access its items based on that name as seen in the first argument of the function, but it's not pretty and that alone should be enough of a discouragement to not do it. It does create a local variable / array as your example assuming there was some reason to want that.
test_arr() {
local a
eval a=(\"\$\{$1\[#\]\}\")
for i in "${a[#]}"; do
echo $i
done
}
arr=("lol 1" "lol 2" "lol 3")
test_arr arr

assigning name to text file in shell script [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
In bash, you can use ${!variable} to use variable variables.
foo="something"
bar="foo"
echo "${!bar}"
# something
eval echo \"\$$bar\" would do it.
The accepted answer is great. However, #Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[#]", so that the array is expanded with the "!". Check out this function to dump variables:
$ function dump_variables() {
for var in "$#"; do
echo "$var=${!var}"
done
}
$ STRING="Hello World"
$ ARRAY=("ab" "cd")
$ dump_variables STRING ARRAY ARRAY[#]
This outputs:
STRING=Hello World
ARRAY=ab
ARRAY[#]=ab cd
When given as just ARRAY, the first element is shown as that's what's expanded by the !. By giving the ARRAY[#] format, you get the array and all its values expanded.
To make it more clear how to do it with arrays:
arr=( 'a' 'b' 'c' )
# construct a var assigning the string representation
# of the variable (array) as its value:
var=arr[#]
echo "${!var}"

How to do double variable substitution inside bash if loop [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
In bash, you can use ${!variable} to use variable variables.
foo="something"
bar="foo"
echo "${!bar}"
# something
eval echo \"\$$bar\" would do it.
The accepted answer is great. However, #Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[#]", so that the array is expanded with the "!". Check out this function to dump variables:
$ function dump_variables() {
for var in "$#"; do
echo "$var=${!var}"
done
}
$ STRING="Hello World"
$ ARRAY=("ab" "cd")
$ dump_variables STRING ARRAY ARRAY[#]
This outputs:
STRING=Hello World
ARRAY=ab
ARRAY[#]=ab cd
When given as just ARRAY, the first element is shown as that's what's expanded by the !. By giving the ARRAY[#] format, you get the array and all its values expanded.
To make it more clear how to do it with arrays:
arr=( 'a' 'b' 'c' )
# construct a var assigning the string representation
# of the variable (array) as its value:
var=arr[#]
echo "${!var}"

bash Script Arguments grouping

I am having problems with expansion of command-line options containing spaces. They are not getting grouped as I expect them to be. How can I modify following code(below) to get the desired output(below).
function myFunction {
while getopts "a:b:A:" optionName; do
echo "$optionName::$OPTARG"
done
}
#dynamic variable, cannot be hardcoded into $MY_ARGS
MY_VAR="X1=162356374 X2=432876 X3=342724"
#$MY_ARGS is useful and will be used more than once,
#so we don't want to eliminate it and replace it's usage with its value everywhere
MY_ARGS="-a 24765437643 -b '$MY_VAR' -A jeeywewueuye"
myFunction $MY_ARGS
Actual Output:
a::24765437643
b::'X1=162356374
Desired Output:
a::24765437643
b::X1=162356374 X2=432876 X3=342724
A::jeeywewueuye
The best way to store a list of arguments is in an array. An array can handle arguments with whitespace without problem, and you don't have to figure out how to get the quotes and backslashes just right.
MY_ARGS=(-a 24765437643 -b "$MY_VAR" -A jeeywewueuye)
myFunction "${MY_ARGS[#]}"
The only unnatural part about arrays is the weird syntax to expand them: "${array[#]}". The quotes, curly braces, and [#] notation are all important.
I agree that arrays answer the question in the best way.
Perhaps you don't want to use arrays (colleagues will not understand), or you must obey the Google Guidelines for bash (nice work, I agree with for over 90%). that claims: "If you find you need to use arrays for anything more than assignment of ${PIPESTATUS}, you should use Python. ".
When you must look for another solutions:
An ugly solution is changing the IFS:
function myFunction {
while getopts "a:b:A:" optionName; do
echo "$optionName::$OPTARG"
done
}
#dynamic variable, cannot be hardcoded into $MY_ARGS
MY_VAR="X1=162356374 X2=432876 X3=342724"
MY_ARGS="-a/24765437643/-b/"$MY_VAR"/-A/jeeywewueuye"
IFS=/
myFunction ${MY_ARGS}
Perhaps you want to do something with myFunction
function myFunction {
while getopts "a:bA:" optionName; do
case "${optionName}" in
b) echo "${optionName}::${MY_VAR}" ;;
*) echo "${optionName}::${OPTARG}" ;;
esac
done
}
or you could tr the spaces into another character before calling myFunction and tr the characters back to spaces in myFunction().

Create associative array in bash 3

After thoroughly searching for a way to create an associative array in bash, I found that declare -A array will do the trick. But the problem is, it is only for bash version 4 and the bash version the server has in our system is 3.2.16.
How can I achieve some sort of associative array-like hack in bash 3? The values will be passed to a script like
ARG=array[key];
./script.sh ${ARG}
EDIT: I know that I can do this in awk, or other tools but strict bash is needed for the scenario I am trying to solve.
Bash 3 has no associative arrays, so you're going to have to use some other language feature(s) for your purpose. Note that even under bash 4, the code you wrote doesn't do what you claim it does: ./script.sh ${ARG} does not pass the associative array to the child script, because ${ARG} expands to nothing when ARG is an associative array. You cannot pass an associative array to a child process, you need to encode it anyway.
You need to define some argument passing protocol between the parent script and the child script. A common one is to pass arguments in the form key=value. This assumes that the character = does not appear in keys.
You also need to figure out how to represent the associative array in the parent script and in the child script. They need not use the same representation.
A common method to represent an associative array is to use separate variables for each element, with a common naming prefix. This requires that the key name only consists of ASCII letters (of either case), digits and underscores. For example, instead of ${myarray[key]}, write ${myarray__key}. If the key is determined at run time, you need a round of expansion first: instead of ${myarray[$key]}, write
n=myarray__${key}; echo ${!n}
For an assignment, use printf -v. Note the %s format to printf to use the specified value. Do not write printf -v "myarray__${key}" %s "$value" since that would treat $value as a format and perform printf % expansion on it.
printf -v "myarray__${key}" %s "$value"
If you need to pass an associative array represented like this to a child process with the key=value argument representation, you can use ${!myarray__*} to enumerate over all the variables whose name begins with myarray__.
args=()
for k in ${!myarray__*}; do
n=$k
args+=("$k=${!n}")
done
In the child process, to convert arguments of the form key=value to separate variables with a prefix:
for x; do
if [[ $x != *=* ]]; then echo 1>&2 "KEY=VALUE expected, but got $x"; exit 120; fi
printf -v "myarray__${x%%=*}" %s "${x#*=}"
done
By the way, are you sure that this is what you need? Instead of calling a bash script from another bash script, you might want to run the child script in a subshell instead. That way it would inherit from all the variables of the parent.
Here is another post/explanation on associative arrays in bash 3 and older using parameter expansion:
https://stackoverflow.com/a/4444841
Gilles' method has a nice if statement to catch delimiter issues, sanitize oddball input ...etc. Use that.
If you are somewhat familiar with parameter expansion:
http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
To use in your scenario [ as stated: sending to script ]:
Script 1:
sending_array.sh
# A pretend Python dictionary with bash 3
ARRAY=( "cow:moo"
"dinosaur:roar"
"bird:chirp"
"bash:rock" )
bash ./receive_arr.sh "${ARRAY[#]}"
Script 2: receive_arr.sh
argAry1=("$#")
function process_arr () {
declare -a hash=("${!1}")
for animal in "${hash[#]}"; do
echo "Key: ${animal%%:*}"
echo "Value: ${animal#*:}"
done
}
process_arr argAry1[#]
exit 0
Method 2, sourcing the second script:
Script 1:
sending_array.sh
source ./receive_arr.sh
# A pretend Python dictionary with bash 3
ARRAY=( "cow:moo"
"dinosaur:roar"
"bird:chirp"
"bash:rock" )
process_arr ARRAY[#]
Script 2: receive_arr.sh
function process_arr () {
declare -a hash=("${!1}")
for animal in "${hash[#]}"; do
echo "Key: ${animal%%:*}"
echo "Value: ${animal#*:}"
done
}
References:
Passing arrays as parameters in bash
If you don't want to handle a lot of variables, or keys are simply invalid variable identifiers, and your array is guaranteed to have less than 256 items, you can abuse function return values. This solution does not require any subshell as the value is readily available as a variable, nor any iteration so that performance screams. Also it's very readable, almost like the Bash 4 version.
Here's the most basic version:
hash_index() {
case $1 in
'foo') return 0;;
'bar') return 1;;
'baz') return 2;;
esac
}
hash_vals=("foo_val"
"bar_val"
"baz_val");
hash_index "foo"
echo ${hash_vals[$?]}
More details and variants in this answer
You can write the key-value pairs to a file and then grep by key. If you use a pattern like
key=value
then you can egrep for ^key= which makes this pretty safe.
To "overwrite" a value, just append the new value at the end of the file and use tail -1 to get just the last result of egrep
Alternatively, you can put this information into a normal array using key=value as value for the array and then iterator over the array to find the value.
This turns out to be ridiculously easy. I had to convert a bash 4 script that used a bunch of associative arrays to bash 3. These two helper functions did it all:
array_exp() {
exp=${#//[/__}
eval "${exp//]}"
}
array_clear() {
unset $(array_exp "echo \${!$1__*}")
}
I'm flabbergasted that this actually works, but that's the beauty of bash.
E.g.
((all[ping_lo] += counts[ping_lo]))
becomes
array_exp '((all[ping_lo] += counts[ping_lo]))'
Or this print statement:
printf "%3d" ${counts[ping_lo]} >> $return
becomes
array_exp 'printf "%3d" ${counts[ping_lo]}' >> $return
The only syntax that changes is clearing. This:
counts=()
becomes
array_clear counts
and you're set. You could easily tell array_exp to recognize expressions like "=()" and handle them by rewriting them as array_clear expressions, but I prefer the simplicity of the above two functions.

Resources