Note:
unlike the wrongly copied and continually upvoted answer provided
(points scoring?), the following script IS NON-BLOCKING, and does
not care what length returned input may be. IE it will work with
ANY screen size.
With SH it is more complex, and I was unable to find enhanced command line version of the built-in read
, eventually I found a mention of dd
on STDIN, here is the result. NOTE that the SH version of built-in echo
does not permit the use echo -en
although /bin/echo -en
does work we use printf
instead.
#!/bin/sh
x_TERM=`stty -g`
stty -icanon -echo
printf "\033[6n"
ESCPOS=""
X=""
I=0
while [ ! "$X" = "R" ]; do
X=`dd bs=1 count=1 2>/dev/null`
I=`expr $I + 1`
if [ $I -gt 2 -a ! "$X" = "R" ]; then
ESCPOS="$ESCPOS$X"
fi
done
stty "$x_TERM"
#echo "$ESCPOS"
CSRLIN=`echo "$ESCPOS" | cut -d \; -s -f 1`
POS=`echo "$ESCPOS" | cut -d \; -s -f 2`
echo "$CSRLIN"
#exit 0 <= dont use inline
I used the same code in two differnt scripts, one outputs CSRLIN
, the other POS
.
EDIT: you need to inline this script to use it in another script (eg . CSRLIN
, as the shell has to be in interactive mode.
Cheers
Paul