1

couldn't find a specific string on a variable containing file content

#!/bin/bash

core_pattern=$(cat /proc/sys/kernel/core_pattern)
apport_full_path="/usr/share/apport/apport"


if [[ $(grep "$apport_full_path" "$core_pattern") ]] ; then
   echo "Found"
else
   echo "Not Found"
fi


grep: |/usr/share/apport/apport %p %s %c %d %P: No such file or directory
Not Found

I expect an output of "Found" or "Not Found" but ending up with an error

1
  • That really helped. thanks a lot! Commented Jun 13, 2019 at 15:18

1 Answer 1

1

If you want to store a file into a variable and then run grep, this approach is bit redundant:

#!/bin/bash

core_pattern=$(cat /proc/sys/kernel/core_pattern)
apport_full_path="/usr/share/apport/apport"


if  grep -q "$apport_full_path" <<< "$core_pattern"  ; then
   echo "Found"
else
   echo "Not Found"
fi

OR, better, run grep over the file itself, why to store to a variable:

#!/bin/bash

pattern_file="/proc/sys/kernel/core_pattern"
apport_full_path="/usr/share/apport/apport"


if  grep -q "$apport_full_path" "$pattern_file"  ; then
   echo "Found"
else
   echo "Not Found"
fi

Generally grep is used like:

grep <string to search> <file_to_seaarch>

or

grep <string_to_Search> <<< "${variable_to_search}"
Sign up to request clarification or add additional context in comments.

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.