Showing posts with label tput. Show all posts
Showing posts with label tput. Show all posts

Friday, October 31, 2008

11 Simple Ways To Recover Your Screen on Linux and Unix

Hey there,

If you're one of the many readers who submit a comment now and again (and even if you aren't ;), today's post is an homage to your helpful and insightful suggestions and tips. Some of you reading this right now may have emailed me with something that deserves to be posted as a comment to another post, and I can say, with great confidence, that it eventually will. I personally respond to all emails, so sometimes balancing that with work, family and more work can cause me to get a little behind. I'm still going to keep doing things this way, of course, not because I'm lazy or narrow-minded (okay, that's up for debate ;), but because I believe in actually responding to anyone who takes the time to send me a comment. And, just to clarify, this response-ethic of mine does not apply in any way shape or form to folks who write me one sentence vulgarities. If you want (or need) to hurl an insult my way, I strongly encourage it, but I probably won't write you back. Like with every other cycle, no matter who started it, someone has to end it :)

And, now, just to keep you on your toes, I'll include a hyperlink back to the original article on 9 simple ways to recover your screen in the same sentence that I let you know that this post is virtually the same material, only bigger, better and with extra sauce ;) Unless you want to read the rambling introduction (which is different in the original post), don't bother visiting the original page. All of the useful stuff from that post is included in today's version.

Once, again, a super-sized Thanks to everyone who wrote in with suggestions for this post and any other post. I truly appreciate your taking the time to write in and am almost always impressed, after a reading, with how much I don't know ;)

And, here we go again. Attribution for reader suggestions will accompany those points; otherwise, this post - from here on out - will be almost exactly the same as the last one.

Have a great weekend!

1. See if you can clear the screen the obvious way

host # clear

2. Or maybe resetting it might help

host # reset

3. stty is also usually available on most Linux and Unix distributions, and it has a sanity check built in. Worth a shot:

host # stty sane

4. tput is also fairly common. You can use it to clear your screen, or reset it, almost like the first two options did (just from a different angle):

host # tput clear

5. and...

host # tput reset

6. Or you can use it to re-initialize the screen (gross oversimplification, but you just want your screen back, right? ;)

host # tput init

7. Finally, you can try to get it to sort out the garbage on your screen by resetting the bold and underline definitions (this makes no sense to me at all, but it works sometimes;)

host # tput sgr 0 0

8. If your shell built-in echo can do this, it's fairly easy to take this route:

host # echo -e \033c

9. And, if it doesn't (or you just can't tell because your characters are still all goofed), you can type the control characters directly:

host # echo ^[c <-- Which, if you don't want to check out the link above, translates to typing: echo and then ctl-v followed by hitting the escape key and then typing the regular letter c

10. Suggested by Matthew Lenz! Here's a very simple way to get back your screen. Why does it work? I have no idea, but I tested it and it does on every machine I could try it out on :

host # cat /bin/cat

11. Suggested by James Ranks! One of these control key combinations will almost always work for you and remove the garbage from your screen (see point 9 for a link back on how to really represent control characters in Linux or Unix) :

host # ctrl-v followed by ctl-n

or

host # ctrl-v followed by ctl-o

Hopefully this little cheat sheet will come in handy at some time. You'll most likely be typing "in the dark."

Cheers,

, Mike



zcat made this comment regarding the post. It illustrates a good point: If things get too hairy, you have to know when to just bail and fix your terminal the old-fashioned way :)

I've catted plenty of binary files in the past and never needed anything
beyond 'reset' to recover from it. Perhaps I just got lucky.

But if I ever did need more than a reset, I think I would probably just
log out and back in at that point; the advantage of using a short
username like 'zcat' (and on the hosts where it's my full name I use
key-based authentication so I never have to type it)







Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Friday, August 22, 2008

9 Simple Ways To Recover Your Screen on Linux and Unix

Hey there,

Wow, it's been a long work week. Figuratively, of course. The official version is that it was a standard 5 days ;) Tonight's post is a bit on the lighter side (both the material covered and the overall tone), as I haven't had much time to write this week. It's like my wife says; I should take some of the 1500 word rants I put down and split them up every once in a while. She's a smart lady. One of these days I'll start listening to her and take it easy on my keyboard ;)

Hopefully, today's title didn't give anyone the wrong impression. If you've gone and thrown your monitor off the balcony in a fit of rage, this post can't help you, except to offer the following advice: Go to the store and buy a buy a new one (they're much cheaper now than when you may have bought yours, which will help offset the counseling, therapy and court-ordered community service expenses ;) Then make a solemn resolution. Promise yourself that, the next time you work all night on a complex program that, even to you, barely makes any sense by the time you've accidentally deleted random chunks of it before remembering to write the buffer to disk, you will take 5 seconds, walk away, huff in a deep deep breath, count to 10 and then smash it with a brick, instead. You'll be as happy as you were last time, but you probably won't get arrested ;)

If you're a pragmatist, take a moment to reflect on the fact that (although this is a very interesting phenomena that I experience from time to time), even though you desperately want to smash your monitor into bits, it has absolutely nothing to do with the problem. For instance, I often whack my monitor on the side when Windows takes 45 minutes to boot up. I should be smacking the OS install disks, or the computer itself. If I was in full possession of my faculties (or, at least, my sense of reason) I would be kicking my own arse for expecting anything more ;)

Alas, enough pontificating. To get your weekend started off right, here are 9 ways you can, hopefully, get your terminal output back to normal if you ever cat a binary executable or do anything else that makes it all turn "Chinese" on you. Enjoy and have a safe weekend :)

Start out with the classics (NOTE: All examples will be given as command line mock-ups which end in a carriage return or enter-key press - I'm assuming you'll be typing blind)

1. See if you can clear the screen the obvious way

host # clear

2. Or maybe resetting it might help

host # reset

3. stty is also usually available on most Linux and Unix distributions, and it has a sanity check built in. Worth a shot:

host # stty sane

4. tput is also fairly common. You can use it to clear your screen, or reset it, almost like the first two options did (just from a different angle):

host # tput clear

5. and...

host # tput reset

6. Or you can use it to re-initialize the screen (gross oversimplification, but you just want your screen back, right? ;)

host # tput init

7. Finally, you can try to get it to sort out the garbage on your screen by resetting the bold and underline definitions (this makes no sense to me at all, but it works sometimes;)

host # tput sgr 0 0

8. If your shell built-in echo can do this, it's fairly easy to take this route:

host # echo -e \033c

9. And, if it doesn't (or you just can't tell because your characters are still all goofed), you can type the control characters directly:

host # echo ^[c <-- Which, if you don't want to check out the link above, translates to typing: echo and then ctl-v followed by hitting the escape key and then typing the regular letter c

Hopefully this little cheat sheet will come in handy at some time. You'll most likely be typing "in the dark." If none of these options work, I usually give in and just disconnect and re-login (It's amazing how much work goes into being lazy ;)

Cheers,

, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Tuesday, April 1, 2008

Script For A Simple Menu Using Tput And ANSI Color

Simple Menu With An Option Selected

Click above to see a larger example of what happens when you dare to select option 1 ;)

Hey there,

Today I thought we'd look at something that almost all admins have to do at some point or another; create a simple shell menu script for users who aren't Unix or Linux savvy, but need to make use of the systems directly, nonetheless.

To spice it up (a little. There's only so much you can do ;) I've used colors within the standard shell menu framework, sticking with the ECMA compliant ANSI color codes. The reason I choose to stand by these color codes (since they, admittedly, lack the range of colors available in different flavors of Linux and Unix) is that they're virtually guaranteed to work on "all" flavors of Linux and Unix. Or maybe I'm just a people pleaser who hasn't realized that he can't win 'em all yet ;)

You'll notice also that, rather than just throw the menu together, using "case" inside a "select" loop, I went out of my way to complicate things by using tput to manage the cursor on the screen. In this case, it actually adds some functionality to the menu that would, otherwise, be impossible to implement. I shied away from unnecessarily using "getopts" when our "case" statement can handle these simple menu arguments just as efficiently :).

You can run this menu very simply, from the command line, like so:

host # ./SimpleMenu.sh

And there are only a few things to really take note of today. In a future post, I'll devote more time to utilizing your terminal screen with tput. It can be used to do a lot more useful, and entertaining, things than what we're having it do today.

Basically, in quick sequential order, we're using tput to:

1. Make the cursor invisible : tput civis
2. Position the cursor at various places on the terminal: tput cup x y <--- With x and y being single numeric coordinates designating the row and column, respectively.
3. Save the current cursor position: tput sc
4. Get back to (recover) the previously saved cursor position: tput rc
5. Make everything go back to the way it was, just in case our screen gets screwed up: tput reset

You can also, very simply, modify this shell menu script by just changing the menu options and adding your own routines to the case loop. And, if you can find something less offensive to your sensibilities, please do change the color scheme ;)

Enjoy :)


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash
#
# SimpleMenu.sh - Only SomeWhat Useful ;)
#
# 2008 - Mike Golvach - eggi@comcast.net
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

trap 'tput reset;exit 1' 1 2 3 9 15

tput civis

while :
do
echo -e "\033[44;37;22m"
clear
echo -e "\033[41;37"

echo -ne "\033[46;30m"
tput cup 8 25 ; echo -n "The Linux and Unix Menagerie"
tput cup 9 25 ; echo -n " Shell Account Services "
tput cup 10 25 ; echo -n " W E L C O M E "
echo -e "\033[40;32m"

tput cup 12 25 ; echo -n " 1. See Who's Logged In "
tput cup 13 25 ; echo -n " 2. Reboot The Server "
tput cup 14 25 ; echo -n " 3. Gain Root Access "
tput cup 15 25 ; echo -n " 4. Call Your Mother "
tput cup 16 25 ; echo -n " 5. Quit "

echo -ne "\033[41;30m"
tput cup 18 28 ; echo -n " Pick Your Poison: "
tput cup 18 48
tput sc

read y
tput cup 20 30

case "$y" in
1|S|s)
output=`w|awk '{print $1}' |xargs -ibing grep bing /etc/passwd|awk -F":" '{print $5}'`
starting_line=21
tput cup $starting_line 28;
echo -ne "\033[46;30mCurrently Logged In Users:"
echo -ne "\033[0m"
echo "$output"|while read x
do
let starting_line=$starting_line+1
tput cup $starting_line 28;
echo -n $x
done
tput rc
echo -n "Any Key To Menu"
read x
;;
2|R|r)
tput cup $starting_line 28;
echo -n "Option Not Active Yet"
tput rc
echo -n "Any Key To Menu"
read x
;;
3|G|g)
tput cup $starting_line 28;
echo -n "Option Not Active Yet"
tput rc
echo -n "Any Key To Menu"
read x
;;
4|C|c)
tput cup $starting_line 28;
echo -n "Option Not Active Yet"
tput rc
echo -n "Any Key To Menu"
read x
;;
5|Q|q)
tput reset
clear
exit
;;
*)
tput cup 20 28
echo "$y - Unknown Option"
tput rc
echo -n "Any Key To Menu"
read x
;;
esac
done

, Mike