|
| 1 | +#!/bin/bash |
| 2 | +# Script to fix bash-preexec errors in Amazon Q shell integration |
| 3 | +# Add this to your bootstrap script to fix the error on any new installation |
| 4 | + |
| 5 | +# Create the .bashrc.d directory if it doesn't exist |
| 6 | +mkdir -p ~/.bashrc.d |
| 7 | + |
| 8 | +# Create the early-loading fix script |
| 9 | +cat > ~/.bashrc.d/00-preexec-fix.sh << 'EOF' |
| 10 | +#!/bin/bash |
| 11 | +# This file is loaded early in the .bashrc.d sequence to fix bash-preexec variables |
| 12 | +# The 00- prefix ensures it loads before other scripts |
| 13 | +
|
| 14 | +# Initialize these variables early |
| 15 | +__bp_last_argument_prev_command="" |
| 16 | +__bp_last_ret_value="0" |
| 17 | +BP_PIPESTATUS=() |
| 18 | +EOF |
| 19 | + |
| 20 | +# Make it executable |
| 21 | +chmod +x ~/.bashrc.d/00-preexec-fix.sh |
| 22 | + |
| 23 | +# Create the comprehensive fix script |
| 24 | +cat > ~/.bashrc.d/fix-bp-error.sh << 'EOF' |
| 25 | +#!/bin/bash |
| 26 | +# Fix for bash-preexec variables |
| 27 | +# This script is sourced by .bashrc |
| 28 | +
|
| 29 | +# Define these variables if they don't exist |
| 30 | +if [ -z "${__bp_last_argument_prev_command+x}" ]; then |
| 31 | + __bp_last_argument_prev_command="" |
| 32 | +fi |
| 33 | +
|
| 34 | +if [ -z "${__bp_last_ret_value+x}" ]; then |
| 35 | + __bp_last_ret_value="0" |
| 36 | +fi |
| 37 | +
|
| 38 | +if [ -z "${BP_PIPESTATUS+x}" ]; then |
| 39 | + BP_PIPESTATUS=() |
| 40 | +fi |
| 41 | +
|
| 42 | +# Override the problematic function to make it more robust |
| 43 | +__bp_preexec_invoke_exec() { |
| 44 | + # Ensure the variable is defined before using it |
| 45 | + if [ -z "${__bp_last_argument_prev_command+x}" ]; then |
| 46 | + __bp_last_argument_prev_command="${1:-}" |
| 47 | + else |
| 48 | + __bp_last_argument_prev_command="${1:-}" |
| 49 | + fi |
| 50 | + |
| 51 | + # Rest of the original function... |
| 52 | + if (( ${__bp_inside_preexec:-0} > 0 )); then |
| 53 | + return |
| 54 | + fi |
| 55 | + local __bp_inside_preexec=1 |
| 56 | + |
| 57 | + # Continue with normal execution |
| 58 | + return 0 |
| 59 | +} |
| 60 | +EOF |
| 61 | + |
| 62 | +# Make it executable |
| 63 | +chmod +x ~/.bashrc.d/fix-bp-error.sh |
| 64 | + |
| 65 | +# Check if .bash_profile exists, if not create it |
| 66 | +if [ ! -f ~/.bash_profile ]; then |
| 67 | + cat > ~/.bash_profile << 'EOF' |
| 68 | +# Initialize bash-preexec variables to prevent errors |
| 69 | +__bp_last_argument_prev_command="" |
| 70 | +__bp_last_ret_value="0" |
| 71 | +BP_PIPESTATUS=() |
| 72 | +
|
| 73 | +# Source .bashrc if it exists |
| 74 | +if [ -f ~/.bashrc ]; then |
| 75 | + source ~/.bashrc |
| 76 | +fi |
| 77 | +EOF |
| 78 | +else |
| 79 | + # If .bash_profile exists, add the initialization at the top if not already there |
| 80 | + if ! grep -q "__bp_last_argument_prev_command" ~/.bash_profile; then |
| 81 | + sed -i '1i# Initialize bash-preexec variables to prevent errors\n__bp_last_argument_prev_command=""\n__bp_last_ret_value="0"\nBP_PIPESTATUS=()' ~/.bash_profile |
| 82 | + fi |
| 83 | +fi |
| 84 | + |
| 85 | +echo "Bash preexec error fix has been installed successfully." |
| 86 | +echo "Please restart your terminal or run 'source ~/.bash_profile' to apply the changes." |
0 commit comments