How do I check if an array contains anything else than a specific value in bash? -


arr=( d d d a) 

the code below supposed check if contains d. there similar way check if contains else d?

if [[ " ${arr[*]} " == *" d "* ]];                                           echo "arr contains d"   fi 

as long you're not using space in array elements can use:

[[ " ${arr[*]} " == *" "[^d]" "* ]] && echo "array has non-d element" || echo "no" 

testing:

arr=(d d d a) [[ " ${arr[*]} " == *" "[^d]" "* ]] && echo "array has non-d element" || echo "no" array has non-d element  arr=(d d d) [[ " ${arr[*]} " == *" "[^d]" "* ]] && echo "array has non-d element" || echo "no" no 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -