-2

I need help with checking a number next to a word from an array, here is the output from the array:

declare -a my_array=([0]="user.name 16" [1]="user.name2 11" [2]="user.name3 7" [3]="user.name 2"

as you can see, after each username there is a number, what cycle can be used to check exactly (of course, in each element of the array, there can be either 1 or 100) this number (less than 20) and write the username and the original number in the output?

Alecso
  • 1
  • 3
  • What do you want to "check" exactly? What condition do you want to check? You want to check if the number exists? You want to check if array values conform to some particular format or requirement? – KamilCuk Jul 09 '22 at 17:16
  • This question is unclear. What does "there can be either 1 or 100" mean – glenn jackman Jul 09 '22 at 17:18
  • 1
    SO is not a code writing service; you're expected to show your effort (eg, research, code); consider reviewing [how do I ask a good question](https://stackoverflow.com/help/how-to-ask) and then come back and update the question accordingly; in particular, show the code you've tried, the (wrong) output generated by your code and the (correct) expected output – markp-fuso Jul 09 '22 at 17:28
  • @KamilCuk i want to check if the value (less than 20) corresponds to the number after the names in the array – Alecso Jul 09 '22 at 19:22
  • @glennjackman sorry for the inaccuracy, from 1 to 100 I meant the number of users and their values ​​in the output – Alecso Jul 09 '22 at 19:23
  • `if the value (less than 20) corresponds to the number after the names in the array` I do not understand, what value? In what way "corresponds" - is equal to? Which two values should correspond? Array index with the number in the array value? Is the "less than 20" a requirement on the value that you want to check, or is it a pre-requirement that you for sure know no value will be greater than 20? – KamilCuk Jul 09 '22 at 19:25
  • @KamilCuk look, let’s say the output is user.name1 30, 30>20, it’s not suitable for my task and I want to send a notification, but let’s say user.name2 15, 15<20, it suits me, we don’t do anything with it – Alecso Jul 09 '22 at 19:34

3 Answers3

3

From comments posted by OP in response to @KamilCuk, looks like the request is to loop over the element (each is "name NN") and select all the items where NN < 20

Solution is to iterate over the elements, extract the NN from each item, and perform an action based on the NN value. See below.

declare -a my_array=([0]="user.name 16" [1]="user.name2 11"
      [2]="user.name3 7" [3]="user.name 2" [4]="u4 30")

for entry in "${my_array[@]}" ; do
    count=${entry##* }
    if [[ "$count" -lt 20 ]] ; then
        # send-notification
        echo "$entry"
    fi
done
dash-o
  • 13,723
  • 1
  • 10
  • 37
0

You can cut the string based on " " and get the number field. For instance:

for I in "${my_array[@]}"; do

  SOME_VAR=$(echo $I | cut -d " " -f 2)
  ...
  <do something with SOME_VAR>
  ...

  # Write username and number to the output
  echo $I
done

This will produce the desired output, by choosing the 2nd field by cutting the string on space ' '.

zer0
  • 2,153
  • 10
  • 12
  • 3
    I recommend you get out of the habit of using ALLCAPS varnames: you're overwriting the USER environment variable. One day you'll write `PATH=something` and then [wonder why](https://stackoverflow.com/q/27555060/7552) your [script is broken](https://stackoverflow.com/q/28310594/7552). – glenn jackman Jul 09 '22 at 17:23
  • Ah, good catch :) I'll fix. Thanks. – zer0 Jul 09 '22 at 17:28
  • Changing `USER` to `I` is missing the point - don't use all caps for non-exported variable names. See https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization. Also, quote your variables, see https://mywiki.wooledge.org/Quotes. – Ed Morton Jul 10 '22 at 13:13
0

Putting on my mind-reading hat: you can do

for user in "${my_array[@]}"; do
    IFS=" " read -r name number <<< "$user"
    # do something with $name and $number
glenn jackman
  • 238,783
  • 38
  • 220
  • 352