Sunday, May 29, 2011

Bash script cursor control

taken from:
http://bashscript.blogspot.com/2010/04/how-to-handle-cursor-movement-in-shell.html


The movement escape sequences are as follows:

        - Position the Cursor:
          \033[<L>;<C>H
             Or
          \033[<L>;<C>f
          puts the cursor at line L and column C.
        - Move the cursor up N lines:
          \033[<N>A
        - Move the cursor down N lines:
          \033[<N>B
        - Move the cursor forward N columns:
          \033[<N>C
        - Move the cursor backward N columns:
          \033[<N>D
        - Clear the screen, move to (0,0):
          \033[2J
        - Erase to end of line:
          \033[K
        - Save cursor position:
          \033[s
        - Restore cursor position:
          \033[u
example:
echo -en "\033[s\033[7B\033[1;34m BASH BASH\033[u\033[0m" 
 
Script to print position of a file in a process:
 
prints on line 2, column 1 
 
#!/bin/bash

/bin/echo -en "\033[2J"
while [ 1 ] ; do
/bin/echo -en "\033[2;1H"
echo `cat /proc/5648/fdinfo/1|grep pos`
echo $a
done
 

copying 2tb drive

On linux, one can follow the advise of the majority of the hits which suggest using dd to clone a disk, but it seems that adding a blocksize to the dd is very useful.

I looked at the stream being read w/o a parameter, and found that it was at bs=512, which is the default.  After about 3 hours of copying the dd had copied 331gb. 

I restarted with a bs=20480000 or about 20mb and in 20 minutes the copy is up to 120gb copied, or somewhat over 5 percent, suggesting that it will copy complety in 200min or about 3 hours total.

bash command line shortcuts

stolen from:bash
http://www.simplehelp.net/2009/01/20/some-useful-linux-bash-tricks/


I use the Linux command line extensively, both on my local machine or when I’m connected to a remote server. There are some keyboard shortcuts that I want to share with you which have made my life on BASH a lot easier. I have not come across too many books that have documented these shortcuts and I think they are quite important to improve your productivity while on the command line.
1. Ctrl + r
This keyboard shortcut is for running a reverse search in your command line. To use this shortcut hold down the “Control” key in your terminal interface and hit the “r” while the “Control” key is still pressed. you should get a message like this:
(reverse-i-search)`’:
Now begin typing a command you used earlier. BASH will automatically complete your command. You can keep hitting the “Ctrl + r” combination to search further.
2. Ctrl + a
This shortcut makes your cursor jump to the beginning of the line. When you have typed a few works in your terminal hit the key combination of “Control + a” and you will reach the beginning of the line you are typing in.
3. Ctrl + e
This shortcut is similar to the previous one, except it makes your cursor jump to the other end of the line, the end. Type this key combination when in the middle of a line you are typing and you will jump right to the end of the line.
4. Ctrl + w
Using this shortcut will result in the deletion of one word to the left of the cursor. So if you are in the middle of a line that you are typing hitting “Control + w” will delete one word before the cursor.
5. ESC + d
This shortcut is the opposite of the previous one. Hitting “Escape + d” in your terminal will delete one word to the right of the cursor.
6. Ctrl + k
This is a very powerful shortcut and must be used with caution. It deletes all the data to the right of the cursor in the line that you are typing in.
7. Ctrl + u
This is again a very powerful shortcut and must be used with caution. It deletes all the data to the left of the cursor in the line that you are typing in. Remember that once you run this shortcut you can not get back the text that you have typed.

Tuesday, May 3, 2011

Create device entries via udev

blog entry
http://ramblings.narrabilis.com/wp/pi/
code
http://ramblings.narrabilis.com/src/pi-0.1.tar.bz2

static int pi_init(void)
{
        struct device *err_dev;

        msg = kmalloc(sizeof(char)*DIGITS, GFP_ATOMIC);

        // register pi as a device
        pi_Major = register_chrdev(0, DEVICE_NAME, &fops);

        if (pi_Major < 0) {
                printk ("Failed to register device %s with error %d\n", DEVICE_NAME, pi_Major);
                return pi_Major;
        }

        /* create /dev/pi
         * we use udev to make the file
         */
        pi_class = class_create(THIS_MODULE,DEVICE_NAME);
        err_dev = device_create(pi_class, NULL, MKDEV(pi_Major,0),NULL,DEVICE_NAME);

        return 0;
}

/* remove the module
*/
static void pi_exit(void)
{
        /* free our memory, then destroy the device
         * then unregister the class and destroy it (unregister before destroy....important
         */
        kfree(msg);
        device_destroy(pi_class,MKDEV(pi_Major,0));
        class_unregister(pi_class);
        class_destroy(pi_class);
        unregister_chrdev(pi_Major, DEVICE_NAME);
}

dd copy between two machines using netcat

If you want to make a partition image on another machine: on source machine: Boot both machines with the helix CD just to be extra sure. Then,

dd if=/dev/hda bs=16065b | netcat targethost-IP 1234

On target machine:

netcat -l -p 1234 | dd of=/dev/hdc bs=16065b