From 6d79ff93cb363cd198ea9b7d0292258fe95d24e5 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 7 Oct 2025 08:53:39 +0200 Subject: [PATCH] Fix the same issue again just for good measure --- tmux-oneshot.sh | 55 +++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/tmux-oneshot.sh b/tmux-oneshot.sh index 1fd243e..07248ec 100644 --- a/tmux-oneshot.sh +++ b/tmux-oneshot.sh @@ -18,10 +18,8 @@ else # Session name (leave empty to use first word of command) SESSION="" -# Command to run (leave empty to use command line arguments) # For multiple commands, use an array: # COMMANDS=("ssh root@server" "sudo su" "password123") -COMMAND="" COMMANDS=() # Whether to attach to session after running command (0 or 1) @@ -32,33 +30,26 @@ EOF fi # Validate required variables -if [ -z "$SESSION" ] && [ -z "$COMMAND" ] && [ ${#COMMANDS[@]} -eq 0 ]; then - echo "Error: Either SESSION, COMMAND, or COMMANDS must be set in $CONFIG_FILE" >&2 +if [ -z "$SESSION" ] && [ ${#COMMANDS[@]} -eq 0 ]; then + echo "Error: Either SESSION or COMMANDS must be set in $CONFIG_FILE" >&2 exit 1 fi # Use command line arguments if provided, otherwise use config if [ $# -gt 0 ]; then - COMMAND="$*" - echo "Using command line arguments: $COMMAND" + COMMANDS=("$*") + echo "Using command line arguments: $*" else - if [ -n "$COMMAND" ]; then - echo "Using config command: $COMMAND" - elif [ ${#COMMANDS[@]} -gt 0 ]; then - echo "Using config commands: ${COMMANDS[*]}" - else - echo "Error: No command provided and no commands set in config" >&2 + if [ ${#COMMANDS[@]} -eq 0 ]; then + echo "Error: No commands provided and no commands set in config" >&2 exit 1 fi + echo "Using config commands: ${COMMANDS[*]}" fi -# Use session name from config, or first word of command if not set +# Use session name from config, or first word of first command if not set if [ -z "$SESSION" ]; then - if [ ${#COMMANDS[@]} -gt 0 ]; then - SESSION=$(echo "${COMMANDS[0]}" | awk '{print $1}') - else - SESSION=$(echo "$COMMAND" | awk '{print $1}') - fi + SESSION=$(echo "${COMMANDS[0]}" | awk '{print $1}') fi # Create session if missing @@ -69,20 +60,20 @@ else echo "Session $SESSION exists, reusing..." fi -# Send command(s) to session -if [ ${#COMMANDS[@]} -gt 0 ]; then - echo "Running multiple commands: ${COMMANDS[*]}" - for cmd in "${COMMANDS[@]}"; do - echo "Running: $cmd" - # Use tmux send-keys with proper quoting - tmux send-keys -t $SESSION "$cmd" C-m - sleep 1 # Brief pause between commands - done -else - echo "Running command: $COMMAND" - # Use tmux send-keys with proper quoting - tmux send-keys -t $SESSION "$COMMAND" C-m -fi +# Send command(s) to session using temporary scripts (like tmux-daemon.sh) +echo "Running commands: ${COMMANDS[*]}" +for cmd in "${COMMANDS[@]}"; do + echo "Running: $cmd" + # Create temporary script to run command (preserves special characters) + tmpfile=$(mktemp) + cat >"$tmpfile" <