I have an array
declare -a fruits=("apple" "banana" "guava" "cherry" "mango" "litchi")
I need to write a shell script, it can take multiple arguments (comma separated). It should check each argument against the array- fruits in case argument doesn't match it should exit stating argument is not in array. It should return all the invalid arguments
Eg ./dummy.sh carrot,potato,cabbage
o/p: carrot,potato,cabbage not found in array
eg 2 ./dummy.sh banana,mango o/p banana,mango found in list
#!/usr/bin/bash
declare -a fruits=("apple" "banana" "guava" "cherry" "mango" "litchi")
IFS=, read -ra args <<< "$1"
for arg in "${args[@]}"
do
#echo "Searching for $arg"
found=0
for fruit in "${fruits[@]}"
do
if [[ $fruit == $arg ]]; then found=1; break; fi
done
if [[ $found == 0 ]]; then echo "$arg is not found"; break; fi
done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.