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…….


basename & dirname : NIX classes

Hi Friends..
Here I am going to describe some commands which can help us in day to day task.

The basename command will strip off the directory portion of a filename path and return only the filename itself. In the following example, basename is used to strip the directory path from the filename so it returns simply

linux@atul $ basename /home/atul/script/WORKING_FINE/

Removing extensions
The basename utility also allows the extension or suffix to be stripped from a file basename. The extension to be stripped is added after the file path. In the following example, .sh is added after the filename. The return from basename is the single word file:

linux@atul $ basename /home/atul/script/WORKING_FINE/ .sh

The dirname utility is the complement of basename. It returns the “path” component of a file pathname string, as shown in the following listing. In the second dirname example, dirname also removes the trailing slash before attempting to understand the string. The result is that the dirname portion of

linux@atul $ dirname /home/atul/script/WORKING_FINE/

dirname has no options for the command and simply returns the directory portion of a file path. It is not used as often as basename.

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…….

System Resources commands : NIX classes

Hi guys…

In this post, I came up with system resources commands, which will help you to day to day task on nix box.

Command/Syntax What it will do
chsh (passwd -e/-s) username login_shell change the user’s login shell (often only by the superuser)
date [options]    report the current date and time
df [options] [resource] report the summary of disk blocks and inodes free and in use
du [options] [directory or file] report amount of disk space in use+
hostname/uname display or set (super-user only) the name of the current machine
kill [options] [-SIGNAL] [pid#] [%job] send a signal to the process with the process id number (pid#) or job control number (%n). The default signal is to kill the process.
man [options] command show the manual (man) page for a command
passwd [options] set or change your password
ps [options] show status of active processes
script file saves everything that appears on the screen to file until exit is executed
stty [options] set or display terminal control options
whereis [options] command report the binary, source, and man page locations for the command named which command reports  the path to the command or the shell alias in use
who or w report who is logged in and what process are running

We will learn all the above commands one by one in next posts of this series.

till then …

keep scripting…….

Twelve Methods to Parse a File Line by Line — Part 4(last)

Here we are continues the magic. You can find the part 1,2 & 3 here ….
Twelve Methods to Parse a File Line by Line — Part 1
Twelve Methods to Parse a File Line by Line — Part 2
Twelve Methods to Parse a File Line by Line — Part 3

Method 10:

function while_line_LINE_FD
exec 3<&0
exec 0< $FILENAME
while line LINE
echo “$LINE”
exec 0<&3

Method 11:

function while_LINE_line_cmdsub2_FD
exec 3<&0
exec 0< $FILENAME
while LINE=$(line)
print “$LINE”
exec 0<&3

Method 12:

function while_line_LINE_FD
exec 3<&0
exec 0< $FILENAME
while line LINE
echo “$LINE”
exec 0<&3

njoy the simplicity…….
©Mastering Unix Shell Scripting

cut – Splitting a file vertically : NIX Classes


The external cut command displays selected columns or fields from each line of a file.

-c     Cutting columns

to cutting specific columns, you need to follow the -c option with a list of column numbers, delimit by a comma. Range can also be used using the hyphen.

$ cut -c 6-22,24-26 emp.list

this command extract the data, from file, from 6th char to 22th and 24th char to 26th characters .

$ cut -c 2,3-4.6-12,15 emp.list

-f      Cutting fields

to extract useful data from these files u need to cut fields rather than columns. Cut use the tab as the default field delimiter, but can also work with a different delimiter. Option -d specified a single character delimiter which serves as field separator.Two option need to be used here : -d for the field delimiter and -f for the field list.

$ cut -d “|” -f2,3 emp.list

it extract the field 2,3 which is delimit by |.

** One of the option -c and -f must be specified. These option are really not optional, one of them is compulsory.

njoy the simplicity…….

pr – Paginating files : NIX Classes

the pr commands prepares a file for printing by adding suitable headers, footers and formatted text. A simple invocation of the command is to use it with a filename as argument:
$ pr filename

pr adds 5 line of margin at the top and 5 at the bottom. the header shows the date and time of last modification of the along with the filename and page number.

** pr options
-k               prints in k column
-d              double space input , reduce clutter
-n              numbers lines, helping in debugging code
-o n          offset lines by n spaces, increasing left margin of page
-t               a tab

$ pr -t -n -d -o 10 dept.list

pr often used as a “pre- processor” before printing with the lp command :
$ pr -h "Department list" dept.list | lp

$ pr +10 chap01 lp                                            start printing from page 10
$ pr -l 54 chap01 | lp                                 page length set to 54 lines

njoy the simplicity…….