Bash scripting: find variable with lowest value - bash

I have script containin 4 variables. Need a function that will take values of those 4 variables and return name of the one that has lowest value.
So let's say that i have:
var1=55
var2=71
var3=30
var4=42
then i would like it to return as an answer: var3
Can anyone help? I need easiest way to solve that but appreciate any working solution.

From your question I don't think your values are in an array and that you want to know which index in the array is smallest - I think your values are in individual variables and you want to know the name of the variable containing the smallest value. If that is really what you are asking try this:
func()
{
minvar=$1
eval minval=\$$1
for i in $*
do
eval var=\$$i
# echo $i=$var
if [[ $var -lt $minval ]]
then
minvar=$i
minval=$var
# echo min=$i
fi
done
}
var1=55
var2=71
var3=30
var4=42
func var1 var2 var3 var4
echo $minvar=$minval

Related

zsh: return associative array from function

How to return assciative arrays from zsh function?
I tried:
creatAARR() {
declare -A AARR=([k1]=2 [k2]=4)
return $AARR
}
creatAARR
But i get error:
creatAARR:return:2: too many arguments
What is the right way?
EDIT:
I captured output to standard output, like how #chepner suggests, but the new variable doesn't seem to behave like an associative array:
creatAARR() {
declare -A AARR=([k1]=2 [k2]=4)
echo "$AARR"
}
declare -A VALL
NEW_ARR=$(creatAARR)
echo "$NEW_ARR" # 2 4
echo "k1: $NEW_ARR[k1]" # prints just k1:
return
Any suggestions?
return accepts only an integer and sets the exit status of the function.
Shell commands cannot actually return values. If you want pass information to the caller of your function, you have a couple of options available to you:
You could print your return value, but this then relies on you to properly format your output and for the caller to correctly parse it. For associative arrays, there are so many ways that this can go wrong; I wouldn’t recommend doing this.
In Zsh, there is a convention that, to communicate a return value, a function can set $REPLY to a scalar value or $reply to an array. Unfortunately, there is no convention for passing associative arrays. You could, of course, put your key-value pairs simply as elements in the non-associative array $reply and then let the caller cast it to or wrap it in an associative array, but this would break the convention and thus might violate your caller's expectations.
The, in my opinion, best approach is to let the caller specify the name of an associative array, which you can then populate with values. This is also handy when you want to return multiple values of any type, since you can let the caller specify multiple variable names.
This last approach you can use as follows:
% creatAARR() {
# Restrict $name to function scope.
local name=$1
# Delete $1, so $# becomes the other args.
shift
# Assign elements to array.
set -A "$name" "$#"
}
% typeset -A AARR=() # Declare assoc. array
% creatAARR AARR k1 2 k2 4
% typeset -p1 AARR # Print details
typeset -A AARR=(
[k1]=2
[k2]=4
)
I've been able to get my requirement with this:
declare -A VAR1
declare -A VAR2
TYPE_VAR1="TYPE_VAR1"
TYPE_VAR2="TYPE_VAR2"
creatAARR() {
declare -A AARR=([k1]=2 [k2]=4)
case $1 in
"$TYPE_VAR1")
set -A VAR1 ${(kv)AARR}
;;
"$TYPE_VAR2")
set -A VAR2 ${(kv)AARR}
;;
esac
}
creatAARR $TYPE_VAR1 $VAR1
echo "${(kv)VAR1}"
return
There may be a better way, but this is what works for me now.
Please feel free to add in your methods.

How can I create an array whose name includes a variable?

I'm having some trouble writing a command that includes a String of a variable in Bash and wanted to know the correct way to do it.
I want to try and fill the Row arrays with the numbers 1-9 but I'm getting myself stuck when trying to pass a variable Row$Line[$i]=$i.
Row0=()
Row1=()
Row2=()
FillArrays() {
for Line in $(seq 0 2)
do
for i in $(seq 1 9)
do
Row$Line[$i]=$i
done
done
}
I can get the desired result if I echo the command but I assume that is just because it is a String.
I want the for loop to select each row and add the numbers 1-9 in each array.
FillArrays() {
for ((Line=0; Line<8; Line++)); do
declare -g -a "Row$Line" # Ensure that RowN exists as an array
declare -n currRow="Row$Line" # make currRow an alias for that array
for ((i=0; i<9; i++)); do # perform our inner loop...
currRow+=( "$i" ) # ...and populate the target array...
done
unset -n currRow # then clear the alias so it can be reassigned later.
done
}
References:
https://wiki.bash-hackers.org/syntax/ccmd/c_for describes the C-style for loop in bash
BashFAQ #6 discusses indirect reference and assignment in detail, including techniques that precede namevars.
Variable expansion happens too late for an assignment to understand it. You can delay the assignment by using the declare builtin. -g is needed in a function to make the variable global.
Also, you probably don't want to use $Line as the array index, but $i, otherwise you wouldn't populate each line array with numbers 1..9.
#! /bin/bash
Row0=()
Row1=()
Row2=()
FillArrays() {
for Line in $(seq 0 8)
do
for i in $(seq 1 9)
do
declare -g Row$Line[$i]=$i
done
done
}
FillArrays
echo "${Row1[#]}"
But note that using variables as parts of variable names is dangerous. For me, needing this always means I need to switch from the shell to a real programming language.

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

copy the value of the variable rather than reference bash script

I am a bit new to the bash scripting. So please bear with me. I am trying to create a table and assign the values in for loop like this:
packages=("foo" "bar" "foobar")
packageMap=()
function test() {
i=0;
for package in "${packages[#]}"
do
echo $i
packageMap[$package]=$i
i=$(expr $i + 1)
done
}
test
echo the first value is ${packageMap["foo"]}
The output for this is:
0
1
2
the first value is 2
While my expected output is:
0
1
2
the first value is 0
So basically the variable's reference is being assigned to this rather than the value. How to solve this?
My bash version:
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
TIA
bash 3.2 only has indexed arrays, so packageMap[$package] only works as intended if $package is an integer, not an arbitrary string.
(What you are observing is $package being evaluated in an arithmetic context, where foo, bar, and foobar are recursively expanded until you get an integer value. Undefined variables expand to 0, so packageMap[foo] is equivalent to packageMap[0].)
If you were using bash 4 or later, you could use an associative array:
packages=("foo" "bar" "foobar")
declare -A packageMap
test () {
i=0
for package in "${packages[#]}"
do
echo $i
packageMap[$package]=$i
i=$(($i + 1))
done
}
Given that i is the same as the index of each element of packages, you could also write
test () {
for i in "${!packages[#]}"; do
package=${packages[i]}
packageMap[$package]=$i
done
}
instead of explicitly incrementing i.
As chep says, there are no associative arrays in bash 3. That said, if you don't mind wasting a bit of CPU, you can use functions to similar effect:
#!/bin/bash
packages=("foo" "bar" "foobar")
function packagemap () {
local i
for i in "${!packages[#]}"; do
[[ ${packages[$i]} = $1 ]] && echo "$i" && return
done
echo "unknown"
}
echo "the first value is $(packagemap "foo")"
The ${!array[#]} construct expands to the set of indices for the array, which for a normally non-associative array consist of incrementing integers starting at 0. But array members can be removed without the indices being renumbered (i.e. unset packages[1]), so it's important to be able to refer to actual indices rather than assuming they're sequential with for loop that simply counts.
And I note that you're using Darwin. Remember that you really need it, you can install bash 4 using Homebrew or MacPorts.

Getting variable values from variable names listed in array in Bash

I'm trying to print values of multiple variables that are listed in a Bash array as evident in the minimal code example below.
#!/bin/bash
VAR1="/path/to/source/root"
VAR2="/path/to/target/root"
VAR3="50"
VARS=("VAR1" "VAR2" "VAR3")
for var in ${VARS[*]}; do
echo "value of $var is ${$var}"
done
This gives me an error
line 8: value of $var is ${$var}: bad substitution
I want the following output:
value of VAR1 is /path/to/source/root
value of VAR2 is /path/to/target/root
value of VAR3 is 50
My search on Google and SO was not very fruitful. Because of the indirection (i.e., var iterates over an array containing names of variables for which I want the values), I'm not able to precisely word my search. But any help is appreciated.
Use indirect reference as:
#!/bin/bash
VAR1="/path/to/source/root"
VAR2="/path/to/target/root"
VAR3="50"
VARS=("VAR1" "VAR2" "VAR3")
for var in ${VARS[*]}; do
echo "value of $var is ${!var}"
done
Output:
value of VAR1 is /path/to/source/root
value of VAR2 is /path/to/target/root
value of VAR3 is 50

Resources