Skip to content

Instantly share code, notes, and snippets.

@akhiljalagam
Last active July 4, 2023 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhiljalagam/a81d1125ad8447953f6794604dacef87 to your computer and use it in GitHub Desktop.
Save akhiljalagam/a81d1125ad8447953f6794604dacef87 to your computer and use it in GitHub Desktop.
Colored xtrace output
#!/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