Other Stuff

And here's stuff that I cant fit anywhere else :-)

Currently, four items:

eval

The eval command is a way to pretend you type something directly. This is a very dangerous command. Think carefully before using it.

One way of using eval, is to use an external command to set variables that you do not know the name of beforehand. Or a GROUP of variables. A common use of this, is to set terminal-size variables on login:

eval `resize`

Backticks

There are ways to put the output of one command as the command line of another one. There are two methods of doing this that are basically equivalent:
echo This is the uptime: `uptime`
echo This is the uptime: $(uptime)
Technically, the second one is the POSIX-preferred one.

Text/color games

This is actually a huge topic, and almost deserves its own tutorial. But I'm just going to mention it briefly.

Some people may be familiar with the "curses" library. It is a way to manipulate and move around text on a screen, reguardless of what kind of "terminal" the user is using.

As mentioned, this is a potentially huge topic. So, I'm just going to give you a trivial example, and say "Go read the man-page on tput". Well, okay, actually, you have to read the "tput" manpage, AND either the "terminfo" or "termcap" manpage to figure out what magical 3-5 letter name to use. For example, it should tell you that "cup" is short for the "cursor_address" command. But you must use "cup", NOT "cursor_address", with tput.


tput init
tput clear
tput cup 3 2
print -n "Here is a clean screen, with these words near the top"
endline=`tput cols`
tput cup $(($endline - 2)) 
print "and now, back to you"
sleep 2


The above example clear the screen, prints the given line at a SPECIFIC place on the screen, then puts the cursor back down near the bottom of the screen for you.

PS: If you've been doing a lot of funky things with the screen, you might want to do a

tput reset
as the last thing before your shellscript exits.

Number-based menus

You dont have to build your own "choose a number" function: ksh has one already! But note that it returns the value of the line, not the number of the line.

 select word in one two three exit; do
	echo word is $word
	echo reply is $REPLY
	if [[ "$word" = "exit" ]] ; then
		break;
	fi
 done

This will print out a mini-menu like the following:
1) one
2) two
3) three
4) exit
#?

Note that this will loop between "do ... done" until you trigger a break somehow! (or until the user control-c's or whatever). So dont forget an exit condition!


TOP of tutorial
This material is copyrighted by Philip Brown