0

Can someone clean up this part of bin bash script for me? I feel like i can write this so much cleaner.

Basically, what i want to do is:

  • If $pluginexcl2 is defined that 2 different grep -v will be used
  • If $pluginexcl1 is defined than only 1 grep -v
  • If both are undefined, not to use grep -v.

Help is appreciated.

# Snygga curl koden
curl_url=$(curl -s https://api.github.com/repos/${pluginrepo}/releases/latest)
 
# Filtrera grep :D
if [ -n "${pluginexcl1}" ] && [ -n "${pluginexcl2}" ]; then
    echo "$curl_url" | grep -wo "$plugingrep" | grep -v "$pluginexcl1" | grep -v "$pluginexcl2" | wget -qi - -O "${pluginfile}"
elif [ -n "${pluginexcl1}" ]; then
    echo "$curl_url" | grep -wo "$plugingrep" | grep -v "$pluginexcl1" | wget -qi - -O "${pluginfile}"
else
    echo "$curl_url" | grep -wo "$plugingrep" | wget -qi - -O "${pluginfile}"
fi
2
  • 2
    Always paste your script into https://shellcheck.net, a syntax checker, or install shellcheck locally. Make using shellcheck part of your development process.
    – waltinator
    Commented Dec 26, 2024 at 3:25
  • As in HTML as a derivate of XML some space and newline are not relevant, it is usually not a good idea to use line-depemndent tools to parse it. Better use something with xpath like`` xmlstarlet`
    – Marco
    Commented Dec 26, 2024 at 7:21

4 Answers 4

1

Consolidate the two conditional grep -v operations into a single operation using an -Extended regex, combined with parameter substitution of a non-sensical string if the variables are not populated.

One idea to replace the entire if/elif/else/fi block:

echo "$curl_url" | grep -wo "$plugingrep" | grep -Ev "${pluginexcl1:-XXXX}|${pluginexcl2:-XXXX}" | wget -qi - -O "${pluginfile}"

Replace the XXXX with a string you're sure doesn't show up in the contents of $curl_url.

NOTES:

  • I'm assuming OP's current code works as expected.
  • I'm (obviously) unable to test this since we're not given sample contents of the referenced variables.
1
latest_release=$(wget -q -O - https://api.github.com/repos/"$pluginrepo"/releases/latest)

plugin_exclude=(
        ${pluginexcl1:+-e "$pluginexcl1"}
        ${pluginexcl2:+-e "$pluginexcl2"}
)

printf '%s\n' "$latest_release" |
grep -w -o -e "$plugingrep" |
if [ "${#plugin_exclude[@]}" -gt 0 ]; then
        grep -v "${plugin_exclude[@]}"
fi |
wget -q -i - -O "$pluginfile"

This puts the exclusion patterns into the array plugin_exclude if defined and non-empty. Each pattern is prepended with -e to avoid accidentally interpreting them as options to the grep command if they start with dashes and also to allow the excluding grep -v invocation to use two separate patterns.

The if statement is then collapsed into a single pipeline where the penultimate stage is a short if statement that applies the exclusion patterns if they exist (if the plugin_exclude array has elements).


The same code, but for the POSIX sh shell:

latest_release=$(wget -q -O - https://api.github.com/repos/"$pluginrepo"/releases/latest)

set --  ${pluginexcl1:+-e "$pluginexcl1"} \
        ${pluginexcl2:+-e "$pluginexcl2"}

printf '%s\n' "$latest_release" |
grep -w -o -e "$plugingrep" |
if [ "$#" -gt 0 ]; then
        grep -v "$@"
fi |
wget -q -i - -O "$pluginfile"
0
0

This substitution "^\b$" will match no word edge, which means the grep -v will exclude no lines for the undefined parameter(s).

# Snygga curl koden
curl_url=$(curl -s https://api.github.com/repos/${pluginrepo}/releases/latest)
 
# Filtrera grep :D

echo "$curl_url" | grep -wo "$plugingrep" | \
   grep -v "${pluginexcl1:-^\b$}\|${pluginexcl2:-^\b$}" | wget -qi - -O "${pluginfile}"
-1

Try this..

curl_url=$(curl -s https://api.github.com/repos/${pluginrepo}/releases/latest)
grep_command="grep -wo \"$plugingrep\""
[ -n "${pluginexcl1}" ] && grep_command+=" | grep -v \"$pluginexcl1\""
[ -n "${pluginexcl2}" ] && grep_command+=" | grep -v \"$pluginexcl2\""
echo "$curl_url" | eval "$grep_command" | wget -qi - -O "${pluginfile}"

I didn't test all script, but i don't see any secret or monster. haha

Hope this help!

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.