Skip to content

Commit 00062bf

Browse files
chore: Use grep -E / grep -F instead of egrep / fgrep (#2164)
Ensures that the -E or -F option, when used, is the first option * i.e. grep -oE => grep -E -o Updates _bash-it-grep to invoke grep with just the provided arguments * This function was (and still is) unused, but decided this new functionality was actually more useful Introduces _bash-it-fgrep to invoke grep -F Removes type -P egrep from the _bash-it-*grep functions For usages that were already going to be modified, use -F if appropriate * Does not touch grep usages that may have benefited from -F, but were not otherwise considered for this PR Adds shellcheck header to modified .bash files that didn't already have it
1 parent bf2034d commit 00062bf

File tree

10 files changed

+36
-22
lines changed

10 files changed

+36
-22
lines changed

completion/available/fabric.completion.bash

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env bash
1+
# shellcheck shell=bash
22
#
33
# Bash completion support for Fabric (http://fabfile.org/)
44
#
@@ -91,7 +91,7 @@ function __fab_completion() {
9191
-*)
9292
if [[ -z "${__FAB_COMPLETION_LONG_OPT}" ]]; then
9393
export __FAB_COMPLETION_LONG_OPT=$(
94-
fab --help | egrep -o "\-\-[A-Za-z_\-]+\=?" | sort -u)
94+
fab --help | grep -E -o "\-\-[A-Za-z_\-]+\=?" | sort -u)
9595
fi
9696
opts="${__FAB_COMPLETION_LONG_OPT}"
9797
;;
@@ -101,7 +101,7 @@ function __fab_completion() {
101101
# -*)
102102
# if [[ -z "${__FAB_COMPLETION_SHORT_OPT}" ]]; then
103103
# export __FAB_COMPLETION_SHORT_OPT=$(
104-
# fab --help | egrep -o "^ +\-[A-Za-z_\]" | sort -u)
104+
# fab --help | grep -E -o "^ +\-[A-Za-z_\]" | sort -u)
105105
# fi
106106
# opts="${__FAB_COMPLETION_SHORT_OPT}"
107107
# ;;

completion/available/gradle.completion.bash

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# shellcheck shell=bash
2+
13
# Copyright (c) 2017 Eric Wendelin
24

35
# Permission is hereby granted, free of charge, to any person obtaining a copy of
@@ -66,7 +68,7 @@ __gradle-generate-script-cache() {
6668

6769
if [[ ! $(find $cache_dir/$cache_name -mmin -$cache_ttl_mins 2>/dev/null) ]]; then
6870
# Cache all Gradle scripts
69-
local gradle_build_scripts=$(find $project_root_dir -type f -name "*.gradle" -o -name "*.gradle.kts" 2>/dev/null | egrep -v "$script_exclude_pattern")
71+
local gradle_build_scripts=$(find $project_root_dir -type f -name "*.gradle" -o -name "*.gradle.kts" 2>/dev/null | grep -E -v "$script_exclude_pattern")
7072
printf "%s\n" "${gradle_build_scripts[@]}" > $cache_dir/$cache_name
7173
fi
7274
}

completion/available/makefile.completion.bash

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# shellcheck shell=bash
2+
13
# Bash completion for Makefile
24
# Loosely adapted from http://stackoverflow.com/a/38415982/1472048
35

@@ -17,7 +19,7 @@ _makecomplete() {
1719
for f in "${files[@]}" ; do
1820
while IFS='' read -r line ; do
1921
targets+=("$line")
20-
done < <(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' "$f" | cut -d':' -f1)
22+
done < <(grep -E -o '^[a-zA-Z0-9_-]+:([^=]|$)' "$f" | cut -d':' -f1)
2123
done
2224

2325
[ "${#targets[@]}" -eq 0 ] && return 0

lib/helpers.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function _is_function() {
211211
_example '$ _is_function ls && echo exists'
212212
_group 'lib'
213213
local msg="${2:-Function '$1' does not exist}"
214-
if LC_ALL=C type -t "$1" | _bash-it-egrep -q 'function'; then
214+
if LC_ALL=C type -t "$1" | _bash-it-fgrep -q 'function'; then
215215
return 0
216216
else
217217
_log_debug "$msg"

lib/utilities.bash

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,21 @@ function _bash-it-array-dedup() {
6060
printf '%s\n' "$@" | sort -u
6161
}
6262

63-
# Outputs a full path of the grep found on the filesystem
63+
# Runs `grep` with *just* the provided arguments
6464
function _bash-it-grep() {
65-
: "${BASH_IT_GREP:=$(type -P egrep || type -P grep)}"
66-
printf "%s" "${BASH_IT_GREP:-/usr/bin/grep}"
65+
: "${BASH_IT_GREP:=$(type -P grep)}"
66+
"${BASH_IT_GREP:-/usr/bin/grep}" "$@"
6767
}
6868

69-
# Runs `grep` with extended regular expressions
69+
# Runs `grep` with fixed-string expressions (-F)
70+
function _bash-it-fgrep() {
71+
: "${BASH_IT_GREP:=$(type -P grep)}"
72+
"${BASH_IT_GREP:-/usr/bin/grep}" -F "$@"
73+
}
74+
75+
# Runs `grep` with extended regular expressions (-E)
7076
function _bash-it-egrep() {
71-
: "${BASH_IT_GREP:=$(type -P egrep || type -P grep)}"
77+
: "${BASH_IT_GREP:=$(type -P grep)}"
7278
"${BASH_IT_GREP:-/usr/bin/grep}" -E "$@"
7379
}
7480

@@ -150,12 +156,12 @@ function _bash-it-component-list-matching() {
150156

151157
function _bash-it-component-list-enabled() {
152158
local IFS=$'\n' component="$1"
153-
_bash-it-component-help "${component}" | _bash-it-egrep '\[x\]' | awk '{print $1}' | sort -u
159+
_bash-it-component-help "${component}" | _bash-it-fgrep '[x]' | awk '{print $1}' | sort -u
154160
}
155161

156162
function _bash-it-component-list-disabled() {
157163
local IFS=$'\n' component="$1"
158-
_bash-it-component-help "${component}" | _bash-it-egrep -v '\[x\]' | awk '{print $1}' | sort -u
164+
_bash-it-component-help "${component}" | _bash-it-fgrep -v '[x]' | awk '{print $1}' | sort -u
159165
}
160166

161167
# Checks if a given item is enabled for a particular component/file-type.

lint_clean_files.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
# shellcheck disable=SC2002 # Prefer 'cat' for cleaner script
99
mapfile -t FILES < <(
1010
cat clean_files.txt \
11-
| grep -v -E '^\s*$' \
12-
| grep -v -E '^\s*#' \
11+
| grep -E -v '^\s*$' \
12+
| grep -E -v '^\s*#' \
1313
| xargs -n1 -I{} find "{}" -type f
1414
)
1515

plugins/available/aws.plugin.bash

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# shellcheck shell=bash
12
cite about-plugin
23
about-plugin 'AWS helper functions'
34

@@ -40,13 +41,13 @@ function __awskeys_help {
4041
function __awskeys_get {
4142
local ln=$(grep -n "\[ *$1 *\]" "${AWS_SHARED_CREDENTIALS_FILE}" | cut -d ":" -f 1)
4243
if [[ -n "${ln}" ]]; then
43-
tail -n +${ln} "${AWS_SHARED_CREDENTIALS_FILE}" | egrep -m 2 "aws_access_key_id|aws_secret_access_key"
44-
tail -n +${ln} "${AWS_SHARED_CREDENTIALS_FILE}" | egrep -m 1 "aws_session_token"
44+
tail -n +${ln} "${AWS_SHARED_CREDENTIALS_FILE}" | grep -F -m 2 -e "aws_access_key_id" -e "aws_secret_access_key"
45+
tail -n +${ln} "${AWS_SHARED_CREDENTIALS_FILE}" | grep -F -m 1 "aws_session_token"
4546
fi
4647
}
4748

4849
function __awskeys_list {
49-
local credentials_list="$((egrep '^\[ *[a-zA-Z0-9_-]+ *\]$' "${AWS_SHARED_CREDENTIALS_FILE}"; grep "\[profile" "${AWS_CONFIG_FILE}" | sed "s|\[profile |\[|g") | sort | uniq)"
50+
local credentials_list="$((grep -E '^\[ *[a-zA-Z0-9_-]+ *\]$' "${AWS_SHARED_CREDENTIALS_FILE}"; grep "\[profile" "${AWS_CONFIG_FILE}" | sed "s|\[profile |\[|g") | sort | uniq)"
5051
if [[ -n $"{credentials_list}" ]]; then
5152
echo -e "Available credentials profiles:\n"
5253
for profile in ${credentials_list}; do

plugins/available/jekyll.plugin.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function editpost() {
3030
pushd "${SITE}/_posts" > /dev/null || return
3131

3232
for POST in *; do
33-
DATE="$(echo "${POST}" | grep -oE "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")"
34-
TITLE="$(grep -oE "title: (.+)" < "${POST}")"
33+
DATE="$(echo "${POST}" | grep -E -o "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")"
34+
TITLE="$(grep -E -o "title: (.+)" < "${POST}")"
3535
TITLE="${TITLE/title: /}"
3636
echo "${COUNTER}) ${DATE} ${TITLE}"
3737
POSTS[COUNTER]="$POST"

plugins/available/postgres.plugin.bash

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# shellcheck shell=bash
12
cite about-plugin
23
about-plugin 'postgres helper functions'
34

@@ -50,7 +51,7 @@ function postgres_status {
5051

5152

5253
function is_postgres_running {
53-
$POSTGRES_BIN/pg_ctl -D $PGDATA status | egrep -o "no server running"
54+
$POSTGRES_BIN/pg_ctl -D $PGDATA status | grep -F -o "no server running"
5455
}
5556

5657

themes/rjorgenson/rjorgenson.theme.bash

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# shellcheck shell=bash
2+
13
# port of zork theme
24

35
# set colors for use throughout the prompt
@@ -50,7 +52,7 @@ function is_integer() { # helper function for todo-txt-count
5052

5153
todo_txt_count() {
5254
if `hash todo.sh 2>&-`; then # is todo.sh installed
53-
count=`todo.sh ls | egrep "TODO: [0-9]+ of ([0-9]+) tasks shown" | awk '{ print $4 }'`
55+
count=`todo.sh ls | grep -E "TODO: [0-9]+ of ([0-9]+) tasks shown" | awk '{ print $4 }'`
5456
if is_integer $count; then # did we get a sane answer back
5557
echo "${BRACKET_COLOR}[${STRING_COLOR}T:$count${BRACKET_COLOR}]$normal"
5658
fi

0 commit comments

Comments
 (0)