63 lines
1.7 KiB
Bash
Executable File
63 lines
1.7 KiB
Bash
Executable File
#!/bin/sh
|
|
#
|
|
# a minimalistic terminal muxer
|
|
|
|
# Save the original stdout and stderr file descriptors for later restoration
|
|
exec 3>&1 4>&2
|
|
|
|
# Function to check if a session is already running on the given VT
|
|
is_session_running() {
|
|
vt_number=$1
|
|
|
|
# Check if any process is running on /dev/tty<vt_number>
|
|
fuser /dev/tty"$vt_number" >/dev/null 2>&1
|
|
return $?
|
|
}
|
|
|
|
# Function to mute the output of a session
|
|
mute_session() {
|
|
vt_number=$1
|
|
if is_session_running "$vt_number"; then
|
|
# Redirect stdout and stderr of the session to /dev/null
|
|
exec > /dev/null 2>&1
|
|
fi
|
|
}
|
|
|
|
# Function to unmute the current session (restore stdout and stderr)
|
|
unmute_session() {
|
|
exec 1>&3 2>&4 # Restore stdout and stderr from file descriptors 3 and 4
|
|
}
|
|
|
|
# Function to start a new session if not already running
|
|
start_or_switch_session() {
|
|
vt_number=$1
|
|
|
|
# Mute all other sessions except the one we're switching to
|
|
for vt in $(seq 1 12); do # Assuming you have up to 12 VTs, adjust as needed
|
|
if [ "$vt" != "$vt_number" ]; then
|
|
mute_session "$vt"
|
|
fi
|
|
done
|
|
|
|
if is_session_running "$vt_number"; then
|
|
echo "Switching to existing session on VT$vt_number"
|
|
unmute_session # Unmute the session we're switching to
|
|
chvt "$vt_number"
|
|
else
|
|
echo "Starting a new session on VT$vt_number"
|
|
openvt -c "$vt_number" -- /bin/sh &
|
|
sleep 1 # Give the session a moment to start
|
|
unmute_session # Unmute the new session
|
|
chvt "$vt_number"
|
|
fi
|
|
}
|
|
|
|
# Ensure a session number is provided
|
|
if [ "$#" -ne 1 ]; then
|
|
echo "Usage: $0 <session_number>"
|
|
exit 1
|
|
fi
|
|
|
|
# Start or switch to the session
|
|
start_or_switch_session $1
|