简体   繁体   中英

Compare array elements with an element

I am trying to compare an array with a value and print an error statement if the match is not found.

arraylist="$(ls new-dir/ | cut -d' ' -f1)"

For example, this stores values such as small, large and medium which are the files present in new-dir . The value to be compared with will be entered by the user viz. var

I have tried something like following:

(for i in "${arraylist[@]}"; do [[ "$i" == "$var"]] && exit 0; done) && echo "found" || echo "not found"

Also tried, however, doesn't work:

arraylist="$(ls new-dir/ | cut -d' ' -f1)"
count=0

for((i=0; i<${#arraylist[@]}; i++)); do
  if [ "$arraylist[$i]" == "$var" ] ; then
    count=1
  fi
done

if [ $count -eq 0 ]; then
  echo "Not found"
fi

Is there any other way to do this comparison?

This code works. Just replace arraylist with your array.

arraylist=('hello' 'world')
var='hello'
count=0

for i in "${arraylist[@]}"
do
    if [ "$i" == "$var" ]; then
        count=1
    fi
done

if [ $count -eq 0 ]; then
  echo "Not found"
fi

To extract the first "word" from each filename, I'd do

declare -A prefixes
cd new-dir
for file in *; do
    read -r prefix _ <<< "$file"
    prefixes["$prefix"]=1
done
cd -

then to look for a match:

# $var is somehow provided
result=""
for p in "${!prefixes[@]}"; do
  if [[ $p == "$var" ]]; then
    result=found
    break
  fi
done
echo "${result:-not found}"

This requires bash v4.0+ for the use of an associative array.

If you have bash v4.3+ you can do

[[ -v prefixes[$var] ]] && echo found || echo not found

Alternately:

shopt -s nullglob
files=( new-dir/"$var "* )
(( ${#files[@]} > 0 )) && echo found || echo not found

The arraylist might be an array with just one index .

The command: arraylist="$(ls new-dir/ | cut -d' ' -f1)" might just assign whole "ls new-dir" output to ${arraylist[0]}

You can check the index value in an array by echo ${arraylist[1]} (to confirm).

Try changing the command to generate an array for a list of files/dirs to:

arraylist=($(ls new-dir/))

The complete script:


arraylist=($(ls new-dir/))

(for i in "${arraylist[@]}"; do [[ "$i" == "$var" ]] && exit 0; done) && echo "found" || echo "not found"

You can also ignore the whole array assignment and just use subshell-command in for loop, eg:

(for i in $(ls -1 new-dir/); do [[ "$i" == "$var" ]] && exit 0; done) && echo "found" || echo "not found"

Remember, no quotes " around $(ls -1 new-dir/)


Another one-liner without any array or loops:

ls -1 new-dir/ | grep -q "$var" && echo "found" || echo "not found"

Edit: As @M. Nejat Aydin suggested, use [[ -e new-dir/$var ]] && echo "found" || echo "Not found" [[ -e new-dir/$var ]] && echo "found" || echo "Not found" and do not use ls in the script .

This is what shell programming is about. Keep it simple:

Array1=( "item1" "item2" "item3" "item-4" )
var="item3"

count=$(echo ${Array1[@]} | tr ' ' '\n' | awk '$1 == "'"$var"'"{print $0}' | wc -l)
[ $count -eq 0 ] && echo "Not found" || echo "found"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM