ps command : NIX Classes

The ps (process status) command is used to provide information about the currently running processes, including their process identification numbers (PIDs).
A process, also referred to as a task, is an executing (i.e., running) instance of a program. Every process is assigned a unique PID by the system.

The basic syntax of ps is

ps [options]


-a     Displays all processes on a terminal, with the exception of group leaders.
-c     Displays scheduler data.
-d     Displays all processes with the exception of session leaders.
-e     Displays all processes.
-f     Displays a full listing.
-glist     Displays data for the list of group leader IDs.
-j     Displays the process group ID and session ID.
-l     Displays a long listing
-plist     Displays data for the list of process IDs.
-slist     Displays data for the list of session leader IDs.
-tlist     Displays data for the list of terminals.
-ulist     Displays data for the list of usernames.

The Default Output

linux@atul:/home/atul $ ps
3567696  pts/2  0:00 ps
4391144  pts/2  0:00 -ksh

The Process list output for user smaadmp7

linux@atul:/home/atul $ ps -u smaadmp7
1930  299258      -  0:01 dsapi_server
1930  671930      -  0:01 dsapi_slave
1930  696452      -  0:17 dsapi_slave
1930  790608      -  0:00 dsapi_server
1930 1200174      -  0:01 dsapi_server
1930 1634556      -  0:15 dsapi_slave
1930 1687662      -  0:00 dsapi_server
1930 1757284      -  0:00 dsapi_slave
1930 1790152      -  0:00 dsapi_server
1930 2081022      -  0:00 dsapi_server
1930 2158782      -  0:47 dsapi_slave
1930 2289812      -  0:00 dsapi_server

The -a option tells ps to list the processes of all users on the system rather than just those of the current user

linux@atul:/home/atul $ ps -a
1294566  pts/8  0:00 sleep
1884294  pts/5  0:00 ksh
2240748  pts/3  0:19 topas_nmon
3813422         0:00 <defunct>
4661352 pts/12  0:00 tail
4710552  pts/8  0:00 ksh
5263386  pts/3  0:00 ksh
5558486 pts/11  0:01 topas_nmon
6266958  pts/2  0:00 ps
6885508 pts/13  0:00 ksh
7254052  pts/4  0:00 ksh
8024154 pts/11  0:00 ksh

Own output format
If you are bored by the regular output, you could simply change the format. To do so use the formatting characters which are supported by the ps command.
If you execute the ps command with the ‘o’ parameter you can tell the ps command what you want to see:

linux@atul:/home/atul $ ps -o "%u : %U : %p : %a"
atul : atul : 2256934 : ps -o %u : %U : %p : %a
atul : atul : 4391144 : -ksh

Log listing
The -l option generates a long listing
The additional columns of most interest are NI and SZ. The former shows the nice value of the process, which determines the priority of the process. The higher the value, the lower the priority. The default nice value is 0 on NIX systems.
The latter displays the size of the process in memory. The value of the field is the number of pages the process is occupying. On Linux systems a page is 4,096 bytes.

linux@atul:/home/atul $ ps -l
200001 A 2291 3305488 4391144   3  61 20 432309400  1788           pts/2  0:00 ps
240001 A 2291 4391144 5161086   0  60 20 3430d7400   792           pts/2  0:00 ksh

The -e option generates a list of information about every process currently running. The -f option generates a listing that contains fewer items of information for each process than the -l option.

linux@atul:/home/atul > ps -ef | more
root       1       0   0   Apr 28      -  6:21 /etc/init
root  102518       1   0   Apr 28      - 23:31 /usr/sbin/syncd 60
root  106530       1   0   Apr 28      -  0:00 /usr/sbin/uprintfd
root  127116       1   0   Apr 28      -  0:10 /usr/ccs/bin/shlap64
root  135190  204998   0   Apr 28      -  4:39 /opt/IBM/ITM/aix526/ux/bin/stat_daemon 12
root  147644  323618   0   Apr 28      -  0:02 /usr/sbin/portmap
root  163990       1   0   Apr 28      -  0:00 /usr/lib/errdemon
root  192710  323618   0   Apr 28      -  0:00 haemd HACMP 1 CCMP0 SECNOSUPPORT
root  204998       1   0   Apr 28      - 2352:55 /opt/IBM/ITM/aix526/ux/bin/kuxagent
root  209066  135190   0   Apr 28      -  0:06 /opt/IBM/ITM/aix526/ux/bin/ifstat 30 7
root  217206  286754   0 08:18:21      -  0:00 sshd: mahendra [priv]
root  229520  323618   0   Apr 28      -  0:00 /usr/sbin/rsct/bin/IBM.ServiceRMd
root  233604  323618   0   Apr 28      -  5:59 /usr/sbin/snmpd -c /etc/snmpd.conf
root  241798  135190   0   Apr 28      -  0:59 /opt/IBM/ITM/aix526/ux/bin/nfs_stat AIX 30 9
root  249928       1   0   Apr 28      -  0:11 /tivoli_ep_B/opt/Tivoli/lcf/bin/aix4-r1/mrt/lcfd
root  253990       1   0   Apr 28      -  3:53 /opt/IBM/ITM/aix526/ul/bin/kulagent
root  258098  135190   0   Apr 28      -  2:00 /opt/IBM/ITM/aix526/ux/bin/kux_vmstat 30 6


keep scripting…….


date command : NIX classes


A useful command line tool is date, which is typically used for displaying the current system date, or setting it. The default format of the date and time displayed will be the system default, eg “date : Wed May  9 19:42:23 GMT 2012
“, but it is possible to apply your own formatting, and also to specify a different date to use, without adjusting the system clock.

The date command writes the current date and time to standard output if called with no flags or with a flag list that begins with a + (plus sign). Otherwise, it sets the current date. Only a root user can change the date and time.
If, for example, you wanted to display the current day’s day of the week only.
If you follow the date command with a + (plus sign) and a field descriptor, you can control the output of the command. You must precede each field descriptor with a % (percent sign). The system replaces the field descriptor with the specified value. Enter a literal % as %% (two percent signs). The date command copies any other characters to the output without change. The date command always ends the string with a new-line character.

date +%A

The + switch tells the date command to apply the following format to the current date. %A tells date that the format to use is the locale’s full weekday name. A full list of the formatting modifiers is at the end of this article. It’s Friday today, so entering the above command at the command prompt would display this:

$ date +%A

If you wanted to display the date in the format YYYY-MM-DD, with a 4 digit year and 2 digit months with leading zeros, you would do this:

$ date +%Y-%m-%d

Specifying different dates

That was pretty easy, but the above examples only show the current system date. What if you wanted to show yesterday’s date? There’s another switch for date which allows you to specify a date other than the current one, the -d switch. The great thing with -d is you can use words to specify previous or future dates, as per the examples below.

Using date in other commands

Within the bash/ksh shell you can embed commands within other commands using backticks. As a very simple example, we’ll use the echo command. The first example is without backticks so will just echo the word “date” the second example uses backticks and does echo the date. You wouldn’t normally do this because date echoes the output anyway.

$ echo date

$ echo `date`
Wed May  9 19:42:23 GMT 2012

Date format specifiers

The following are the available date format specifiers: (Some are supported by only specific shells, please check before using these)

%%     a literal %
%a     locale's abbreviated weekday name (e.g., Sun)
%A     locale's full weekday name (e.g., Sunday)
%b     locale's abbreviated month name (e.g., Jan)
%B     locale's full month name (e.g., January)
%c     locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
%C     century; like %Y, except omit last two digits (e.g., 21)
%d     day of month (e.g, 01)
%D     date; same as %m/%d/%y
%e     day of month, space padded; same as %_d
%F     full date; same as %Y-%m-%d
%g     last two digits of year of ISO week number (see %G)
%G     year of ISO week number (see %V); normally useful only with %V
%h     same as %b
%H     hour (00..23)
%I     hour (01..12)
%j     day of year (001..366)
%k     hour ( 0..23)
%l     hour ( 1..12)
%m     month (01..12)
%M     minute (00..59)
%n     a newline
%N     nanoseconds (000000000..999999999)
%p     locale's equivalent of either AM or PM; blank if not known
%P     like %p, but lower case
%r     locale's 12-hour clock time (e.g., 11:11:04 PM)
%R     24-hour hour and minute; same as %H:%M
%s     seconds since 1970-01-01 00:00:00 UTC
%S     second (00..60)
%t     a tab
%T     time; same as %H:%M:%S
%u     day of week (1..7); 1 is Monday
%U     week number of year, with Sunday as first day of week (00..53)
%V     ISO week number, with Monday as first day of week (01..53)
%w     day of week (0..6); 0 is Sunday
%W     week number of year, with Monday as first day of week (00..53)
%x     locale's date representation (e.g., 12/31/99)
%X     locale's time representation (e.g., 23:13:48)
%y     last two digits of year (00..99)
%Y     year
%z     +hhmm numeric timezone (e.g., -0400)
%Z     alphabetic time zone abbreviation (e.g., EDT)


keep scripting…….

chsh commad : NIX classes

The chsh command changes a user’s login shell attribute. The shell attribute defines the initial program that runs after a user logs in to the system. This attribute is specified in the /etc/passwd file. By default, the chsh command changes the login shell for the user who gives the command.

Shell used by me

 [root@home ~]# echo $SHELL

To get the chsh help

 [root@home ~]# chsh --help
 Usage: chsh [ -s shell ] [ --list-shells ] [ --help ] [ --version ][ username ]

To list the available shells in system

[root@home ~]# chsh -l

Step to change Linux login shell.
The example below show the use of chsh command to change shell for current user.

[atul@home ~]$ chsh
 Changing shell for atul.
 New shell [/bin/bash]: /bin/csh
 Shell changed.
[atul@home ~]$ chsh -s /bin/bash  ---> chsh [ -s shell ]
 Changing shell for atul.
 Shell changed.

The chsh command issue with no option and –s option with the full pathname of the desired shell, is used to change shell for the user and it will prompt user for their password (note: the password only prompt for non-root user).  In above example we change the shell to /bin/csh (C SHell) and to /bin/bash (Bourne-Again Shell).  NOTE: the changes will take effect after you logout and login again.

Step to change shell for other user.

[root@home ~]# chsh -s /bin/csh atul  -->  chsh [ -s shell ][ username ]
 Changing shell for atul.
 Shell changed.


keep scripting…….

Perl 1 liners : tips & tricks : Part -2

Here we continues the Perl one liner’s magic…….
you can find Perl 1 liners : tips & tricks : Part -1 here. 11. Invert the letter case.

perl -ple ‘y/A-Za-z/a-zA-Z/’ file_name

This one-liner uses the y operator (also known as troperator). Operators y and tr do string transliteration.Given y/SEARCH/REPLACE/, the operator transliterates all occurrences of the characters found in SEARCH list with the corresponding (position-wise) characters in REPLACE list. Here all caps letters are replaced with small letters and vice-versa.

12. Search and replace in a file.

perl -i -pe ‘s/1/One/’ file_name

Here 1 is replaced with One in the file for 1st occurence only. This is equivalent to the sed command on UNIX systems.

13. Substitute (find and replace) “Dialog” with “Proquest”on lines that matches “IBM”.

perl -pe ‘/IBM/ && s/Dialog/Proquest/’ file_name

This one-liner is equivalent to:

while (defined($line = <>)) {
if ($line =~ /IBM/) {
$line =~ s/Dialog/Proquest/;

14. Print lines that are 80 chars or longer.

perl -ne ‘print if length >= 80’ file_name

This one-liner prints all lines that are 80 chars or longer.

15. Print only line selected line or print all lines expect particular line.

perl -ne ‘$. == 13 && print && exit’ file_name

The $. special variable stands for “current line number”.This 1 liner prints 13 line of file.

perl -ne ‘$. != 27 && print’ file_name

Prints all the lines expect 27.

16. Print all lines between two regexes (including lines that match regex).

perl -ne ‘print if /regex1/../regex2/’ file_name

Here it prints all the lines between the 2 regex-es.

17. Print all lines that contain a only number.

perl -ne ‘print if /^\d+$/’ file_name

18. Print all lines that contain only characters.

perl -ne ‘print if /^[[:alpha:]]+$/ file_name

19. Print all lines that repeat.

perl -ne ‘print if ++$a{$_} == 2’ file_name

This one-liner keeps track of the lines it has seen so far and it also keeps the count of how many times it has seen the line before. If it sees the line the 2nd time, it prints it out because ++$a{$_} == 2 is true. If it sees the line more than 2 times, it just does nothing because the count for this line has gone beyond 2 and the result of the print check is false.

20. Print all the lines if strings in it looks like an email address.

perl -ne ‘print if/.+@.+\..+/’ file_name

Prints all the lines containing email address.

Enjoy the Simplicity………

read command in Unix/Linux

Reads one line from standard input.

The read command reads one line from standard input and assigns the values of each field in the input line to a shell variable using the characters in the IFS (Internal Field Separator) variable as separators.

The Variable Name parameter specifies the name of a shell variable that takes the value of one field from the line of input. The first shell variable specified by the Variable Name parameter is assigned the value of the first field, the second shell variable specified by the Variable Name parameter is assigned the value of the second field, and so on, until the last field is reached.

If the line of standard input has more fields than there are corresponding shell variables specified by the Variable Name parameter, the last shell variable specified is given the value of all the remaining fields. If there are fewer fields than shell variables, the remaining shell variables are set to empty strings.

    Note: If you omit the Variable Name parameter, the variable REPLY is used as the default variable name.

p     Reads input from the output of a process run by the Korn Shell using |& (pipe, ampersand).
Note: An end-of-file character with the -p flag causes cleanup for this process so that another can be spawned.
-r     Specifies that the read command treat a \ (backslash) character as part of the input line, not as   a      control character.
-s     Saves the input as a command in the Korn Shell history file.
-u [ n ]     Reads input from the one-digit file descriptor number, n. The file descriptor can be opened with the ksh exec built-in command. The default value of the n is 0, which refers to the keyboard. A value of 2 refers to standard error.

To save a copy of the input line as a command in the history file, type:
read -s line < input_file
If input_file contains “echo hello world,” then “echo hello world” will be saved as a command in the history file.

Perl 1 liners : tips & tricks : Part -1

Typically there are cases, where one would not like to create a source file to check small programs. Such things can be tested by running them using “-e” option of the Perl. There are many other command line switches available and some of them have been listed below.

List of Perl’s Command Line Switches  Switch Description

-e This option can be used to enter a Perl program directly in the command line. 
-p This option loops around your program and print it. 
-n This option too loops around your program. 
-l Automatically chomps the input line (basically gets rid of newline at the end). 
-a Autosplit mode with -n or -p option. 
-i Edit <> files in place. 
-d Runs the program in debug mode. 
-v Prints version and sub-version of the Perl binary. 
-w Enables the useful warnings. 
-W Enable all warnings. 
-X Disable all warnings. 

Below are some handy one-liners which should be useful to all Perl programmers.

1. Remove all blank lines from a file

perl -i -ne ‘print unless /^$/’ file_name

The -n flag causes Perl to assume a similar program.

while (<>) {
    print unless $_ =~ /^$/;

2. Print the number of empty lines in a file.

perl -lne ‘$a++ if /^$/; END {print $a}’ file_name

The -l argument makes sure a newline gets added afterprinting out this number. The above program is interpreted as below.

while (<>) {
    $a++ if $_ =~ /^$/;

END { print $a; print “\n”; }

3. Find the total number of fields (words) in each line.

perl -alne ‘print scalar @F’ file_name

# or

perl -alne ‘print ~~@F’ file_name

The -a argument turns on field auto-splitting.This one-liner forces to evaluate the @F in scalar context, which inPerl means “the number of elements in @F.” Therefore thisone-liner prints out the number of elements on each line.

4. Find the total number of fields (words) on all lines.

perl -alne ‘$t += @F; END { print $t }’

Here we just keep adding the number of fields on each line to the variable $t, and at the end we print it out. The result is number of words on all lines.

5. Generate and print the alphabet or numbers.

perl -le ‘print a..z’
    #prints abcdefghijklmnopqrstuvwxyz
perl -le ‘print 1..20’
    # prints 1234567891011121314151617181920

It actually becomes difficult to interpret the output because there is not field separator in the output. If one wishes to include whitespaces between the output numbers (or alphabets) then it may be assigned to an array variable and then printed, as given below.

perl -le ‘@F=A..Z; print “@F”‘
    #prints A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
perl -le ‘@F=1..15; print “@F”‘
    # prints 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

If one wants the alphabetical series as AA, AB and so on, the following trick can be used

perl -le ‘@F=A..AZ; print “@F”‘
    # prints A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

It is also possible to change the field separator in the output by using the $, (the Output Field Separator built-in variable).

perl -le ‘$, = “,”; print a..z’
    # prints a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z

6. Generate Random numbers.

perl -le ‘$,= “\n”; print map { (“890”..”999″)[rand 26] } 1..4’
    # prints 890
    #        907
    #        900
    #        914

In each iteration the code chooses a random number from the numbers between 890-999. When map is done iterating, it returns the generated list of numbers and prints it out.

7. Create an array of even or odd numbers.

perl -le ‘@even = grep {$_ % 2 == 0} 1..100; print “@even”‘

The above code generates an array of odd numbers from 1 to99 (as 1, 3, 5, 7, 9, 11, …, 99). It uses the grepfunction that evaluates the given code $_ % 2 == 1 for eachelement in the given list 1..100 and returns only theelements that had the code evaluate to true.

8. Find the length of the string.

perl -le ‘print length “one-liners are great”‘
     # prints 20

9. Find the number of elements in an array.

perl -le ‘@array = (“a”..”z”); print ~~@array’
    # prints 26

10. Convert all text to uppercase, lowercase.

perl -nle ‘print uc’ file_name
    # prints file in uppercase.

perl -nle ‘print lc’ file_name
    # prints file in uppercase.

We will continue this session in next part. till then…

Enjoy the Simplicity………

bc command : tips & tricks

bc stands for bench calculator
Here are some examples of bc command :
a) addition
$ echo '57+43' | bc100

b) subtraction
$ echo '57-43' | bc14

c) multiplication
$ echo '57*43' | bc2451

The scale variable determines the number of digits which follow the decimal point in your result. By default, the value of the scale variable is zero.

d) division
$ echo 'scale=25;57/43' | bc1.3255813953488372093023255


njoy the simplicity…….