Search This Blog

Tuesday, February 10, 2009

Regular Expression

Regular Expression is really cool, All should know it;) Initially you might feel that its too complex but when u get into it u feel its look complex but it is really cool and easy to use. It completes big task in few lines....... Its really great;) Below is my first function in Python which i have used to search for a String using '*' to escape and search in string. Cheers Python B) def SearchString(pattern,string): ulist = pattern.split('*') if len(ulist) > 1: k = '^' + ulist[0] + '[\w|\_|\-]*' + ''.join([ i + '[\w|\_|\-]*' for i in ulist[1:-1]]) + ulist[-1] + '$' print k if re.search(k,user) != None: return "Found" else: return "Not Found" elif ulist == string: return "Found" else: return "Not Found" >>> SearchUser('vin*jda*is','vinojdavis') ^vin[\w|\_|\-]*jda[\w|\_|\-]*is$ 'Found' >>> SearchUser('vi**jda*is','vinojdavis') ^vi[\w|\_|\-]*[\w|\_|\-]*jda[\w|\_|\-]*is$ 'Found' >>> SearchUser('vin*jda*is','viojdavis') ^vin[\w|\_|\-]*jda[\w|\_|\-]*is$ 'Not Found'

Tuesday, February 3, 2009

Kill process in Linux or terminate a process in UNIX or Linux systems

Q. How do I kill process in Linux?

A. Linux and all other UNIX like oses comes with kill command. The command kill sends the specified signal (such as kill process) to the specified process or process group. If no signal is specified, the TERM signal is sent.

Kill process using kill command under Linux/UNIX

kill command works under both Linux and UNIX/BSD like operating systems.

Step #1: First, you need to find out process PID (process id)

Use ps command or pidof command to find out process ID (PID). Syntax: ps aux | grep processname pidof processname

For example if process name is lighttpd, you can use any one of the following command to obtain process ID: # ps aux | grep lighttpdOutput:

lighttpd  3486  0.0  0.1   4248  1432 ?        S    Jul31   0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
lighttpd  3492  0.0  0.5  13752  3936 ?        Ss   Jul31   0:00 /usr/bin/php5-cg

OR use pidof command which is use to find the process ID of a running program: # pidof lighttpdOutput:

3486

Step #2: kill process using PID (process id)

Above command tell you PID (3486) of lighttpd process. Now kill process using this PID: # kill 3486 OR # kill -9 3486 Where,

  • -9 is special Kill signal, which will kill the process.

killall command examples

DO NOT USE killall command on UNIX system (Linux only command). You can also use killall command. The killall command kill processes by name (no need to find PID): # killall -9 lighttpd Kill Firefox process: # killall -9 firefox-bin As I said earlier killall on UNIX system does something else. It kills all process and not just specific process. Do not use killall on UNIX system (use kill -9).

Sunday, February 1, 2009

Variable Datatypes & Range in C

Variable Types:
There are a number of ‘built-in’ data types in C. These are listed below. Where a
shorter version of the type name exists, this is given in brackets; essentially the base
type int is implicit whenever short , long, or unsigned are used.
  1. short int (short)
  2. unsigned short int (unsigned short )
  3. char
  4. unsigned char
  5. signed char
  6. int
  7. unsigned int (unsigned )
  8. long int (long)
  9. unsigned long int (unsigned long )
  10. float
  11. double
  12. long double
The range of values that can be stored in variables of these types will depend on the
compiler and computer that you are using, but on an IBM PCs and the Borland Turbo
C compiler the ranges are:
  • short int -128 ® 127 (1 byte)
  • unsigned short int 0 ® 255 (1 byte)
  • char 0 ® 255 or -128 ® +127 2 (1 byte)
  • unsigned char 0 ® 255 (1 byte)
  • signed char -128 ® 127 (1 byte)
  • int -32,768 ® +32,767 (2 bytes)
  • unsigned int 0 ® +65,535 (2 bytes)
  • long int -2,147,483,648 ® +2,147,483,647 (4 bytes)
  • unsigned long int 0 ® 4,294,967,295 (4 bytes)
  • float single precision floating point (4 bytes)
  • double double precision floating point (8 bytes)
  • long doubl e extended precision floating point (10 bytes)

Wednesday, January 28, 2009

How do I label a Linux partition? How do I display current label?

How do I label a Linux partition? How do I display current label? A. You need to use e2label or tune2fs command line to change the label on an ext2/ext3 filesystem. e2label will display or change the filesystem label on the ext2 filesystem located on device.

Display current label

If the optional argument new-label is not present, e2label will simply display the current filesystem label. $ sudo e2label /dev/sda1 or # e2label /dev/sda1 Sample output:

/boot

Set a new label

If the optional argument new-label is present, then e2label will set the filesystem label to be new-label. Ext2 filesystem labels can be at most 16 characters long; if new-label is longer than 16 characters, e2label will truncate it and print a warning message. To set a new label, enter: # e2label /dev/sdb2 usbstroage It is also possible to set the filesystem label using the -L option of tune2fs, enter: # tune2fs -L usbstroage /dev/sdb2