Sunday, September 24, 2006

Linux Command Line Odds and Ends

Here are some useful Odds and Ends... most are related to command line stuff, some not; whatever, enjoy. Most of these came from either scouring the web, or Learning the Bash Shell or Learning Red Hat Enterprise Linux and Fedora.

Job Control
  • kill %<PID> kill a process
  • kill -QUIT %<PID> kill a process, a bit stronger
  • kill -KILL %<PID> unconditionally kill a process
  • fg bring a background job into the foreground
  • jobs list jobs running
  • ps process information

File permissions (owner, group, others)
  • 0 ---
  • 1 --x
  • 2 -w-
  • 3 -wx
  • 4 r--
  • 5 r-x
  • 6 rw-
  • 7 rwx

Globbing
  • * matches zero or more characters
  • ? matches any one character
  • [abc...] matches any of the characters specified
  • [a-z] matches any character in the specified range
  • [!abc...] matches any character other than those specified
  • [!a-z] matches any character not in the specified range
  • ~ home directory of current user
  • ~userid home directory of a user
  • ~+ current working directory
  • ~- previous working directory

Quotes
  • 'xxx' interprereted literally, variables not substituted
  • "xxx" interprereted literally, variables ARE substituted
  • `xxx` output of xxx command replaces it


Command line special characters
  • # comment
  • ; command seperator
  • & run in background
  • \ command continued on next line
  • | pipe

Input/Output Redirectors
  • prog > file stdout to file
  • prog 2> file stderr to file
  • prog >> file concatenates stdout to file
  • prog 2>> file concatenates stderr to file
  • prog > file 2>&1 stdout and stderr to file
  • prog >> file 2>&1 concatenates stdout and stderr to file
  • prog < file stdin from file
  • prog << text reads stdin until a line matching text is found, then EOF posted ("here document")
  • prog | prog2 pipe stdout
  • prog 2>&1 | prog2 pipe stdout and stderr

Command Line Movement
  • Ctrl+Shift+N open new console window
  • Crtl+Alt+F[1-7] go to virtual console 1-6 or X(7)
  • Ctrl+Alt+Backspace stop X and go to console
  • Alt+B Back one word
  • Alt+F Forward one word
  • Ctrl+A Beginning of line
  • Ctrl+E End of line
  • Alt+D Delete word |------------->X
  • Ctrl+D Delete char
  • Ctrl+K Delete |------------->X
  • Ctrl+U Delete X<-----------|
  • Ctrl+L Clear screen
  • Ctrl+Y UNDO
  • ESC+. Insert last word of previous command
  • TAB Possible completions

IRC
  • /server join a server
  • /join join a channel
  • /quit quit the server
  • /close close the current screen
  • /part leave the current channel
  • /partall leave all channels
  • /msg msg a user with a new window
  • /notice msg a user without a new window
  • /query force a window open to msg a user
  • /chat DCC with a user
  • /dns dns lookup for a user
  • /ping ping a user
  • /me *** does something
  • /whois query whois for a user

Ctrl Keys
  • Ctrl+C intr: stop current command
  • Ctrl+D eof: end of input
  • Ctrl+\ quit: stop current command (if Ctrl+C doesn't work)
  • Ctrl+S stop: halt output to screen
  • Ctrl+Q resume output to screen
  • Ctrl+Z suspend current command (works well with bg, fg and jobs)

Escape Sequences
  • \a alert (bell)
  • \b backspace
  • \c omit final newline
  • \E escape character
  • \f formfeed
  • \n newline
  • \r return
  • \t tab
  • \v vertical tab
  • \xxx ASCII in octal
  • \\ backslash
Tar: tarball options
  • -c create
  • -r append
  • -t list contents
  • -x extract
  • -a append files
  • -v verbose
  • -z zip/unzip
  • -f use filename
  • Oft-used:
    • tar -cf foo.tar foo; gzip foo.tar
    • gunzip bar.tar.gz; tar -xvf bar.tar

Saturday, September 23, 2006

Python Bookmarks, Tricks, Tips, Gotchas

Python = Best Language EVAR! 'nuff said. Here are some Bookmarks, Tricks, Tips, and Gotchas that I've compiled:

BOOKMARKS

TRICKS

"dictionary comprehensions"
>>> dict((x, None) for x in [1,2,3])
{1: None, 2: None, 3: None}

Running a script in an interactive session
>>> import sys
>>> sys.argv = ["spam.py", "one", "two", "three"]
>>> execfile("spam.py")

Ternary if else statement (2.5 only)
x = true_value if condition else false_value

"switch" statement using dictionary
def function_1 (...):
...
functions = {'a': function_1,
'b': function_2,
'c': self.method_1, ...}
func = functions[value]
func()

list comprehension with multiple ifs/fors
>>> result = [x for x in range(10) if x % 2 == 0 if x % 3 == 0]
>>> result
[0, 6]
>>> result = [ x*y for x in range(10) if x%2 == 0 for y in range(10) if y % 3 == 0]
>>> result
[0, 0, 0, 0, 0, 6, 12, 18, 0, 12, 24, 36, 0, 18, 36, 54, 0, 24, 48, 72]

turn a list into a string and back again
>>> ' '.join(['a', 'b', 'c'])
'a b c'
>>> _.split()
['a', 'b', 'c']

more "mathy" numeric boolean comparison
>>> 4<6<8>>> 1<2<3<4<5>4>3>2>1
True

any() and all() (2.5 only)
>>> any([False, False, False])
False
>>> any([False, False, True])
True
>>> all([True, True, False])
False
>>> all([True,True,True])
True

switch values without a temporary variable
>>> a,b = b,a

Drop into a python debugging session when you hit this line
import pdb ; pdb.set_trace()

Zen of Python easter egg:
>>> import this

Reverse a string:
>>> 'string'[::-1]
'gnirts'

Quicksort in three lines of code (pure propaganda piece, don't use in real code... from Python Cookbook, p.215)
def qsort(L):
if len(L) &lt;= 1: return L
return qsort([lt for lt in L[1:] if lt &lt; L[0]]) + L[0:1] + qsort([ge for ge in L[1:] if ge >= L[0]])

Create a 3-D array initialized to 1(when working with large arrays, always use Numeric)
import Numeric as N
a = N.empty(2*3*4)
for i, x in enumerate(a): a[i] = 1
a = N.reshape(a, (2,3,4))

Enumerate and zip at the same time
for (i,x),y,z in zip(enumerate(a),b,c): print i,x,y,z

TIPS (code samples taken from Dive Into Python)
  • use k in d instead of k in d.keys() for checking for a key in a dictionary (the former is a built-in call to C code and a O(1) operation rather than a Python module with O(n)
  • enumerate(seq) returns index/value pair for a sequence: use it in for loops when you need both values (NEVER use len(range(seq)))
  • Introspection: dir(obj) returns a list of all attributes of obj; vars(obj) returns a dictionary of the same (incl. the values of those attributes); don't forget about locals() and globals(), either
  • isinstance(obj, class) tells whether obj is an instance of class... use it instead of if type(X) == type(Y)
  • callable(obj) returns boolean indicating weather the object is callable or not
  • getattr(x, 'y', default) is the reflection-y way of doing x.y
  • Assign multiple values at once: x , y, z = 1, 'blah', function
  • Python supports filename globbing: use the glob function in the glob module
  • Regular expressions: always use raw strings to avoid excessive backslashing
    • re.sub(r'\bROAD$', 'RD', stringToModify) substitution
    • re.compile for more complicated/reoccuring tasks
      • phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})$')
      • phonePattern.search('800-555-1212').groups()
    • re.search(r'[^aeiou]y$', 'vacancy') search
  • You can pass each item of a sequence (instead of the entire sequence itself) into a function that supports it (i.e. defined such as def fn(*x)) by calling it: fn(*seq) If seq is a dict, the function call passes in each key.
  • Change a bunch of non-object variables by passing them into a function and returning them all in a tuple
  • Use the timeit module for small code snippets
  • If you want "true" division, (5/3 = 1.67 instead of 1) use 5//3 or add from __future__ import division to the beginning of your module
  • Use sys.stdout.write() instead of print to avoid trailing characters when writing to stdout
  • You can create a dictionary that acts like a list by using integers (0...n) as keys
GOTCHAS
  • Python one-liners are tricky (usually in form python -c "code"... see this thread
  • __init__ doesn't "construct" something... the object has already been constructed by the time the interpreter hits __init__
  • Keys of dictionaries must be hashable... that means lists as keys are right out
  • EVERYTHING in python is an object (try it at the interpreter if you don't believe me)
  • self should always be the first argument for every function in a class. You must bind all passed in variables (self.value = value) to make them attributes of the class (preferably in __init__). Refer to class attributes within a class as self.value.
  • When you add a value pair to a dictionary whose key already exists in the dictionary, the new value pair replaces the old one
  • Adding __ to a class attribute (e.g. __attributeX) makes it module-private
  • Don't use optional arguments to initialize mutable objects! Any optional arguments (e.g. def f(x, y=[])) get assigned during evaluation, not execution. Use this form instead: def(x, y=None): if y is None: y = []
  • Python raises an exception when __init__ returns anything other than None
  • a = b and a = copy.copy(b) both assign a to the same object as b (shallow copy). For a deep copy, use copy.deepcopy
  • os.walk doesn't change the current directory, so using something like os.path.abspath(filename) will NOT work... use os.path.join(top, dirpath, filename) instead
  • comparing a string to a integer works! (when operations do not!)
  • If you get a RuntimeError: dictionary changed size during iteration, try changing for x in y to for x in y.keys()
  • More gotchas: 10 Python Pitfalls, Python Gotchas, When Pythons Attack

Thursday, September 21, 2006

Big Day

2 things happened today:

1) Got my awesome, awesome Dell 2407 monitor:















2) Ordered some parts off of newegg so I can use this montor at more than the crappy resolution of my laptop (and, y'know, for some other functionality...):
  • AMD X2 64-bit 3800+ CPU
  • Seagate 320G hard drive
  • Cheap LiteOn DVD burner
  • Cheap HP Deskjet printer
  • Cooler Master case (with 430W power supply)
  • Asus A8N5X Motherboard
  • PNY GF6600 AGP 256M Graphics Card
  • 2G Corsair RAM
And, all the accessories:
  • A blue keyboard with a superman logo emblazoned on it
  • Logitech (best company ever for cheap and excellent peripherals) optical mouse
  • Buffalo Wireless PCI card
  • Arctic Silver Thermal Paste (I never use the cheap stuff that comes on the cooling unit)
  • Power strip
All parts (save the monitor) clocked in at under 1000 bucks, including shipping and taxes. Can't wait to get my dirty little paws on it next week...

Update: screwed up on some stuff... RMAed the video card because it was the wrong interface (stupid! stupid!)... got a VGA XFX|GF 7300GS 256MB PVT72GWANG to replace it... and that keyboard is slim and lame... got a stripped down logitech one to replace it.

Thursday, September 14, 2006

GNU Coreutils/other useful UNIXy stuff

There are a lot of useful things in the GNU coreutils that are useful but many people (me) had no idea that they even exist... here are a few:
  • sort sort the contents of a file
    • -r reverse
    • -f ignore case
    • -n numeric
    • -b ignore leading blanks
    • -c check if sorted (no printout = sorted)
  • uniq remove duplicate lines from a sorted file
    • -c sort and count number of occurances
    • -i ignore case
Of course, if you ever want any information about the coreutils: info coreutils

Ubuntu System Beep

Hate the system beep? Get rid of it with rmmod pcspkr
Nostalgic for the system beep? Bring it back with modprobe pcspkr

Tip from: Lifehacker's top 10 Ubuntu tips and tweaks

Also, you can find it in GNOME at System->Preferences->Sound->System Beep

Wednesday, September 06, 2006

Everything you've ever wanted to know about GNOME

About.com's GNOME Desktop User Guide

Some useful keyboard shortcuts from another About.com page:
  • Alt+Tab Switch between windows. A list of windows that you can select is displayed. Release the keys to select a window. You can press the Shift key to cycle through the windows in reverse order.
  • Alt+F4 Close the currently focused window.
  • Alt+F5 Unmaximize the current window, if it is maximized.
  • Alt+F7 Move the currently focused window. After pressing this shortcut, you can move the window using either the mouse or the arrow keys. To finish the move, click the mouse or press any key on the keyboard.
  • Alt+F8 Resize the currently focused window. After pressing this shortcut, you can resize the window using either the mouse or the arrow keys. To finish the resize, click the mouse or press any key on the keyboard.
  • Alt+F9 Minimize the current window.
  • Alt+F10 Maximize the current window.
  • Alt+spacebar Open the window menu for the currently selected window. The window menu allows you to perform actions on the window, such as minimizing, moving between workspaces, and closing.
  • Shift+Ctrl+Alt+Arrow keys Move the current window to another workspace in the specified direction.
  • Ctrl++ enlarge the screen/text
  • Ctrl+- shrink the screen/text
All of these settings are customizable: go to System->Preferences and there's a bunch of menus there that you can use to trick out your setup (such as Keyboard Shortcuts). I set those annoying windows keys that are next to the alt keys to actually mean something in linux. The right one (Super_R) I use to hide all the windows on the screen and the left one (Super_L) I use to launch a terminal.

UPDATE: To get Nautilus in GNOME to have the location bar (like default in KDE): Open a Nautilis window->Edit->Preferences->Behavior->Always use text-entry location bar ... There's a host of other useful display/behavior options there, too.

Tuesday, September 05, 2006

Back to school

Since I've been back at college, I've been using some things that I don't use otherwise... it's quite a different world here than in Silicon Valley.
  • Spellcheck
    • aspell -c <file> spellcheck a file
    • aspell -a interactive spellchecker
  • PDF-related
    • evince &lt;file> to launch a GUI PDF viewer
    • pdftohtml &lt;file> to create three files, one which contains a table of contents, another which contains the text, and a third which combines both with frames
    • gs ghostscript
    • apropos pdf list things that your system can do with PDF files... there's a lot of them!
  • How to open a new tab in a GNOME terminal (How is this back to school? Well, I'm trying to eliminate all mouse usage now that I'm using this laptop everywhere...)
    • Ctrl+Shift+T Open tab
    • Ctrl+Shift+W Close tab
    • Ctrl+PageUp/Down select Previous/Next tab
    • Ctrl+Shift+PageUp/Down move terminal tab to the left/right
    • Alt+&lt;number> select specific terminal tab by number
Some things I'd like to learn (anyone out there know?):
  • How to insert unicode characters in vi
  • Invoke a spellchecker from within vi