Skip to content

Instantly share code, notes, and snippets.

@wlib
Last active April 30, 2024 17:07

Revisions

  1. wlib revised this gist Apr 14, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion bash_untrusted.sh
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,9 @@ function prompt {

    if [[ -n "$BASH_COMMAND" ]]; then
    # Show the command in green with a simple -> prompt
    echo -e "-> \x1B[0;32m$BASH_COMMAND\x1B[0m"
    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
  2. wlib created this gist Apr 14, 2021.
    21 changes: 21 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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.
    45 changes: 45 additions & 0 deletions bash_untrusted.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/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 -e "-> \x1B[0;32m$BASH_COMMAND\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"