diff --git a/tmux-oneshot.sh b/tmux-oneshot.sh index de65788..c1d51cc 100644 --- a/tmux-oneshot.sh +++ b/tmux-oneshot.sh @@ -19,7 +19,10 @@ else 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) ATTACH_SESSION=0 @@ -29,8 +32,8 @@ EOF fi # Validate required variables -if [ -z "$SESSION" ] && [ -z "$COMMAND" ]; then - echo "Error: Either SESSION or COMMAND must be set in $CONFIG_FILE" >&2 +if [ -z "$SESSION" ] && [ -z "$COMMAND" ] && [ ${#COMMANDS[@]} -eq 0 ]; then + echo "Error: Either SESSION, COMMAND, or COMMANDS must be set in $CONFIG_FILE" >&2 exit 1 fi @@ -39,11 +42,14 @@ if [ $# -gt 0 ]; then COMMAND="$*" echo "Using command line arguments: $COMMAND" else - if [ -z "$COMMAND" ]; then - echo "Error: No command provided and COMMAND not set in config" >&2 + 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 exit 1 fi - echo "Using config command: $COMMAND" fi # Use session name from config, or first word of command if not set @@ -59,11 +65,22 @@ else echo "Session $SESSION exists, reusing..." fi -# Send command to session -echo "Running command: $COMMAND" -printf '%s\n' "$COMMAND" | tmux load-buffer - -tmux paste-buffer -t $SESSION -tmux send-keys -t $SESSION C-m +# Send command(s) to session +if [ ${#COMMANDS[@]} -gt 0 ]; then + echo "Running multiple commands: ${COMMANDS[*]}" + for cmd in "${COMMANDS[@]}"; do + echo "Running: $cmd" + printf '%s\n' "$cmd" | tmux load-buffer - + tmux paste-buffer -t $SESSION + tmux send-keys -t $SESSION C-m + sleep 1 # Brief pause between commands + done +else + echo "Running command: $COMMAND" + printf '%s\n' "$COMMAND" | tmux load-buffer - + tmux paste-buffer -t $SESSION + tmux send-keys -t $SESSION C-m +fi if [ $ATTACH_SESSION -eq 1 ]; then echo "Attaching to tmux session: $SESSION"