1

I have the following

#!/bin/bash
aprograms=`pgrep a`
echo $aprograms

which outputs:

alejandro@ubuntu:~$ bash test.sh 
2 6 7 8 12 13 16 17 20 27 ...

I want to control if there is a value inside $aprograms. I tryed the following (Which I dont know if its even a valid approach):

if [ $value in $aprograms ];then
    echo "found"

But doesnt work. Is there a correct way to control if there is a value is inside $aprograms?

5 Answers 5

1

you could use bash's parameter expansion.

shopt -s extglob
var="2 6 7 8 12 13 16 17 20 27"
if [ "${var/17?( )/}" != "$var" ] ; then echo "match"; fi
Sign up to request clarification or add additional context in comments.

3 Comments

That would also detect 17 in a string 1177
Then it would not detect 17 if it happens to be the last or the first value in that string. :) But I like the direction in which your answer is going.
Good find! Edited answer. You should be in QA.
1
#!/bin/bash

function foo () {
  echo 2 6 7 8 12 13 16 17 20 27
}

function search_value () { 
  for i in $2; do {
    if [ $i -eq $1 ]; then {
      echo "found"
    } fi
  } done
}

search_value 13 "$( foo )"

Just change foo with your input program:

search_value 13 "$( pgrep a )"    

3 Comments

It's worth remembering that this solution works only for string containing integer values. search_value a "a b c" will throw an error.
That's true, reading question example I've guessed he is working with integers. Some " around variables and using == as operator instead of -eq should work even for strings.
Yeah. Also I was a little shocked that apparently bash is immune to semicolon injection when I tried to serve it something like search_value 12 "1 ; do; end ; ls ;" :)
1
if [[ " $aprograms " =~ \ $value\  ]] ; then
    echo "found"
fi

Also, with grep

if pgrep a | grep -q -w "$value" ; then
    echo "found:
fi

Comments

0
-eq

is equal to

if [ "$a" -eq "$b" ]

3 Comments

But the question was about finding an element in something what looks like an array, not about comparing values.
see if this will helping , bash grep results into array
the solution will a combination of all of this
0

Since pgrep outputs PIDs one per line, you can use grep to find the one you're looking for.

#!/bin/bash
aprograms=$(pgrep a)
echo "$aprograms"
if grep -Fxqs "$value" <<< "$aprograms"
then
    echo "found"
fi

Always quote variables when they are expanded in order to preserve whitespace.

The -F option to grep uses the pattern as a fixed string instead of a regex. The -x option matches the whole line so smaller strings or numbers don't match parts of larger ones. The options -q and -s suppress output and error output. If your version of grep doesn't have these, then use:

if grep -Fx "$value" <<< "$aprograms" > /dev/null >&2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.