when searching treat line as string, and regex as regexp

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
remotes/origin/shellcheck
Steven Armstrong 7 years ago
parent a3968f8313
commit fb26894cbd
  1. 13
      cdist/conf/type/__line/explorer/state
  2. 9
      cdist/conf/type/__line/gencode-remote

@ -37,6 +37,13 @@ else
fi fi
awk -v position="$position" -v needle="$needle" ' awk -v position="$position" -v needle="$needle" '
function _find(_text, _pattern) {
if (needle == "regex") {
return match(_text, _pattern)
} else {
return index(_text, _pattern)
}
}
BEGIN { BEGIN {
getline anchor < (ENVIRON["__object"] "/parameter/" position) getline anchor < (ENVIRON["__object"] "/parameter/" position)
getline pattern < (ENVIRON["__object"] "/parameter/" needle) getline pattern < (ENVIRON["__object"] "/parameter/" needle)
@ -46,7 +53,7 @@ BEGIN {
if (position == "after") { if (position == "after") {
if (match($0, anchor)) { if (match($0, anchor)) {
getline getline
if (match($0, pattern)) { if (_find($0, pattern)) {
state = "present" state = "present"
} }
else { else {
@ -56,7 +63,7 @@ BEGIN {
} }
} }
else if (position == "before") { else if (position == "before") {
if (match($0, pattern)) { if (_find($0, pattern)) {
getline getline
if (match($0, anchor)) { if (match($0, anchor)) {
state = "present" state = "present"
@ -68,7 +75,7 @@ BEGIN {
} }
} }
else { else {
if (match($0, pattern)) { if (_find($0, pattern)) {
state = "present" state = "present"
exit 0 exit 0
} }

@ -78,6 +78,13 @@ if [ -f "$file" ]; then
fi fi
awk -v position="$position" -v needle="$needle" -v remove=$remove -v add=$add ' awk -v position="$position" -v needle="$needle" -v remove=$remove -v add=$add '
function _find(_text, _pattern) {
if (needle == "regex") {
return match(_text, _pattern)
} else {
return index(_text, _pattern)
}
}
BEGIN { BEGIN {
line_file = ENVIRON["__object"] "/parameter/line" line_file = ENVIRON["__object"] "/parameter/line"
getline line < line_file getline line < line_file
@ -88,7 +95,7 @@ BEGIN {
} }
{ {
if (remove) { if (remove) {
if (match(\$0, pattern)) { if (_find(\$0, pattern)) {
# skip over this line -> remove it # skip over this line -> remove it
next next
} }

Loading…
Cancel
Save