Last active
July 4, 2023 11:25
-
-
Save akhiljalagam/a81d1125ad8447953f6794604dacef87 to your computer and use it in GitHub Desktop.
Colored xtrace output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SCRIPT="$1" | |
# Remove the script name from positional parameters | |
shift | |
echo "Arguments passed: $@" | |
echo "Total number of arguments: $#" | |
UNIQUE_STR='@@@' | |
COLOR=5 | |
normal() | |
{ | |
tput sgr0 # tell the terminal to not style the output | |
} | |
colored() | |
{ | |
tput setaf $COLOR # tell the terminal to set forward color to $COLOR | |
} | |
export PS4="+${UNIQUE_STR}" # artificially insert $UNIQUE_STR to the trace | |
exec &> >(sed "s/\(\+\)*\+\(${UNIQUE_STR}\)\(.*\)$/$(colored)\1+\3$(normal)/") # see below | |
promptyn () { | |
while true; do | |
echo "$1" | |
read -r yn | |
case $yn in | |
[Yy]* ) return 0;; | |
[Nn]* ) return 1;; | |
* ) echo "Please answer yes or no.";; | |
esac | |
done | |
} | |
promptyn 'proceed with script and args? (y/n) ?' || exit 1 | |
set -x # set trace | |
source "$SCRIPT" "$@" # run the commands in the current shell so that all settings apply |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment