============================== Miscellaneous Unix/Linux 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 * Using "script" to create a session log * TELNET to Unix from Windows * Notes on FTP command syntax * Locked Out of Unix/Linux Home * Line endings on Unix, Windows, and Macintosh * Understanding the different types of "sort": ---------------------- 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" FLOSS == Free/Libre Open Source Software - 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 Torvalds makes no money from the use of his GPL kernel software - you can redistribute what you receive in a Linux distro for free (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: h or help or ? q or Q or quit :q! (used in VI to exit without saving) x or exit ^D (CTRL-D) ^C (CTRL-C) 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. A process stopped with ^Z still has all its files open.) ------------------------------------ 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 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. While only one program is allowed to read your keyboard at once, many programs may write on your screen at once. 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 ^? ^W ^U - ^H or ^? - erase previous character (backspace) - ^W - erase previous word - ^U - erase entire line - ^R - redraw line (in case overwritten) - ^C - interrupt current (foreground) process - ^D - send EOF (end of input) to program from keyboard - ^L - clear screen (in bash shell, less, more, and VIM) - to set your backspace: stty erase '^H' -or- stty erase '^?' ---------------------------------- 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 -------------------------- Most terminals and terminal emulators can be switched into a "graphics" mode by sending them a special sequence of escape characters. Sometimes this happens when you don't want it, e.g. you display a binary format file on your screen. Here is 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 reading your keyboard that you are finished typing at your terminal (EOF); 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 $ -------------------------------------- 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 raw.txt Script started, file is raw.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 raw.txt $ $ col -b saveme.txt After you exit the shell started by "script", the file "raw.txt" will contain a raw session log containing *everything* that was sent to your screen, printable or unprintable. Filtering is usually needed. The command "col -b" is useful for filtering out backspace and carriage-return characters from a recorded script session: $ col -b saveme.txt Note: The col command only reads standard input; you cannot pass it file names on the command line. RTFM Warning: If you use a full-screen editor such as VIM inside a screen session, the screen output will be a huge mess when recorded in the session file due to all the unprintable characters that VIM uses to draw characters on your screen. 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. --------------------------- 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. -------------------------------------------- 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. -------------------------------------------- Understanding the different types of "sort": -------------------------------------------- The Unix sort command sorts lines by character, not by number. Explain the difference in output of the two "sort" pipelines, below. (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.) $ 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 Why is the sort output different in these two examples? (RTFM)