Tuesday, June 07, 2005

UNIX tips for the day - (1)

1. Standard Input to text editor
If you want a command line text editor to read from standard input instead of from a file, use the following:

$> ls -R pipe_symbol vi -

2. To add line numbers to the beginning of every line in a file
$> grep -n . input_file > output_file

3. Searching the man pages for keywords
If the system you are using is configured to support this, you can use man with the -k option to search descriptions of all the man pages . For example,
$> man -k poll

will result in the list of all the pages that contain the string 'poll'

4. Checking if a process is currently running
If you know the pid of the process, you can use the kill command to find out if it is running, without using 'ps -aef grep ' etc.
$> kill -0 pid

If a process with that pid is not running, it would crib saying 'No such process'

5. How do you sleep for less than one second in a C Program in UNIX?
Sleeping for less than one second in UNIX is called 'napping' ( short sleeps) . To achieve it, use usleep(), which suspends execution for n microseconds. If your system doesnt support usleep(), implement it by using select() / poll ();

int usleep(long usec) {
static struct {
long sec,usec;
} delay;

delay.sec = usec / 1000000L;
delay.usec = usec % 1000000L;

return select( 0, (long* 0), (long* 0), (long* 0), &delay);



Post a Comment

<< Home