Help for a simple script
#!/bin/bash
array1=(
prova1
prova2
slack64
)
a="slack64"
b="ab"
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
fi
This script simply doesn’t work, I want a script which check if slack64 is present in a list(i use an array),and simply give me, yes is present,or no.
I don’t know how to compare an array with a single variable.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Use a different kind of array: rather than an integer-indexed array, use an associative array, so the key (index) is what you will be checking for. bash-4.0 or later is required for this.
declare -A array1=(
[prova1]=1 [prova2]=1 [slack64]=1
)
a=slack64
[[ -n "${array1[$a]}" ]] && printf '%s is in arrayn' "$a"
In the above we don’t really care about the values, they need only be non-empty for this. You can “invert” an indexed array into a new associative array by exchanging the key and value:
declare -a array1=(
prova1 prova2 slack64
)
declare -A map # required: declare explicit associative array
for key in "${!array1[@]}"; do map[${array1[$key]}]="$key"; done # see below
a=slack64
[[ -n "${map[$a]}" ]] && printf '%s is in arrayn' "$a"
This can pay off if you have large arrays which are frequently searched, since the implementation of associative arrays will perform better than array-traversing loops. It won’t suit every use case though, since it cannot handle duplicates (though you can use the value as a counter, instead of just 1 as above), and it cannot handle an empty index.
Breaking out the complex line above, to explain the “inversion”:
for key in "${!a[@]}" # expand the array indexes to a list of words
do
map[${a[$key]}]="$key" # exchange the value ${a[$key]} with the index $key
done
Method 2
The straightforward way is to iterate with a loop :
var=ab
for item in "${array[@]}"; do
[[ $var == "$item" ]] && echo "$var present in the array"
done
Method 3
How to check if a Bash Array contains a value
False positive match
array=(a1 b1 c1 d1 ee)
[[ ${array[*]} =~ 'a' ]] && echo 'yes' || echo 'no'
# output:
yes
[[ ${array[*]} =~ 'a1' ]] && echo 'yes' || echo 'no'
# output:
yes
[[ ${array[*]} =~ 'e' ]] && echo 'yes' || echo 'no'
# output:
yes
[[ ${array[*]} =~ 'ee' ]] && echo 'yes' || echo 'no'
# output:
yes
Exact match
In order to look for an exact match, your regex pattern needs to add extra space before and after the value like (^|[[:space:]])"VALUE"($|[[:space:]])
# Exact match
array=(aa1 bc1 ac1 ed1 aee)
if [[ ${array[*]} =~ (^|[[:space:]])"a"($|[[:space:]]) ]]; then
echo "Yes";
else
echo "No";
fi
# output:
No
if [[ ${array[*]} =~ (^|[[:space:]])"ac1"($|[[:space:]]) ]]; then
echo "Yes";
else
echo "No";
fi
# output:
Yes
find="ac1"
if [[ ${array[*]} =~ (^|[[:space:]])"$find"($|[[:space:]]) ]]; then
echo "Yes";
else
echo "No";
fi
# output:
Yes
For more usage examples the source of examples are here
Method 4
You can also use grep for that:
array1=(prova1 prova2 slack64)
a=slack64
if (printf '%sn' "${array1[@]}" | grep -xq $a); then
echo "it's in"
fi
Method 5
With zsh:
array1=( prova1 prova2 slack64 ) a=slack64 if (( $array1[(Ie)$a] )); then printf '%sn' "$a in array1" fi
The I array subscript flag is for returning the index of the rightmost element that matches $a (or 0 if not found); with e, it’s an exact / literal match, not pattern matching.
With bash:
Since bash’s array members can’t contain NUL bytes (contrary to zsh’s), if you have GNU grep or compatible you can always do:
if (( ${#array1[@]} )) && printf '%s' "${array1[@]}" | grep -zqxFe "$a"; then
printf '%sn' "$a in array1"
fi
That is, print the elements NUL-delimited and ask grep to quietly find exact Fixed-string matches for the expression stored in $a in those zero-delimited records.
Method 6
This function works with associative arrays.
We can use this function to do one of the following:
— check if the array has a certain key —————–> inArray “myKey” ${!myArray[@]}”
— check if the array contains a certain value ———> inArray “myValue” “${myArray[@]}”
function inArray # ( keyOrValue, arrayKeysOrValues )
{
local e
for e in "${@:2}"; do
[[ "$e" == "$1" ]] && return 0;
done
return 1
}
declare -A arr
arr[doo]=55
inArray "doo" "${!arr[@]}"
echo $? # 0
inArray "bla" "${!arr[@]}"
echo $? # 1
inArray "55" "${arr[@]}"
echo $? # 0
Method 7
You can use “grep”.
_array=(a b c)
_find_me="b"
echo ${_array[@]} | grep "$_find_me" &&
echo "I found: $_find_me" ||
echo "I could not found: $_find_me"
Output:
a b c I found: b
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0