======================== Miscellaneous Unix Facts ======================== -Ian! D. Allen - idallen@idallen.ca This file is an overview of some basic Unix features. It contains: * Notes on GNU and Linux * Getting Out of Programs * Allowing messages to your Terminal * Typing text into a terminal emulator * Unscrambling your Terminal * EOF and Interrupting Processes: ^D and ^C * TELNET to Unix from Windows * Notes on FTP command syntax * Locked Out of Unix/Linux Home * Understanding the different types of "sort": * Using "script" to create a session log * Line endings on Unix, Windows, and Macintosh ---------------------- Notes on GNU and Linux ---------------------- GNU - Gnu's Not Unix - GNU is a Free Software Foundation (FSF) project - rewrote Unix as free (libre) software (the way it started out, pre AT&T) - chief architect: Richard Stallman (original author of EMACS) - the GNU kernel hasn't progressed far (named HURD) - but lots of GNU utilities exist Linux "distribution" == Linux Kernel + GNU Utilities - people took the working Linux kernel and added all the Unix-compatible GNU utility software from the FSF - Linux and GNU software is released as "free software" under the General Public License (GPL) that permits free redistribution - "free" as in "libre" or "free speech", not as in "gratuit" or "free beer" - the GPL copyright license is often called "copyleft", since it is designed to *give* you rights and preserve rights, not take them away - When you install Red Hat, Mandrake, Caldera, Yellow Dog, Yggdrasil, Debian, S.U.S.E., Slackware, etc., you are installing a *distribution* ("distro") of Linux. - you're paying only for the packaging, since Linus makes no money from the use of his GPL kernel - you can redistribute what you receive (unless the distro includes non-GPL software) - the Linux programming API is compatible with the POSIX operating system standard API - source code that compiles for Unix/POSIX will compile on Linux ----------------------- Getting Out of Programs ----------------------- Getting out of Unix programs; or, getting help: Try various things such as: help ? quit Q exit x ^D (CTRL-D) ^C (CTRL-C) :q! (used in VI to exit without saving) logout bye ESC (the ESC key) . (a period) ^\ (CTRL-backslash) One of the above usually works. Sometimes you can use ^Z (CTRL-Z) to "stop" the process temporarily, and then type "kill %%" to kill it. (Remember to kill it, or it will sit there waiting forever.) ------------------------------------ Typing text into a terminal emulator ------------------------------------ When running a terminal emulator (usually in a window or by remote login), your typing is being handled by a both the Unix terminal driver and by the program reading the input. The terminal driver treats your terminal as two devices, a keyboard and a screen, that are only loosely coupled. Things you enter on the keyboard may or may not echo back on the screen. Things that appear on your screen may or may not be related to what you are typing on your keyboard. The terminal driver treats certain control characters specially: - control chars (unprintables) are written down as CTRL-H or ^H - ^? stands for the ASCII DEL character (decimal 127, octal 0177) - some commands (e.g. stty) let you enter control chars as a two-character escape sequence of ^ followed by a letter, e.g. ^H - "line edit" control characters: ^H ^? ^U ^W ^R ^L - backspace to erase one char - stty erase '^H' -or- stty erase '^?' - ^H or ^? - erase previous character (backspace) - ^U - erase entire line - ^W - erase previous word - ^R - redraw line (in case overwritten) - ^L - clear screen (in bash shell, less, more, and vim) - Interrupt a process that is running: ^C - Signal EOF (end of input) from the keyboard: ^D ---------------------------------- Allowing messages to your Terminal ---------------------------------- Other users of your computer may write messages onto your terminal if you give them permission to do so. Some programs (e.g. "talk" and "ytalk") even accept connections from the Internet. (Most Unix systems grant this permission by default.) To check the status of or change this permission, use the "mesg" command: $ mesg is y $ mesg n $ mesg is n All these messaging commands require "mesg y" to be able to write onto your terminal. Turning it to "mesg n" guarantees that you will not be disturbed while you work. When messages arrive on your terminal, they write on your screen and what you see on your screen may no longer reflect what the state of your input is, especially when using a full-screen program such as VI/VIM. In VIM command mode, ^L (Ctrl-L) will redraw your screen, erasing the message that was written onto it. -------------------------- Unscrambling your Terminal -------------------------- How to unscramble a terminal emulator that is in graphics characters set mode, where see many special and line-drawing characters instead of your typed text. (This might happen after you accidentally use "cat" to send a non-text file to your terminal screen, e.g. "cat /bin/ls" or "cat file.gz".) To Fix (you may not be able to read what you are typing!): - On a Linux system type: $ setterm -reset - On ACADUNIX try: $ reset - On either type: $ echo ^V^O (that's CTRL-V CTRL-O) The above should switch your terminal emulator back to its normal character set. Practice this now, in case it happens to you during a Lab quiz! ------------------------------ EOF and Interrupting Processes - ^D and ^C ------------------------------ To send an interrupt signal to a process that is running on your terminal, use the Interrupt Character, usually CTRL-C (^C). (You can program a different character; sometimes DEL is used.) The Interrupt usually throws away any pending input/output for the process and causes the process to terminate abnormally. Use it with caution. Interrupting a process usually terminates the process. Whatever the process was doing is left incomplete and unfinished. Buffers are not flushed; files will be incomplete. Your EOF character, signalling end of input, is usually CTRL-D (^D). You must type this at the beginning of a line (or type it twice). (You can program a different character; but, it is almost never done.) Sending the EOF character tells the process that you are finished typing at your terminal; but, it does not interrupt or terminate the process. The process will finish reading your keyboard and the continue with whatever it is doing. (Often, that means producing some output and then exiting.) Example showing how ^C interrupts a program before it finishes: $ sort >out enter some lines of text and then interrupt the program and you will get an empty file ^C $ cat out $ $ sort >out enter some lines of text and then use an EOF to signal end of file and sort will sort your data ^D $ cat out and sort will sort your data enter some lines of text and then use an EOF to signal end of file $ --------------------------- TELNET to Unix from Windows --------------------------- I don't recommend using the Windows TELNET clients - they display the screen poorly and students often forget to set up their windows correctly, which may lead to people corrupting files that they edit incorrectly. If you use TELNET under Windows, you must configure your Windows TELNET client to connect correctly to a Unix system: 1) drag TELNET window to full size (it will not expand further) 2) set terminal type: vt100 3) set lines: 24 Review the Notes file: terminal.txt ----------------------------- Locked Out of Unix/Linux Home ----------------------------- If you find yourself unable to access your home directory, with permission errors such as the following: $ ls ls: .: The file access permissions do not allow the specified action. You have probably removed either read or execute permissions from your directory. To restore these permissions for your userid, use this: $ chmod u+rwx $HOME Details on the chmod command are available in the Unix manual pages. The environment variable $HOME expands to be your home directory. You must be able to read your directory, to see what file names are in it. You must have execute permission on a directory to pass through it to any of its contents. You need both read and execute for "ls ." to work. -------------------------------------------- Understanding the different types of "sort": -------------------------------------------- The Unix sort command sorts lines by character, not by number. Explain the difference in output of these two "sort" pipelines: $ list="1 11 2 22 3 33 4 44 3 33 2 22 1 11" $ echo "$list" 1 11 2 22 3 33 4 44 3 33 2 22 1 11 $ echo "$list" | tr ' ' '\n' 1 11 2 22 3 33 4 44 3 33 2 22 1 11 $ echo "$list" | tr ' ' '\n' | sort 1 1 11 11 2 2 22 22 3 3 33 33 4 44 $ echo "$list" | tr ' ' '\n' | sort -n 1 1 2 2 3 3 4 11 11 22 22 33 33 44 (The translate command "tr" is turning blanks into newlines so that the numbers appear on separate lines on input to sort; sort only sorts lines.) Why is the sort output different in these two examples? (RTFM) -------------------------------------- Using "script" to create a session log -------------------------------------- You can create a file log of everything you type and see on your screen using the "script" command and giving it the name of a file into which it will record your session: $ script saveme.txt Script started, file is saveme.txt $ date Thu Sep 23 02:19:59 EDT 2004 $ echo hi there hi there $ who am i idallen pts/1 Sep 19 20:07 $ exit Script done, file is saveme.txt $ The file "saveme.txt" now contains a session log containing everything you typed and everything that was printed on your screen. You can use the vim editor to edit this file (to remove junk you don't want) and then you can copy it to another machine for printing. (See the Notes file file_transfer.txt for information on moving files around.) Warning: If you use a full-screen editor such as vim inside a screen session, the recorded keystrokes and screen output will be a huge mess when recorded in the session file. Make sure you edit out this mess before you print the file! Don't use full-screen editors inside a "script" session if you can avoid it. The "script" command has an option to append to a session file instead of overwriting it. (RTFM) See the BUGS section of the "script" manual page. The command "col -b" is useful for filtering out backspace and carriage-return characters from a recorded script session. (Note: The col command only reads standard input; you cannot pass it file names on the command line. RTFM) -------------------------------------------- Line endings on Unix, Windows, and Macintosh -------------------------------------------- C programmers will recognize that the line end character for Unix text files is '\n' - an ASCII newline (NL) character. (See "man ascii" for details on the ASCII character set.) Unix commands that count characters in files and lines will also count the newline character at the end of every line: $ echo hi | wc -c 3 $ echo hi >out ; echo ho >>out ; wc -c out 6 out Microsoft operating systems use two characters at the end of every line of text - the NL is preceded by an ASCII carriage return (CR). A text file containing the word "hi" contains four characters: hi A text file written on Unix contains only linefeed (LF, "\n") characters at the ends of lines; Windows expects lines in text files to end in both a carriage-return (CR, "\r") *and* a linefeed character. This may result in "staircasing" text if you send a Unix text file to a Windows printer from inside some Windows programs (e.g. Notepad). Apple computers (e.g. Macintosh) use the single character CR instead of LF at the end of every text line.