Thursday, 25 December 2014

Shell script with a Progress Indicator

Hi,

    Shell script is a powerful tool for system administrators to make their life easy. It's Potential are numerous, below is a simple shell script that shows how to control a background function from the main script and show a progress indicator until the main script is complete. It's a basic script i want to post here for my reference and also for anyone who just need a little push to start their scripting, you can expand/improvise this script as per your need.


#!/bin/bash

#script to print twirling lines

#function to print rotating lines continuously

function rotate_line
{

INTERVAL=1
T_COUNT=0

while :
do
        T_COUNT=$(expr $T_COUNT + 1)
        case $T_COUNT in
                1) echo -e "-----\b\b\b\b\b\c"
                   sleep $INTERVAL
                   ;;
                2) echo -e '\\\\\\'"\b\b\b\c"
                   sleep $INTERVAL
                   ;;
                3) echo -e "|||||\b\b\b\b\b\c"
                   sleep $INTERVAL
                   ;;
                4) echo -e "/////\b\b\b\b\b\c"
                   sleep $INTERVAL
                   ;;
                *) T_COUNT=0
                   ;;
        esac
done

}

#function to exit the rotate_line function in case of interruption to the main script, please note that kill -9 can't be trapped , so if a kill -9 is passed to the main script,
#the rotate_line function will keep running and had to be killed manually

function trap_exit
{
    kill -9 $ROTATE_PID
}

# Main script
#call the trap_exit function for the interrupt code 1, 2, 3, 15 and exit with the status code 2, note that without the below statement, the interruption of main script will make #the rotate_line process orphan and running forever until manually interrupted. However kill -9 can't trapped


trap 'trap_exit; exit 2' exit 1 2 3 15

#call the rotate_line function in background


rotate_line &

#Record the process id of the rotate_line function
ROTATE_PID=$!

TOTAL_COUNT=300

#loop through for 5 minutes and rotating lines will be displayed for this 5 minutes
until (( TOTAL_COUNT == 0 ))
do
        (( TOTAL_COUNT = TOTAL_COUNT - 1 ))
        sleep 1
done


#Once the loop is completed , terminate the rotate_line function
kill -9 $ROTATE_PID


Saturday, 6 December 2014

How to Create a Solaris Package

Hello,

          I've been working on Solaris for the past 3 years and the commands i used most on this servers are pkgadd and pkgrm. So if you are developing a application and planning to have it deployed/installed on a solaris machine, it's better to create a solaris package to do the installation rather than copying the files manually over to the server, because the UN-installation becomes a night mare if you do the copy manually, you will definitely not remember where you copied those files and more over you have better things to remember than this. If you use solaris packages to do the installation , the pkg utility in solaris server remembers where those files are installed and you can use the pkg utility to do the UN-installation or query the contents of the package, for more information on the pkg utility commands refer http://docs.oracle.com/cd/E26505_01/html/E29492/ewbej.html . In this post i will be creating a simple solaris package for the GNUscreen utility, i 've already downloaded the GNU screen 3.9.4 and compiled them and available in my solaris server. Now i need to create a package for it so that i don't have to do the compilation every time i need to install the screen utility in other solaris server. I am going  to use Solaris 10 10/08 s10x_u6wos_07b X86 for creating the package.


Let's get to work, i am inside the directory where the files/directory to be packaged are available.

[madhu@pkgserver] $ pwd
/tmp/GNUscreen-pkg


List the contents inside the directory to see what GNU-screen-3.9.4 has,

[madhu@pkgserver] $ ls -l
drwxr-xr-x   2 madhu admin        186 Sep  7 09:47 bin
drwxr-xr-x   2 madhu admin        182 Sep  7 09:47 etc
drwxr-xr-x   2 madhu admin        535 Sep  7 09:47 info
drwxr-xr-x   3 madhu admin       178 Sep  7 09:47 man
drwxr-xr-x   3 madhu admin        180 Sep  7 09:47 share


Next step is to create the pkginfo file, this file is mandatory for package creation. After installing the package , when you do a pkginfo <<packag name>> , the information is read from this file and displayed to the user.  Below is the pkginfo file for this package

[madhu@pkgserver] $ cat pkginfo
PKG=GNUscreen
NAME=GNU screen 3.9.4 SPARC 32bit Solaris 8
VERSION=3.9.4
ARCH=sparc
CLASSES=none
CATEGORY=utility
VENDOR=tester
PSTAMP=1st April 2000
EMAIL=test@email.com
BASEDIR=/


Next step is to Create the Prototype file, this file is the input file for the package creation. This file contains the directories/files/pre-install/post-install scripts to be included in the package,

The Prototype file should be in a specific format, in the below steps we will generate this file.

[madhu@pkgserver] $ pkgproto /tmp/GNUscreen-pkg=/opt/GNUscreen-pkg > prototype
pkgproto is the command to generate the prototype file and the arguments are source directory and target directory.  Ref : http://www.lehman.cuny.edu/cgi-bin/man-cgi?pkgproto+1

Let's list the contents of prototype file. I've truncated the contents to keep the blog post short,

[madhu@pkgserver]$ cat prototype
d none /opt/GNUscreen-pkg 0755 madhu admin
d none /opt/GNUscreen-pkg/bin 0755 madhu admin
f none /opt/GNUscreen-pkg/bin/screen-3.9.4=/tmp/GNUscreen-pkg/bin/screen-3.9.4 0755 madhu admin
d none /opt/GNUscreen-pkg/etc 0755 madhu admin
f none /opt/GNUscreen-pkg/etc/screenrc=/tmp/GNUscreen-pkg/etc/screenrc 0644 madhu admin
d none /opt/GNUscreen-pkg/info 0755 madhu admin
f none /opt/GNUscreen-pkg/info/screen.info-1=/tmp/GNUscreen-pkg/info/screen.info-1 0644 madhu admin
f none /opt/GNUscreen-pkg/info/screen.info-2=/tmp/GNUscreen-pkg/info/screen.info-2 0644 madhu admin
f none /opt/GNUscreen-pkg/info/screen.info=/tmp/GNUscreen-pkg/info/screen.info 0644 madhu admin
f none /opt/GNUscreen-pkg/info/screen.info-3=/tmp/GNUscreen-pkg/info/screen.info-3 0644 madhu admin
f none /opt/GNUscreen-pkg/info/screen.info-4=/tmp/GNUscreen-pkg/info/screen.info-4 0644 madhu admin
f none /opt/GNUscreen-pkg/info/screen.info-5=/tmp/GNUscreen-pkg/info/screen.info-5 0644 madhu admin
d none /opt/GNUscreen-pkg/man 0755 madhu admin
d none /opt/GNUscreen-pkg/man/man1 0755 madhu admin
f none /opt/GNUscreen-pkg/man/man1/screen.1=/tmp/GNUscreen-pkg/man/man1/screen.1 0644 madhu admin
d none /opt/GNUscreen-pkg/share 0755 madhu admin


The syntax of this file is of format <type> <class> <name of file> <permissions> <user> <group> , all the fields are self explanatory, except the second one called class, more information about the class can be found here http://docs.oracle.com/cd/E19683-01/806-7008/6jftmsc38/index.html . If needed you can change the values in the Prototype file except the <name of file> , i am changing the <user> <group> . 
Also the pkginfo file is missing in the protoype file, so that needs to included as well else the package creation will fail. After the change it looks like below.

[madhu@pkgserver]$ cat prototype
i pkginfo
d none /opt/GNUscreen-pkg 0755 root root
d none /opt/GNUscreen-pkg/bin 0755 root root
f none /opt/GNUscreen-pkg/bin/screen-3.9.4=/tmp/GNUscreen-pkg/bin/screen-3.9.4 0755 root root
d none /opt/GNUscreen-pkg/etc 0755 root root
f none /opt/GNUscreen-pkg/etc/screenrc=/tmp/GNUscreen-pkg/etc/screenrc 0644 root root
d none /opt/GNUscreen-pkg/info 0755 root root
f none /opt/GNUscreen-pkg/info/screen.info-1=/tmp/GNUscreen-pkg/info/screen.info-1 0644 root root
f none /opt/GNUscreen-pkg/info/screen.info-2=/tmp/GNUscreen-pkg/info/screen.info-2 0644 root root
f none /opt/GNUscreen-pkg/info/screen.info=/tmp/GNUscreen-pkg/info/screen.info 0644 root root
f none /opt/GNUscreen-pkg/info/screen.info-3=/tmp/GNUscreen-pkg/info/screen.info-3 0644 root root
f none /opt/GNUscreen-pkg/info/screen.info-4=/tmp/GNUscreen-pkg/info/screen.info-4 0644 root root
f none /opt/GNUscreen-pkg/info/screen.info-5=/tmp/GNUscreen-pkg/info/screen.info-5 0644 root root
d none /opt/GNUscreen-pkg/man 0755 root root
d none /opt/GNUscreen-pkg/man/man1 0755 root root
f none /opt/GNUscreen-pkg/man/man1/screen.1=/tmp/GNUscreen-pkg/man/man1/screen.1 0644 root root
d none /opt/GNUscreen-pkg/share 0755 root root


Ok, now everything is set, it's time to create the package .

[madhu@pkgserver]$ pkgmk -o -d /tmp/GNUscreen-pkg/ -f prototype
## Building pkgmap from package prototype file.
## Processing pkginfo file.
WARNING: missing directory entry for </opt>
## Attempting to volumize 28 entries in pkgmap.
part  1 -- 2244 blocks, 121 entries
## Packaging one part.
/tmp/GNUscreen-pkg/GNUscreen/pkgmap
/tmp/GNUscreen-pkg/GNUscreen/pkginfo
/tmp/GNUscreen-pkg/GNUscreen/root/opt/GNUscreen-pkg/bin/screen-3.9.4
/tmp/GNUscreen-pkg/GNUscreen/root/opt/GNUscreen-pkg/etc/screenrc
/tmp/GNUscreen-pkg/GNUscreen/root/opt/GNUscreen-pkg/info/screen.info
/tmp/GNUscreen-pkg/GNUscreen/root/opt/GNUscreen-pkg/info/screen.info-1
/tmp/GNUscreen-pkg/GNUscreen/root/opt/GNUscreen-pkg/info/screen.info-2
/tmp/GNUscreen-pkg/GNUscreen/root/opt/GNUscreen-pkg/share/screen/screeninfo.src
/tmp/GNUscreen-pkg/GNUscreen/root/opt/GNUscreen-pkg/share/screen/screenrc
/tmp/GNUscreen-pkg/GNUscreen/root/opt/GNUscreen-pkg/share/screen/us-braille.tbl
## Validating control scripts.
## Packaging complete.


pkgmk command is used to create the package, let's check the content of the package to ensure the integrity of the package.  Ref : http://heirloom.sourceforge.net/pkgtools/pkgmk.1.html

[madhu@pkgserver]$ pwd
/tmp/GNUscreen-pkg/GNUscreen
[madhu@pkgserver]$ ls -lt root/opt/GNUscreen-pkg/
total 40
drwxr-xr-x   3 melango  httpd        180 Dec  6 15:57 share
drwxr-xr-x   3 melango  httpd        178 Dec  6 15:57 man
drwxr-xr-x   2 melango  httpd        535 Dec  6 15:57 info
drwxr-xr-x   2 melango  httpd        182 Dec  6 15:57 etc
drwxr-xr-x   2 melango  httpd        186 Dec  6 15:57 bin


Though we created the package ,it's in the directory format. We need to transform it in to the pkg format for ease of use.

[madhu@pkgserver]$ pkgtrans -o . /tmp/GNUscreen.pkg GNUscreen
Transferring <GNUscreen> package instance


Ref : http://heirloom.sourceforge.net/pkgtools/pkgtrans.1.html

Let's check the installation.

 [madhu@pkgserver]$ /usr/sbin/pkgadd -d /tmp/GNUscreen.pkg

The following packages are available:
  1  GNUscreen     GNU screen 3.9.4 SPARC 32bit Solaris 8
                   (sparc) 3.9.4

Select package(s) you wish to process (or 'all' to process
all packages). (default: all) [?,??,q]: all

Processing package instance <GNUscreen> from </tmp/GNUscreen.pkg>

GNU screen 3.9.4 SPARC 32bit Solaris 8(sparc) 3.9.4
tester
## Processing package information.
## Processing system information.
## Verifying disk space requirements.
## Checking for conflicts with packages already installed.
## Checking for setuid/setgid programs.

Installing GNU screen 3.9.4 SPARC 32bit Solaris 8 as <GNUscreen>

## Installing part 1 of 1.
/opt/GNUscreen-pkg/bin/screen-3.9.4
/opt/GNUscreen-pkg/etc/screenrc
/opt/GNUscreen-pkg/info/screen.info
/opt/GNUscreen-pkg/info/screen.info-1
/opt/GNUscreen-pkg/info/screen.info-2
/opt/GNUscreen-pkg/info/screen.info-3
/opt/GNUscreen-pkg/info/screen.info-4
/opt/GNUscreen-pkg/info/screen.info-5
/opt/GNUscreen-pkg/man/man1/screen.1

[ verifying class <none> ]

Installation of <GNUscreen> was successful.



[madhu@pkgserver]$ ls -ltr /opt/GNUscreen-pkg/
total 10
drwxr-xr-x   2 root     root         512 Dec  6 17:26 bin
drwxr-xr-x   2 root     root         512 Dec  6 17:26 etc
drwxr-xr-x   2 root     root         512 Dec  6 17:26 info
drwxr-xr-x   3 root     root         512 Dec  6 17:26 man
drwxr-xr-x   3 root     root         512 Dec  6 17:26 share



Nice, the files are available in the desired location with the right permission and ownership.

Let's get some information about the package.

[madhu@pkgserver]$ pkginfo -l GNUscreen
   PKGINST:  GNUscreen
      NAME:  GNU screen 3.9.4 SPARC 32bit Solaris 8
  CATEGORY:  utility
      ARCH:  sparc
   VERSION:  3.9.4
   BASEDIR:  /
    VENDOR:  tester
    PSTAMP:  1st April 2000
  INSTDATE:  Dec 06 2014 17:26
     EMAIL:  test@email.com
    STATUS:  completely installed
     FILES:       28 installed pathnames
                   8 directories
                   1 executables
                1396 blocks used (approx)


Let's remove the package


[madhu@pkgserver]$ /usr/sbin/pkgrm GNUscreen

The following package is currently installed:
   GNUscreen  GNU screen 3.9.4 SPARC 32bit Solaris 8
              (sparc) 3.9.4

Do you want to remove this package? [y,n,?,q] y

## Removing installed package instance <GNUscreen>
## Verifying package <GNUscreen> dependencies in global zone
## Processing package information.
## Removing pathnames in class <none>
/opt/GNUscreen-pkg/share/screen/us-braille.tbl
/opt/GNUscreen-pkg/share/screen/screenrc
/opt/GNUscreen-pkg/share/screen/screeninfo.src
/opt/GNUscreen-pkg/share/screen/screencap
/opt/GNUscreen-pkg/share/screen/newsyntax38
/opt/GNUscreen-pkg/share/screen/newsyntax
/opt/GNUscreen-pkg/share/screen/gs-braille.tbl
/opt/GNUscreen-pkg/share/screen/gr-braille.tbl
## Updating system information.

Removal of <GNUscreen> was successful.

[madhu@pkgserver]$ ls -ltr /opt/GNUscreen-pkg/
/opt/GNUscreen-pkg/: No such file or directory



That's it the solaris package is created. Hope this will be helpful for someone. Please note am not an expert in solaris administration, just sharing what i know. If i am wrong , correct me by commenting. Have a Nice Day :)








Saturday, 4 January 2014

sed handy commands

Just exploring sed utility , so thought of sharing my learnings.

[root@localhost] $ cat example_file
 This is the first line of an example text.
 It is a text with erors.
 Lots of erors.
 So much erors, all these erors are making me sick.
 This is a line not containing any errors.
 This is the last line.

Print the lines containing the string "errors"
 [root@localhost] $ sed -n '/errors/p' example_file
 This is a line not containing any errors.

Print the lines not containing the string "errors"
[root@localhost] $ sed '/errors/d' example_file
This is the first line of an example text.
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
This is the last line.

Print lines 2,3 and 4
root@localhost] $ sed -n '2,4p' example_file
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.

Print other lines excluding lines 2,3,4
[root@localhost] $ sed '2,4d' example_file
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.

Insert a string at the beginning of lines 1,2
[root@localhost] $ sed -e 's/^/Hi, /g' -e  '3,6d' example_file
Hi, This is the first line of an example text.
Hi, It is a text with erors.


Removing Blank lines from a file
[root@localhost] $ sed /^$/d -i example_file