How do I achieve the following in Shell Script (bourne and/or bash)... something similar to what I can, using RegEx on a file (content sample below)
- I need to match the pattern, and then -
- Capture the content to the right side of 'LD_PRELOAD=' into a variable.
File Content:
blah
blah blah
#Some comment
LD_PRELOAD=/usr/lib64/libstdc++.so.6
export LD_PRELOAD
#more comments
The following RegEx works fine on the online Regex Builder (https://regex101.com/r/XW0JVR/1)
/LD_PRELOAD=(\S+)[\n\r]+^\s*export\s+LD_PRELOAD/gm
I tried the following using Shell Script, but it doesn't work...
#!/bin/bash
envvars_stdFile="tempEnvVarsStd"
regEx="LD_PRELOAD=(\S+)[\n\r]+^\s*export\s+LD_PRELOAD"
if [[ "${envvars_stdFile}" =~ "${regEx}" ]]
then
cppLib="${BASH_REMATCH[1]}"
echo "cppLib is: $cppLib"
else
echo "RegEx Pattern (${regEx}) doesn't match in file: $envvars_stdFile"
fi
It Produces: RegEx Pattern (LD_PRELOAD=(\S+)[\n\r]+^\s*export\s+LD_PRELOAD) doesn't match in file: tempEnvVarsStd