From cc7a77804b12d86251e71303d1887b48c43f13b2 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 21 Aug 2016 23:02:35 +1200 Subject: Consolidate ed,bc,grep,ls.sh into one function --- sh/profile.d/bc.sh | 22 -------------------- sh/profile.d/ed.sh | 22 -------------------- sh/profile.d/grep.sh | 27 ------------------------ sh/profile.d/ls.sh | 27 ------------------------ sh/profile.d/options.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 98 deletions(-) delete mode 100644 sh/profile.d/bc.sh delete mode 100644 sh/profile.d/ed.sh delete mode 100644 sh/profile.d/grep.sh delete mode 100644 sh/profile.d/ls.sh create mode 100644 sh/profile.d/options.sh (limited to 'sh/profile.d') diff --git a/sh/profile.d/bc.sh b/sh/profile.d/bc.sh deleted file mode 100644 index 16200b33..00000000 --- a/sh/profile.d/bc.sh +++ /dev/null @@ -1,22 +0,0 @@ -# Test that we have metadata about what options this system's bc(1) supports, -# and try to create it if not -( - # Create a directory to hold metadata about bc - bcd=$HOME/.cache/bc - if ! [ -d "$bcd" ] ; then - mkdir -p -- "$bcd" || exit - fi - - # Write bc(1)'s --help output to a file, even if it's empty - if ! [ -f "$bcd"/help ] ; then - bc --help "$bcd"/help 2>/dev/null || exit - - # Iterate through some useful options and create files to show they're - # available - set -- quiet - for opt ; do - grep -q -- --"$opt" "$bcd"/help || continue - touch -- "$bcd"/"$opt" || exit - done - fi -) diff --git a/sh/profile.d/ed.sh b/sh/profile.d/ed.sh deleted file mode 100644 index abbd75c5..00000000 --- a/sh/profile.d/ed.sh +++ /dev/null @@ -1,22 +0,0 @@ -# Test that we have metadata about what options this system's ed(1) supports, -# and try to create it if not -( - # Create a directory to hold metadata about ed - ecd=$HOME/.cache/ed - if ! [ -d "$ecd" ] ; then - mkdir -p -- "$ecd" || exit - fi - - # Write ed(1)'s --help output to a file, even if it's empty - if ! [ -f "$ecd"/help ] ; then - ed --help "$ecd"/help 2>/dev/null || exit - - # Iterate through some useful options and create files to show they're - # available - set -- verbose - for opt ; do - grep -q -- --"$opt" "$ecd"/help || continue - touch -- "$ecd"/"$opt" || exit - done - fi -) diff --git a/sh/profile.d/grep.sh b/sh/profile.d/grep.sh deleted file mode 100644 index d6f9c36b..00000000 --- a/sh/profile.d/grep.sh +++ /dev/null @@ -1,27 +0,0 @@ -# Test that we have metadata about what options this system's grep(1) supports, -# and try to create it if not -( - # Create a directory to hold metadata about grep - gcd=$HOME/.cache/grep - if ! [ -d "$gcd" ] ; then - mkdir -p -- "$gcd" || exit - fi - - # Write grep(1)'s --help output to a file, even if it's empty - if ! [ -f "$gcd"/help ] ; then - grep --help "$gcd"/help 2>/dev/null || exit - - # Iterate through some useful options and create files to show they're - # available - set -- binary-files \ - color \ - devices \ - directories \ - exclude \ - exclude-dir - for opt ; do - grep -q -- --"$opt" "$gcd"/help || continue - touch -- "$gcd"/"$opt" || exit - done - fi -) diff --git a/sh/profile.d/ls.sh b/sh/profile.d/ls.sh deleted file mode 100644 index 2b0f507d..00000000 --- a/sh/profile.d/ls.sh +++ /dev/null @@ -1,27 +0,0 @@ -# Test that we have metadata about what options this system's ls(1) supports, -# and try to create it if not -( - # Create a directory to hold metadata about ls(1) - lcd=$HOME/.cache/ls - if ! [ -d "$lcd" ] ; then - mkdir -p -- "$lcd" || exit - fi - - # Write ls(1)'s --help output to a file, even if it's empty - if ! [ -f "$lcd"/help ] ; then - ls --help >"$lcd"/help 2>/dev/null || exit - - # Iterate through some useful options and create files to show they're - # available - set -- almost-all \ - block-size \ - classify \ - color \ - human-readable \ - time-style - for opt ; do - grep -q -- --"$opt" "$lcd"/help || continue - touch -- "$lcd"/"$opt" || exit - done - fi -) diff --git a/sh/profile.d/options.sh b/sh/profile.d/options.sh new file mode 100644 index 00000000..aa6bea06 --- /dev/null +++ b/sh/profile.d/options.sh @@ -0,0 +1,55 @@ +# Cache the options available to certain programs. Run all this in a subshell +# (none of its state needs to endure in the session) +( +options() { ( + + # Check or create the directory to cache the options + dir=$HOME/.cache/$1 + + # Directory already exists; bail out + [ -d "$dir" ] && exit + + # Create the directory + mkdir -p -- "$dir" || exit + cd -- "$dir" || exit + + # Write grep(1)'s --help output to a file, even if it's empty + "$1" --help help 2>/dev/null || exit + + # Shift the program name off; remaining arguments are the options to check + shift + + # Iterate through some useful options and create files to show they're + # available if found in the help output + for opt ; do + grep -q -- '[^[:alnum:]]--'"$opt"'[^[:alnum:]]' help && + touch -- "$opt" + done +) ; } + +# Cache options for bc(1) +options bc \ + quiet + +# Cache options for ed(1) +options ed \ + verbose + +# Cache options for grep(1) +options grep \ + binary-files \ + color \ + devices \ + directories \ + exclude \ + exclude-dir + +# Cache options for ls(1) +options ls \ + almost-all \ + block-size \ + classify \ + color \ + human-readable \ + time-style +) -- cgit v1.2.3