Snippets

This is a place to store quick little snippets that I like. Things that don’t deserve a whole blog post, but are helpful nonetheless.

Shell Scripts

Find the largest files on a disk:

stkerr@lore:~$ du | sort -n -r

Uses the ‘du’, or disk usage, command to return the size of all files, then pipes that to the sort command. The sort command then sorts them numerically, rather than alphabetically, and lists the largest files first. Probably helpful to pipe this output to head.

Retrieve a table of historical data from Yahoo Finance

#!/bin/bash
command='curl ichart.finance.yahoo.com/table.csv?s='
command=$command$1
command=$command'&d=2&e=30&f=2011&g=d&a=8&b=7&c=2007&ignore=.csv'
$command

Run the previous lines as a Bash script and they will pull the given stock symbol’s historical data table. Example, ‘puller.sh AAPL’ will pull Apple’s data

Grub

Manually load a kernel

find /boot/grub/stage1
configfile (hdX,X)/boot/grub/grub.conf

The first command will locate the given stage1 file. This will reveal the device name. Then use that device name in the ‘configfile’ command to load the config file.

MySQL

Import a CSV file

mysql> LOAD DATA INFILE '/home/stkerr/stocks/nasdaq.csv' INTO TABLE stocks.description FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES (exchange,symbol,name,sector,industry);

Python Snippets

Get the latest Windows error message using ctypes

from ctypes import *
def getErrorMessage():
    k32 = windll.kernel32
    error = k32.GetLastError()
    buffer = create_string_buffer(500)
    result = k32.FormatMessageA(0x1000, 0, error & 0xFFFF, 0, byref(buffer), 500, 0)
    return result

Crash Commands

Commands that I found useful for the Crash Linux kernel debugging utility

Print out an entire linked list

list -H task_struct.tasks 0xf7059c80 -s task_struct.comm

-H designates that the address is the head of the specified linked list. -s denotes the field to display and print. Traverses using the next and prev pointer to do all the recording.

Print data about the [swapper] process (pid 0)

p init_task
p init_task.tasks

Print info about a process

task 

Print a structures definition and then print an instantiation of it

struct nsproxy
struct nsproxy 

The first line prints the definition of the structure (in this case nsproxy). The second line prints the specified address, assuming it is of the specified type (so print the nsproxy struct at ).

Leave a Reply

Your email address will not be published. Required fields are marked *