Get terminal width for ssh motd?

Viewed 287

For my MOTD script, I've come across a need to know the width of the terminal connecting. On my computer, I (usually) have plenty of width to display my ascii banner, but on my phone, I only have x columns to work with (53 in this case). My script currently looks like this:

echo "H4sICLVncV4AA3dvbGYAfVOxkusgDOzvK9zrDe6lUeNOxWsoOJi5mdT0lPfxbxfbSZzcOxKDLaRltRLLchlJuljo/fvjuq2eEtehP+y6ienc7z0tP42Lv4SquZrJ8G7+HnH1NjhaT2YaQ6Tbr+hWwVFc3AzQPcaQ8pu/DtDvXZInUxe8SY/2i39mlj46zrAueHMRlcC3/88f0vbuZDTo3b2resvbtv3NuTSx53xNZtIoBn59IBIPPvuSvLeCg2AJN2eQpEU1VU0UJSnq4J3ZSGsRCI0Bca1Bb4HiCHTQQGCgXAu2YG5hOSWQ8y4DyCVGtNJaq6W6oKoYs1IC0h9kZkl3c1JIZhw+MxQmh8PHGCV4EoayzDZGn3xHCIIRTjvkphiUkYkiCkejYMgNFXTT6QTsaK5TIkW7IJgI7B9htnCkRjOeCAxHlJHeREiQseupsTIPmmTMUwPgOedai5RGNGJDnSPDHcUIeEyX3mnICwSRIXMBkygAy6XkmiFhweZM9wGGIkEsasNHL51udaBnwQ1yQxOXwR5pW8mtRhmtwZYezJRXQikUk7ww42iFx6A5A43XJXKNRiVxL3gKx52W6S61zZrq292vAdKoDjtIo+aMOvWKiwKjodR6p7TTszvJV6jtE/JYy1EbdqXxApQsNXKw+5Dh0XJPBTiE01c0KJk/tzoCIJJwB3Gn2pahPNCsAG5m9Cik7i0xC8J6vrK7nevtXDnuL6f5KeB2RfjGj2Nd1umNFT7rMhe+7Ob9OSzvCOv3Ebh8rRNqR1yWq3l/x/yGsPLPwPWZwbqfd5qf5lcE+PH5mgz5m+bzc18x/zlnAPwDpX8D2k4GAAA="\
| base64 -d \
| gunzip \
| cut -c 1-$(tput cols)

where cut -c 1-$(tput cols) works fine in all instances except the initial connection. I've also tried cut -c 1-$(stty size | awk '{print $2}') and cut -c 1-$COLUMNS. These all work when running the script from the terminal, but not when I initially connect via ssh.

It seems like the $COLUMNS variable isn't set until I'm connected, but is there a way to get this variable to set itself before my banner script runs?

1 Answers

How about doing a

shopt -s checkwinsize

on the remote side, before printing the motd?

Related