From 3413365d41ca8079058279e887ddf9fc396eb905 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Sun, 28 Jul 2024 15:35:50 +0300 Subject: [PATCH] chore(bin): tweak x-path-* scripts --- local/bin/x-path-append | 69 ++++++++++++---------------------------- local/bin/x-path-prepend | 49 +++++++++------------------- local/bin/x-path-remove | 41 +++++++----------------- 3 files changed, 47 insertions(+), 112 deletions(-) diff --git a/local/bin/x-path-append b/local/bin/x-path-append index d8343f1..57a8dc9 100755 --- a/local/bin/x-path-append +++ b/local/bin/x-path-append @@ -6,13 +6,6 @@ # Set verbosity with VERBOSE=1 VERBOSE="${VERBOSE:-0}" -# Function to print usage information -usage() -{ - echo "Usage: $0 " - exit 1 -} - # Function to print messages if VERBOSE is enabled # $1 - message (string) msg() @@ -20,48 +13,28 @@ msg() [[ "$VERBOSE" -eq 1 ]] && echo "$1" } -# Function to remove a directory from PATH -# $1 - directory to remove (string) -remove_from_path() -{ - local dir=$1 +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi - if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then - export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$dir\"" | sed 's/:$//') - msg "Directory $dir has been removed from PATH" - else - msg "Directory $dir is not in PATH" - fi -} +dir="$1" -# Function to append a directory to PATH -# $1 - directory to append (string) -append_to_path() -{ - local dir=$1 +if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then + export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$dir\"" | sed 's/:$//') + msg "Directory $dir has been removed from PATH" +else + msg "Directory $dir is not in PATH" +fi - if [ ! -d "$dir" ]; then - msg "(?) Directory $dir does not exist" - exit 0 - fi +if [ ! -d "$dir" ]; then + msg "(?) Directory $dir does not exist" + exit 0 +fi - if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then - msg "(!) Directory $dir is already in PATH" - else - export PATH="${PATH:+"$PATH:"}$dir" - msg "(!) Directory $dir has been added to the end of PATH" - fi -} - -# Main function -main() -{ - if [ "$#" -ne 1 ]; then - usage - fi - - remove_from_path "$1" - append_to_path "$1" -} - -main "$@" +if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then + msg "(!) Directory $dir is already in PATH" +else + export PATH="${PATH:+"$PATH:"}$dir" + msg "(!) Directory $dir has been added to the end of PATH" +fi diff --git a/local/bin/x-path-prepend b/local/bin/x-path-prepend index 883a98d..8797b7a 100755 --- a/local/bin/x-path-prepend +++ b/local/bin/x-path-prepend @@ -6,13 +6,6 @@ # Set verbosity with VERBOSE=1 VERBOSE="${VERBOSE:-0}" -# Function to print usage information -usage() -{ - echo "Usage: $0 " - exit 1 -} - # Function to print messages if VERBOSE is enabled # $1 - message (string) msg() @@ -20,33 +13,21 @@ msg() [[ "$VERBOSE" -eq 1 ]] && echo "$1" } -# Function to add a directory to the front of PATH -# $1 - directory to add (string) -prepend_to_path() -{ - local dir=$1 +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi - if [ ! -d "$dir" ]; then - msg "(?) Directory $dir does not exist" - exit 0 - fi +dir="$1" - if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then - msg "(!) Directory $dir is already in PATH" - else - export PATH="$dir${PATH:+":$PATH"}" - msg "(!) Directory $dir has been added to the front of PATH" - fi -} +if [ ! -d "$dir" ]; then + msg "(?) Directory $dir does not exist" + exit 0 +fi -# Main function -main() -{ - if [ "$#" -ne 1 ]; then - usage - fi - - prepend_to_path "$1" -} - -main "$@" +if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then + msg "(!) Directory $dir is already in PATH" +else + export PATH="$dir${PATH:+":$PATH"}" + msg "(!) Directory $dir has been added to the front of PATH" +fi diff --git a/local/bin/x-path-remove b/local/bin/x-path-remove index 23b4fe3..fc17fa0 100755 --- a/local/bin/x-path-remove +++ b/local/bin/x-path-remove @@ -6,13 +6,6 @@ # Set verbosity with VERBOSE=1 VERBOSE="${VERBOSE:-0}" -# Function to print usage information -usage() -{ - echo "Usage: $0 " - exit 1 -} - # Function to print messages if VERBOSE is enabled # $1 - message (string) msg() @@ -20,29 +13,17 @@ msg() [[ "$VERBOSE" -eq 1 ]] && echo "$1" } -# Function to remove a directory from PATH -# $1 - directory to remove (string) -remove_from_path() -{ - local dir=$1 +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi - if ! echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then - msg "(?) Directory $dir is not in PATH" - exit 0 - fi +dir="$1" - export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$dir\"" | sed 's/:$//') - msg "(!) Directory $dir has been removed from PATH" -} +if ! echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then + msg "(?) Directory $dir is not in PATH" + exit 0 +fi -# Main function -main() -{ - if [ "$#" -ne 1 ]; then - usage - fi - - remove_from_path "$1" -} - -main "$@" +export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$dir\"" | sed 's/:$//') +msg "(!) Directory $dir has been removed from PATH"