MyTetra Share
Делитесь знаниями!
О, смотри-ка какое хорошее место. Дайте два!
Check if an array contains a value
21.08.2015
11:11
Раздел: root - Linux - Console - bash

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.35
Яндекс индекс цитирования