Tuesday, July 31, 2007

UNIX Shell is Old Fashion ?

Is UNIX shell an old fashion stuff ?

UNIX has been with us for more 3.5 decades ( Unix History) and shell is the glue for UNIX commands to work together as well as interact with the system. Most of the commands are designed to do one thing and do it extremely well. Also, it is designed to work with other commands via the standard input, standard output and error. Output from one command can be the input to the next commands, the same concept can be extended to allow a chain of commands to achieve a specific goal.

Example, you may want to find out how many users with name starting "ch" that are using Korn shell (/bin/ksh)

$ egrep '^ch' /etc/passwd | cut -f7 -d: | grep -c /bin/ksh
4
Using regular expression "egrep" to find those starting with 'ch' in the password file, the output will be served as input to the "cut" command which output the 7th field based on colon (:) as the field separator. The output will be fed into "grep" command to count (-c) the number of lines

Here we can show you step by step to achieve our goal

$ cat /etc/passwd
root:x:0:0:Super-User:/:/sbin/sh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:
listen:x:37:4:Network Admin:/usr/net/nls:
gdm:x:50:50:GDM Reserved UID:/:
webservd:x:80:80:WebServer Reserved UID:/:
nobody:x:60001:60001:NFS Anonymous Access User:/:
noaccess:x:60002:60002:No Access User:/:
nobody4:x:65534:65534:SunOS 4.x NFS Anonymous Access User:/:
chchan:x:1001:1000::/users/chchan:/bin/sh
hung:x:1002:1000::/users/hung:/bin/bash
chihung:x:1003:1000::/users/chihung:/bin/ksh
chi:x:1004:1000::/users/chi:/bin/ksh
manu:x:1005:1000::/users/manu:/bin/bash
chikeung:x:1006:1000::/users/chikeung:/bin/ksh
choo:x:1007:1000::/users/choo:/bin/ksh

$ egrep 'ch' /etc/passwd
chchan:x:1001:1000::/users/chchan:/bin/sh
chihung:x:1003:1000::/users/chihung:/bin/ksh
chi:x:1004:1000::/users/chi:/bin/ksh
chikeung:x:1006:1000::/users/chikeung:/bin/ksh
choo:x:1007:1000::/users/choo:/bin/ksh

$ egrep 'ch' /etc/passwd | cut -f7 -d:
/bin/sh
/bin/ksh
/bin/ksh
/bin/ksh
/bin/ksh

$ egrep 'ch' /etc/passwd | cut -f7 -d: | grep -c /bin/ksh
4

In fact, if you are an advanced user, you can do all that by using just "egrep" along.

$ egrep -c '^ch.*:/bin/ksh$' /etc/passwd
4
Powerful, isn't it. With regular expression, you can tell "egrep" that any line that starts with "ch" (anchor ^ - beginning of a line) followed by zero or more of any character. At the end of the line (anchor $ - end of a line), you need to match ":/bin/ksh". With "-c" option in "egrep", it will return the number of occurrence.

So, the answer to the question is - Unix is old, but not old-fashion, IMO. We should use whatever is approach to the task instead of reinventing the wheel.

Labels: ,

0 Comments:

Post a Comment

<< Home