#!/bin/bash source ~/.bashrc # Automatically add 'source ~/.bashrc' after shebang to all .sh scripts in subdirs # Usage: ./auto-prep-new-scripts.sh [--watch] SCRIPTS_ROOT="/home/intlc/projects/loc_az_hci/scripts" add_bashrc_source() { local file="$1" # Only add if not already present and if it's a bash script if grep -q "^#!/bin/bash" "$file" && ! grep -q "^source ~/.bashrc" "$file"; then awk 'NR==1{print; print "source ~/.bashrc"; next}1' "$file" > "$file.tmp" && mv "$file.tmp" "$file" echo "Patched: $file" fi } find "$SCRIPTS_ROOT" -type f -name '*.sh' | while read -r script; do add_bashrc_source "$script" done if [[ "$1" == "--watch" ]]; then echo "Watching for changes to .sh scripts..." while inotifywait -e create -e modify -e move --format '%w%f' -r "$SCRIPTS_ROOT" | grep -E '\.sh$'; do find "$SCRIPTS_ROOT" -type f -name '*.sh' | while read -r script; do add_bashrc_source "$script" done done fi