In this unid you will go through a longer session with Unix to get to know the system and the terminal a bit better.
From the time you logged on to the system you are ”on session” until you log out. As you read this section try those commands yourself in the newly recieved shell account!
Users can display the system’s current date and time using the date command:
The date string above might look strange to you. It is the format originally chosen by the inventors of Unix and still the default format in most of the Unix-like systems. To change the format give the date command a format string as an option. Here are some examples:
Make a note of the timezone in the earlier example. In that IST stands for Indian Standard Time, which is also the time observed in Sri Lanka. IST is 5 h 30 min ahead of UTC, Coordinated Universal Time. Typically a Unix system’s hardware clock is synchronized to UTC. The system knows the local time from its timezone setting. (And if you carry a computer across timezones, you switch the timezone, not the system clock!)
The original designers of Unix have arbirtraliy chosen the instant 00:00:00 UTC of 1 January 1970 as the origin of its system time or the “epoch”. This has become a part of the Unix standard and is widely used. You can get the this epoch time in seconds by running date "+%s".
The date command can also change the system time, but only the system administrator is allowed do that. In fact, Unix systems rely much on accurate clock time and therefore almost always synchronized with a time server. But system administration is out of scope right now.
The standard calender program of Unix is called cal:
The program ncal offers you an alternative layout. Try cal -3 and ncal -3.
There is another time-related command called uptime which shows the duration for which the system has been running. In addition it tells you how many users are currently logged in and prints out three values for system load averages: the load average during the last 1, 5 and 15 minutes.
The unit of measurement for load average is the number of CPUs (or hyperthreads) being utilized. If the machine is single CPU 1.00 means the CPU is fully utilized, if it has 2 CPUs, each with two hyperthreds a value of 4.00 means that all four CPU threads are being fully utilized. (Well, there is more to it. For now we have to leave it at that.)
There is another famous command called top which continously monitors the system state:
The system’s DNS name can be querried through the command hostname. The command uname prints system information like the CPU architecture and the operating system:
Another source of system information are called environment variables. This is an area in the memory space of the shell where a list of variables and their values are stored. One can querry them with the echo command. The value of a variable is addressed by prepending its name by the $-sign:
Some other useful environment variables are: HOSTNAME, HOSTTYPE, TERM, USER. Try them!
Note that traditionally the environment variables are written in all capital.
The terminal emulator reads the keys you type as you type them immediately pass them to the (remote) shell. The keys are normally “echoed” back to the terminal unless they are suppressed as in the case of passwords. If the shell is busy with something else or the connection is very slow, you might not see the echo immediately. But you don’t need to worry. The keys strokes are put to a queue and the shell answers wenn it is back. Which means, you can type blindly ahead of the shell output.
You can think of the keys strokes and the output of the shell as two independent streams of characters. They do not get in to the way of the other!
If too much text is printed on terminal the clear command will clear it. Also watch the behaviour of the simple Enter key. In fact, it is a simple way of testing whether a shell is responding.
Programs may send special characters to the terminal to get various effects like colour or blinking text. Sometimes this leads to mishaps, where the display gets completely messed up. With the reset command you can recover, but you may have to type it blindly!
The original terminals had fixed sized fonts. Which meant that for a given resolution of the terminal the number of characters per line (called columns) and the number of lines displayed were always the same: 80 columns times 24 lines was a common standard. In today’s high resolution graphics there is no such correlation.
You can querry the terminal size through the resize command.
In dedicated terminals there was a BREAK key to stop whatever the command is running at that moment. Later this funtion was taken over by the Delete key. In todays keyboards the Delete key deletes the last character you typed. As you can see these things are system dependent. But a set of commands known as Control commands have a faily consistant behaviour throughout many systems.
You can break a running program by typing Ctrl+c. To delete a character, the equivalent to Delete, ctrl+h. ctrl+u deletes whole command line to the left of the cursor, ctrl+k kills the part to the right of it. ctrl+d signals the end of input, which will close the shell. Or, if you just want the output to pause, to keep something important disappearing off the screen, type Ctrl+s, the pause. To restart type Ctrl+q.
The following table lists some of the more important Ctrl keys.
Shortcut | Effect |
Ctrl-a | Go to the beginning of the line |
Ctrl-c | Kill the current process |
Ctrl-d | (on a line of its own) Exit the current shell |
(otherwise) delete the character under the cursor | |
Ctrl-e | Go to the end of the line |
Ctrl-h | Delete the character to the left of the cursor (backspace) |
Ctrl-k | Delete from the cursor position to the end of line |
Ctrl-l | Clear the screen |
Ctrl-u | Delete from beginning of line to cursor position |
In the previous unit we said that the command logout is the right way to log out. Strictly speaking this is not right. You can log out only from the very first shell the login process gave you. You could have started more shells on top of it. (You just start another shell by typing its name. Try bash or dash, names of available shells.) Before you log out, you need to cloase those shells. The proper way to do that is to type ctrl+d (or exit). The “exit” from the last shell will automatically logs you out!
Preparation: When capturing a screen-cast the typical GUI user makes HD films, generating huge files which are sometimes not even sharp. The shell user captures his terminal in the terminal itself. The resulting files are thousand times smaller and always sharp!
This is quite easy: There are many terminal recording tools. We will use ttyrec. Just by issuing the command ttyrec it will begin capturing everything that happens in the terminal to a file. To end the capture, type exit. You will find the recording in a file called ttyrecord. To give it a different name, simply provide the file name as a parameter. For example:
Please be careful not to press non-printable keys like arrows during the recording. They tend to upset the recording!
To play back, run:
Assignment: Login to your shell account and first go through the steps given below. Once you are confident capture your work to a file named eNNNNN_assignment2.ttyrec. If you still make a mistake, just start ttyrec again with the same file name.
Step 1. Display the system clock time.
Step 2. How many seconds have passed from 00:00:00 UTC of 1 January 1970 to the time you are doing the assignment?
Step 3. Display the output of uptime.
Step 4. Get the values of the environment variables HOSTNAME, HOSTTYPE, USER and HOME?
Step 5. Clear the screen.
Step 6. Find the size of your terminal in columns x lines. Hint: resize.
Now exit the ttyrec. Check the recording by running ttyplay. If everything is OK then go back to the Assignment in Moodle, write a note saying that you have finished the assignment. (You don’t have to upload anything, the superuser can play back the script file.)