arul's code
free code snippets

Display columns from colon delimited file

Read a colon delimited file /etc/passwd and display certain columns

This shell script reads a colon delimited file (for example /etc/passwd) and displays certain columns.

It uses while .. do .. done < filename to read the file.


#!/bin/bash

# readpasswdfile.sh
# Shell script to read /etc/passwd file
# Coded by Arul John
#  + Sample line in /etc/password is
#  + root:x:0:0:root:/root:/bin/bash

IFS=:
echo "Username | Shell"
while read name pass x x x x shell
do
    echo "$name uses $shell"
done < /etc/passwd

Make the script executable.

  chmod +x readpasswdfile.sh

It will show something like this:


arul ~/work $ ./readpasswd.sh
Username | Shell
root uses /bin/bash
bin uses /sbin/nologin
daemon uses /sbin/nologin
adm uses /sbin/nologin
lp uses /sbin/nologin
sync uses /bin/sync
shutdown uses /sbin/shutdown
halt uses /sbin/halt
mail uses /sbin/nologin
uucp uses /sbin/nologin
operator uses /sbin/nologin
games uses /sbin/nologin
gopher uses /sbin/nologin
ftp uses /sbin/nologin
nobody uses /sbin/nologin
dbus uses /sbin/nologin
usbmuxd uses /sbin/nologin
avahi-autoipd uses /sbin/nologin
vcsa uses /sbin/nologin
rtkit uses /sbin/nologin
abrt uses /sbin/nologin
nscd uses /sbin/nologin
tcpdump uses /sbin/nologin
avahi uses /sbin/nologin
haldaemon uses /sbin/nologin
openvpn uses /sbin/nologin
ntp uses /sbin/nologin
apache uses /sbin/nologin
saslauth uses /sbin/nologin
mailnull uses /sbin/nologin
smmsp uses /sbin/nologin
nm-openconnect uses /sbin/nologin
sshd uses /sbin/nologin
smolt uses /sbin/nologin
pulse uses /sbin/nologin
gdm uses /sbin/nologin
arul uses /bin/bash
nagios uses /sbin/nologin
vboxadd uses /bin/false
mysql uses /bin/bash
memcached uses /sbin/nologin

Share this

Last Updated: Posted on 07 Feb 2015