chore(bin): tweak x-path-* scripts

This commit is contained in:
2024-07-28 15:35:50 +03:00
parent 37299f4893
commit 3413365d41
3 changed files with 47 additions and 112 deletions

View File

@@ -6,13 +6,6 @@
# Set verbosity with VERBOSE=1 # Set verbosity with VERBOSE=1
VERBOSE="${VERBOSE:-0}" VERBOSE="${VERBOSE:-0}"
# Function to print usage information
usage()
{
echo "Usage: $0 <dir>"
exit 1
}
# Function to print messages if VERBOSE is enabled # Function to print messages if VERBOSE is enabled
# $1 - message (string) # $1 - message (string)
msg() msg()
@@ -20,48 +13,28 @@ msg()
[[ "$VERBOSE" -eq 1 ]] && echo "$1" [[ "$VERBOSE" -eq 1 ]] && echo "$1"
} }
# Function to remove a directory from PATH if [ "$#" -ne 1 ]; then
# $1 - directory to remove (string) echo "Usage: $0 <dir>"
remove_from_path() exit 1
{ fi
local dir=$1
if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then dir="$1"
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
}
# Function to append a directory to PATH if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then
# $1 - directory to append (string) export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$dir\"" | sed 's/:$//')
append_to_path() msg "Directory $dir has been removed from PATH"
{ else
local dir=$1 msg "Directory $dir is not in PATH"
fi
if [ ! -d "$dir" ]; then if [ ! -d "$dir" ]; then
msg "(?) Directory $dir does not exist" msg "(?) Directory $dir does not exist"
exit 0 exit 0
fi fi
if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then
msg "(!) Directory $dir is already in PATH" msg "(!) Directory $dir is already in PATH"
else else
export PATH="${PATH:+"$PATH:"}$dir" export PATH="${PATH:+"$PATH:"}$dir"
msg "(!) Directory $dir has been added to the end of PATH" msg "(!) Directory $dir has been added to the end of PATH"
fi fi
}
# Main function
main()
{
if [ "$#" -ne 1 ]; then
usage
fi
remove_from_path "$1"
append_to_path "$1"
}
main "$@"

View File

@@ -6,13 +6,6 @@
# Set verbosity with VERBOSE=1 # Set verbosity with VERBOSE=1
VERBOSE="${VERBOSE:-0}" VERBOSE="${VERBOSE:-0}"
# Function to print usage information
usage()
{
echo "Usage: $0 <dir>"
exit 1
}
# Function to print messages if VERBOSE is enabled # Function to print messages if VERBOSE is enabled
# $1 - message (string) # $1 - message (string)
msg() msg()
@@ -20,33 +13,21 @@ msg()
[[ "$VERBOSE" -eq 1 ]] && echo "$1" [[ "$VERBOSE" -eq 1 ]] && echo "$1"
} }
# Function to add a directory to the front of PATH if [ "$#" -ne 1 ]; then
# $1 - directory to add (string) echo "Usage: $0 <dir>"
prepend_to_path() exit 1
{ fi
local dir=$1
if [ ! -d "$dir" ]; then dir="$1"
msg "(?) Directory $dir does not exist"
exit 0
fi
if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then if [ ! -d "$dir" ]; then
msg "(!) Directory $dir is already in PATH" msg "(?) Directory $dir does not exist"
else exit 0
export PATH="$dir${PATH:+":$PATH"}" fi
msg "(!) Directory $dir has been added to the front of PATH"
fi
}
# Main function if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then
main() msg "(!) Directory $dir is already in PATH"
{ else
if [ "$#" -ne 1 ]; then export PATH="$dir${PATH:+":$PATH"}"
usage msg "(!) Directory $dir has been added to the front of PATH"
fi fi
prepend_to_path "$1"
}
main "$@"

View File

@@ -6,13 +6,6 @@
# Set verbosity with VERBOSE=1 # Set verbosity with VERBOSE=1
VERBOSE="${VERBOSE:-0}" VERBOSE="${VERBOSE:-0}"
# Function to print usage information
usage()
{
echo "Usage: $0 <dir>"
exit 1
}
# Function to print messages if VERBOSE is enabled # Function to print messages if VERBOSE is enabled
# $1 - message (string) # $1 - message (string)
msg() msg()
@@ -20,29 +13,17 @@ msg()
[[ "$VERBOSE" -eq 1 ]] && echo "$1" [[ "$VERBOSE" -eq 1 ]] && echo "$1"
} }
# Function to remove a directory from PATH if [ "$#" -ne 1 ]; then
# $1 - directory to remove (string) echo "Usage: $0 <dir>"
remove_from_path() exit 1
{ fi
local dir=$1
if ! echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then dir="$1"
msg "(?) Directory $dir is not in PATH"
exit 0
fi
export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$dir\"" | sed 's/:$//') if ! echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then
msg "(!) Directory $dir has been removed from PATH" msg "(?) Directory $dir is not in PATH"
} exit 0
fi
# Main function export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$dir\"" | sed 's/:$//')
main() msg "(!) Directory $dir has been removed from PATH"
{
if [ "$#" -ne 1 ]; then
usage
fi
remove_from_path "$1"
}
main "$@"