Fix more of the same issue
This commit is contained in:
@@ -2,6 +2,40 @@
|
|||||||
# tmux-oneshot.sh
|
# tmux-oneshot.sh
|
||||||
# Run a single command in a tmux session
|
# Run a single command in a tmux session
|
||||||
|
|
||||||
|
# Built-in prompt patterns (command_pattern:prompt_pattern:timeout_seconds)
|
||||||
|
PROMPT_PATTERNS=(
|
||||||
|
"ssh*:Password|password:5"
|
||||||
|
"sudo*:Password|password|\\[sudo\\] password for:5"
|
||||||
|
"su*:Password|password:5"
|
||||||
|
"mysql*:Enter password:3"
|
||||||
|
"docker*:Password:2"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Generic function to wait for prompts based on command patterns
|
||||||
|
wait_for_prompt() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
# Find matching pattern for this command
|
||||||
|
for pattern in "${PROMPT_PATTERNS[@]}"; do
|
||||||
|
IFS=':' read -r cmd_pattern prompt_pattern timeout <<< "$pattern"
|
||||||
|
if [[ "$cmd" == $cmd_pattern ]]; then
|
||||||
|
echo "Waiting for prompt: $prompt_pattern (timeout: ${timeout}s)"
|
||||||
|
timeout=$((timeout * 10)) # Convert to 0.1s intervals
|
||||||
|
while [ $timeout -gt 0 ] && ! tmux capture-pane -t $SESSION -p | tail -3 | grep -E "($prompt_pattern)" >/dev/null 2>&1; do
|
||||||
|
sleep 0.1
|
||||||
|
timeout=$((timeout - 1))
|
||||||
|
done
|
||||||
|
if [ $timeout -eq 0 ]; then
|
||||||
|
echo "Timeout waiting for prompt: $prompt_pattern"
|
||||||
|
fi
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# No pattern matched, brief pause
|
||||||
|
sleep 0.5
|
||||||
|
}
|
||||||
|
|
||||||
# Load config file if it exists
|
# Load config file if it exists
|
||||||
SCRIPT_DIR=$(dirname "$0")
|
SCRIPT_DIR=$(dirname "$0")
|
||||||
SCRIPT_NAME=$(basename "$0" .sh)
|
SCRIPT_NAME=$(basename "$0" .sh)
|
||||||
@@ -60,19 +94,14 @@ else
|
|||||||
echo "Session $SESSION exists, reusing..."
|
echo "Session $SESSION exists, reusing..."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Send command(s) to session using temporary scripts (like tmux-daemon.sh)
|
# Send commands individually with proper timing
|
||||||
echo "Running commands: ${COMMANDS[*]}"
|
echo "Running commands: ${COMMANDS[*]}"
|
||||||
for cmd in "${COMMANDS[@]}"; do
|
for cmd in "${COMMANDS[@]}"; do
|
||||||
echo "Running: $cmd"
|
echo "Running: $cmd"
|
||||||
# Create temporary script to run command (preserves special characters)
|
tmux send-keys -t $SESSION "$cmd" C-m
|
||||||
tmpfile=$(mktemp)
|
|
||||||
cat >"$tmpfile" <<EOF
|
# Wait for command to complete or prompt for input with timeout
|
||||||
#!/bin/bash
|
wait_for_prompt "$cmd"
|
||||||
$cmd
|
|
||||||
EOF
|
|
||||||
chmod +x "$tmpfile"
|
|
||||||
tmux send-keys -t $SESSION "$tmpfile" C-m
|
|
||||||
sleep 1 # Brief pause between commands
|
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ $ATTACH_SESSION -eq 1 ]; then
|
if [ $ATTACH_SESSION -eq 1 ]; then
|
||||||
|
Reference in New Issue
Block a user