MyTetra Share
Делитесь знаниями!
Check if an array contains a value
Время создания: 21.08.2015 11:11
Раздел: root - Linux - Console - bash
Запись: Yurons/mytetra/master/base/1440144668xem4trs8oc/text.html на raw.github.com

Below is a small function for achieving this. The search string is the first argument and the rest are the array elements:

containsElement () {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
} 

A test run of that function could look like:

$ array=("something to search for" "a string" "test2000")
$ containsElement "a string" "${array[@]}"
$ echo $?
0
$ containsElement "blaha" "${array[@]}"
$ echo $?
1 
 
MyTetra Share v.0.59
Яндекс индекс цитирования