#!/bin/sh # Writes a message ($2 ...) to the most recently active terminal of user $1 # Unlike write, it also works on xterms. The most recently active one is # determined by comparing device file modification times (equivalent to most # recent output). # list of tty's on which user is logged in: LIST=`who | awk '/^'$1'[[:space:]]*tty/ {print "/dev/"$2}'` # debugging: #echo >> /tmp/writex.debug #echo "tty list: $LIST" >> /tmp/writex.debug # in the case of X login, add pseudo terminals (X terminals): if who | grep '^'$1'[[:space:]]*:0' &> /dev/null; then shopt -s nullglob LIST=`echo $LIST /dev/pts/*` fi #echo "complete list: $LIST" >> /tmp/writex.debug if [ -z "$LIST" ]; then echo "writex: $1 is not logged in!" exit 1 fi # find most recently active terminal: TERMDEV= for term in $LIST; do if [ -z "$TERMDEV" -o "$TERMDEV" -ot $term ]; then TERMDEV=$term fi done shift echo $@ > $TERMDEV