Skip to content

Commit

Permalink
chore: Use grep -E / grep -F instead of egrep / fgrep (Bash-it#2164)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
davidpfarrell committed Oct 13, 2022
1 parent bf2034d commit 00062bf
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 22 deletions.
6 changes: 3 additions & 3 deletions completion/available/fabric.completion.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
# shellcheck shell=bash
#
# Bash completion support for Fabric (http://fabfile.org/)
#
Expand Down Expand Up @@ -91,7 +91,7 @@ function __fab_completion() {
-*)
if [[ -z "${__FAB_COMPLETION_LONG_OPT}" ]]; then
export __FAB_COMPLETION_LONG_OPT=$(
fab --help | egrep -o "\-\-[A-Za-z_\-]+\=?" | sort -u)
fab --help | grep -E -o "\-\-[A-Za-z_\-]+\=?" | sort -u)
fi
opts="${__FAB_COMPLETION_LONG_OPT}"
;;
Expand All @@ -101,7 +101,7 @@ function __fab_completion() {
# -*)
# if [[ -z "${__FAB_COMPLETION_SHORT_OPT}" ]]; then
# export __FAB_COMPLETION_SHORT_OPT=$(
# fab --help | egrep -o "^ +\-[A-Za-z_\]" | sort -u)
# fab --help | grep -E -o "^ +\-[A-Za-z_\]" | sort -u)
# fi
# opts="${__FAB_COMPLETION_SHORT_OPT}"
# ;;
Expand Down
4 changes: 3 additions & 1 deletion completion/available/gradle.completion.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# shellcheck shell=bash

# Copyright (c) 2017 Eric Wendelin

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

if [[ ! $(find $cache_dir/$cache_name -mmin -$cache_ttl_mins 2>/dev/null) ]]; then
# Cache all Gradle scripts
local gradle_build_scripts=$(find $project_root_dir -type f -name "*.gradle" -o -name "*.gradle.kts" 2>/dev/null | egrep -v "$script_exclude_pattern")
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")
printf "%s\n" "${gradle_build_scripts[@]}" > $cache_dir/$cache_name
fi
}
Expand Down
4 changes: 3 additions & 1 deletion completion/available/makefile.completion.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# shellcheck shell=bash

# Bash completion for Makefile
# Loosely adapted from http://stackoverflow.com/a/38415982/1472048

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

[ "${#targets[@]}" -eq 0 ] && return 0
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function _is_function() {
_example '$ _is_function ls && echo exists'
_group 'lib'
local msg="${2:-Function '$1' does not exist}"
if LC_ALL=C type -t "$1" | _bash-it-egrep -q 'function'; then
if LC_ALL=C type -t "$1" | _bash-it-fgrep -q 'function'; then
return 0
else
_log_debug "$msg"
Expand Down
20 changes: 13 additions & 7 deletions lib/utilities.bash
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,21 @@ function _bash-it-array-dedup() {
printf '%s\n' "$@" | sort -u
}

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

# Runs `grep` with extended regular expressions
# Runs `grep` with fixed-string expressions (-F)
function _bash-it-fgrep() {
: "${BASH_IT_GREP:=$(type -P grep)}"
"${BASH_IT_GREP:-/usr/bin/grep}" -F "$@"
}

# Runs `grep` with extended regular expressions (-E)
function _bash-it-egrep() {
: "${BASH_IT_GREP:=$(type -P egrep || type -P grep)}"
: "${BASH_IT_GREP:=$(type -P grep)}"
"${BASH_IT_GREP:-/usr/bin/grep}" -E "$@"
}

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

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

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

# Checks if a given item is enabled for a particular component/file-type.
Expand Down
4 changes: 2 additions & 2 deletions lint_clean_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# shellcheck disable=SC2002 # Prefer 'cat' for cleaner script
mapfile -t FILES < <(
cat clean_files.txt \
| grep -v -E '^\s*$' \
| grep -v -E '^\s*#' \
| grep -E -v '^\s*$' \
| grep -E -v '^\s*#' \
| xargs -n1 -I{} find "{}" -type f
)

Expand Down
7 changes: 4 additions & 3 deletions plugins/available/aws.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'AWS helper functions'

Expand Down Expand Up @@ -40,13 +41,13 @@ function __awskeys_help {
function __awskeys_get {
local ln=$(grep -n "\[ *$1 *\]" "${AWS_SHARED_CREDENTIALS_FILE}" | cut -d ":" -f 1)
if [[ -n "${ln}" ]]; then
tail -n +${ln} "${AWS_SHARED_CREDENTIALS_FILE}" | egrep -m 2 "aws_access_key_id|aws_secret_access_key"
tail -n +${ln} "${AWS_SHARED_CREDENTIALS_FILE}" | egrep -m 1 "aws_session_token"
tail -n +${ln} "${AWS_SHARED_CREDENTIALS_FILE}" | grep -F -m 2 -e "aws_access_key_id" -e "aws_secret_access_key"
tail -n +${ln} "${AWS_SHARED_CREDENTIALS_FILE}" | grep -F -m 1 "aws_session_token"
fi
}

function __awskeys_list {
local credentials_list="$((egrep '^\[ *[a-zA-Z0-9_-]+ *\]$' "${AWS_SHARED_CREDENTIALS_FILE}"; grep "\[profile" "${AWS_CONFIG_FILE}" | sed "s|\[profile |\[|g") | sort | uniq)"
local credentials_list="$((grep -E '^\[ *[a-zA-Z0-9_-]+ *\]$' "${AWS_SHARED_CREDENTIALS_FILE}"; grep "\[profile" "${AWS_CONFIG_FILE}" | sed "s|\[profile |\[|g") | sort | uniq)"
if [[ -n $"{credentials_list}" ]]; then
echo -e "Available credentials profiles:\n"
for profile in ${credentials_list}; do
Expand Down
4 changes: 2 additions & 2 deletions plugins/available/jekyll.plugin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function editpost() {
pushd "${SITE}/_posts" > /dev/null || return

for POST in *; do
DATE="$(echo "${POST}" | grep -oE "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")"
TITLE="$(grep -oE "title: (.+)" < "${POST}")"
DATE="$(echo "${POST}" | grep -E -o "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")"
TITLE="$(grep -E -o "title: (.+)" < "${POST}")"
TITLE="${TITLE/title: /}"
echo "${COUNTER}) ${DATE} ${TITLE}"
POSTS[COUNTER]="$POST"
Expand Down
3 changes: 2 additions & 1 deletion plugins/available/postgres.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'postgres helper functions'

Expand Down Expand Up @@ -50,7 +51,7 @@ function postgres_status {


function is_postgres_running {
$POSTGRES_BIN/pg_ctl -D $PGDATA status | egrep -o "no server running"
$POSTGRES_BIN/pg_ctl -D $PGDATA status | grep -F -o "no server running"
}


Expand Down
4 changes: 3 additions & 1 deletion themes/rjorgenson/rjorgenson.theme.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# shellcheck shell=bash

# port of zork theme

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

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

0 comments on commit 00062bf

Please sign in to comment.