Skip to content

Instantly share code, notes, and snippets.

@wlib
Last active April 30, 2024 17:07
Show Gist options
  • Save wlib/093f8b8f670016813073a4c4f8b28e81 to your computer and use it in GitHub Desktop.
Save wlib/093f8b8f670016813073a4c4f8b28e81 to your computer and use it in GitHub Desktop.
Run a shell script with bash, line-by-line, prompted on each command. Useful for running unknown scripts or debugging. Not a secure substitute for understanding a script beforehand.
#!/bin/bash
# Just so we can skip the first command - running the first argument
SKIP=true
function prompt {
if [[ "$SKIP" == true ]]; then
SKIP=false
return
fi
if [[ -n "$BASH_COMMAND" ]]; then
# Show the command in green with a simple -> prompt
echo -n -e "-> \x1B[0;32m"
echo -n $BASH_COMMAND
echo -e "\x1B[0m"
# Loop user input until a valid answer comes
while true; do
# Ask for only a single character of input, so the user does not need to type an extra enter
read -n1 -s -p "Run command? [Y/n] "
echo
case "$REPLY" in
"y" | "Y" | "" )
break
;;
"n" | "N" )
# Bright red indication that that script stopped
echo -e "\x1B[1;31mStopped!\x1B[0m"
exit 1
;;
* )
echo "Please answer by typing n (for no), y (for yes), or Enter (also for yes)"
;;
esac
done
fi
}
# Allow debugging in the file that is being source'd
set -o functrace
# Run the prompt function before each command
trap prompt DEBUG
# Run the first argument
source "$1"
MIT License
Copyright (c) 2021 Daniel Ethridge
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@kokizzu
Copy link

kokizzu commented Apr 15, 2021

^ :3 lol'd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment