Fix the same issue again just for good measure
This commit is contained in:
@@ -18,10 +18,8 @@ else
|
|||||||
# Session name (leave empty to use first word of command)
|
# Session name (leave empty to use first word of command)
|
||||||
SESSION=""
|
SESSION=""
|
||||||
|
|
||||||
# Command to run (leave empty to use command line arguments)
|
|
||||||
# For multiple commands, use an array:
|
# For multiple commands, use an array:
|
||||||
# COMMANDS=("ssh root@server" "sudo su" "password123")
|
# COMMANDS=("ssh root@server" "sudo su" "password123")
|
||||||
COMMAND=""
|
|
||||||
COMMANDS=()
|
COMMANDS=()
|
||||||
|
|
||||||
# Whether to attach to session after running command (0 or 1)
|
# Whether to attach to session after running command (0 or 1)
|
||||||
@@ -32,33 +30,26 @@ EOF
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Validate required variables
|
# Validate required variables
|
||||||
if [ -z "$SESSION" ] && [ -z "$COMMAND" ] && [ ${#COMMANDS[@]} -eq 0 ]; then
|
if [ -z "$SESSION" ] && [ ${#COMMANDS[@]} -eq 0 ]; then
|
||||||
echo "Error: Either SESSION, COMMAND, or COMMANDS must be set in $CONFIG_FILE" >&2
|
echo "Error: Either SESSION or COMMANDS must be set in $CONFIG_FILE" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Use command line arguments if provided, otherwise use config
|
# Use command line arguments if provided, otherwise use config
|
||||||
if [ $# -gt 0 ]; then
|
if [ $# -gt 0 ]; then
|
||||||
COMMAND="$*"
|
COMMANDS=("$*")
|
||||||
echo "Using command line arguments: $COMMAND"
|
echo "Using command line arguments: $*"
|
||||||
else
|
else
|
||||||
if [ -n "$COMMAND" ]; then
|
if [ ${#COMMANDS[@]} -eq 0 ]; then
|
||||||
echo "Using config command: $COMMAND"
|
echo "Error: No commands provided and no commands set in config" >&2
|
||||||
elif [ ${#COMMANDS[@]} -gt 0 ]; then
|
|
||||||
echo "Using config commands: ${COMMANDS[*]}"
|
|
||||||
else
|
|
||||||
echo "Error: No command provided and no commands set in config" >&2
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
echo "Using config commands: ${COMMANDS[*]}"
|
||||||
fi
|
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 [ -z "$SESSION" ]; then
|
||||||
if [ ${#COMMANDS[@]} -gt 0 ]; then
|
|
||||||
SESSION=$(echo "${COMMANDS[0]}" | awk '{print $1}')
|
SESSION=$(echo "${COMMANDS[0]}" | awk '{print $1}')
|
||||||
else
|
|
||||||
SESSION=$(echo "$COMMAND" | awk '{print $1}')
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Create session if missing
|
# Create session if missing
|
||||||
@@ -69,20 +60,20 @@ else
|
|||||||
echo "Session $SESSION exists, reusing..."
|
echo "Session $SESSION exists, reusing..."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Send command(s) to session
|
# Send command(s) to session using temporary scripts (like tmux-daemon.sh)
|
||||||
if [ ${#COMMANDS[@]} -gt 0 ]; then
|
echo "Running commands: ${COMMANDS[*]}"
|
||||||
echo "Running multiple commands: ${COMMANDS[*]}"
|
for cmd in "${COMMANDS[@]}"; do
|
||||||
for cmd in "${COMMANDS[@]}"; do
|
|
||||||
echo "Running: $cmd"
|
echo "Running: $cmd"
|
||||||
# Use tmux send-keys with proper quoting
|
# Create temporary script to run command (preserves special characters)
|
||||||
tmux send-keys -t $SESSION "$cmd" C-m
|
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
|
sleep 1 # Brief pause between commands
|
||||||
done
|
done
|
||||||
else
|
|
||||||
echo "Running command: $COMMAND"
|
|
||||||
# Use tmux send-keys with proper quoting
|
|
||||||
tmux send-keys -t $SESSION "$COMMAND" C-m
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $ATTACH_SESSION -eq 1 ]; then
|
if [ $ATTACH_SESSION -eq 1 ]; then
|
||||||
echo "Attaching to tmux session: $SESSION"
|
echo "Attaching to tmux session: $SESSION"
|
||||||
|
Reference in New Issue
Block a user