Last active
September 6, 2019 04:21
-
-
Save gubatron/86848574b332d0f7ef9cfc805de64b37 to your computer and use it in GitHub Desktop.
bash: contains_item function. Check if an item is in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################# | |
# contains_item ${needle} ${haystack[@]} | |
############################################################################# | |
contains_item() { | |
set +x | |
local ITEM=$1 | |
local LIST=${@:2} | |
for ELEM in ${LIST[@]} | |
do | |
#echo "Scanning for needle:${ITEM}: currently @ ${ELEM}" | |
if [ ${ITEM} == ${ELEM} ]; then | |
#echo "Found ${ITEM} == ${ELEM}" | |
return 0 | |
fi | |
done | |
#echo "Did not find ${ITEM}" | |
return 1 | |
} | |
############################################################################# | |
# | |
# Example: We want to populate --enable-decoder= and --disable-decoder= flags | |
# to invoke a ./configure script, the script gives us the list of all decoders available | |
# we we want to exclude the ones in a given list | |
# | |
# ENABLED_DECODERS=(flv h263 h263i h264 hevc mpeg1video mpeg2video mpeg4 svq1 svq3 vp6 vp8 vp9 wmv1 wmv2 aac ac3 flac mp3 mp3 vorbis wmav1 wmav2 adpcm_g726) | |
# AVAILABLE_DECODERS=`./configure --list-decoders` # We get all the available decoders | |
# | |
# DISABLED_DECODERS_FLAGS="" # We'll store the final decoders to disable here | |
# | |
# for DECODER in ${AVAILABLE_DECODERS} | |
# do | |
# ENCODER_ENABLED=`contains_item ${DECODER} ${ENABLED_DECODERS[@]}` | |
# # if we don't find it in the enabled list, we add it. | |
# # (Yes, in bash 1 stands for false. functions return 0 when everything went fine as in errors) | |
# if [[ ${ENCODER_ENABLED} == 1 ]]; then | |
# echo "Disabling not found decoder: ${DECODER}" | |
# DISABLED_DECODERS_FLAGS+="--disable-decoder=${DECODER} "; | |
# fi | |
# done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment