Fix the same issue again just for good measure

This commit is contained in:
2025-10-07 08:53:39 +02:00
parent db83cbbffc
commit 6d79ff93cb

View File

@@ -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
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
# Send command(s) to session using temporary scripts (like tmux-daemon.sh)
echo "Running 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
# Create temporary script to run command (preserves special characters)
tmpfile=$(mktemp)
cat >"$tmpfile" <<EOF
#!/bin/bash
$cmd
EOF
chmod +x "$tmpfile"
tmux send-keys -t $SESSION "$tmpfile" 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
done
if [ $ATTACH_SESSION -eq 1 ]; then
echo "Attaching to tmux session: $SESSION"