Search This Blog

Friday, September 18, 2015

Using commandline options in shell scripts

Sample code using getopt

while getopts :f:cuh OP
do
            case ${OP} in
                           f)         SERVERS=${OPTARG}
                                       ;;
                           c)        if [ "${SERVERS}" != "false" ]
                                      then
                                                check
                                       else
                                               usage
                                      fi
                                      ;;
                           u)       if [ "${SERVERS}" != "false" ]
                                     then
                                                cd ${BASE}
                                      fi
                                     ;;
                        *|h)      usage
                                     ;;
                     esac
done

Friday, May 08, 2015

Use variable in search pattern in awk

Passing a variable to awk and using it in the search pattern.



awk -v string="$STRING" -f  script.awk  [filename]

Script extract:

$0 ~ string {
      
       print $0;
}

Note:

        On Solaris use nawk.

Tuesday, April 14, 2015

Match IP numbers with regex.


Print any IP address talking on port 21 in coming or out going.
Change the port number to any port that needs to be matched.

netstat -na | perl -ne '/((\d{1,3}\.){4}21)/ &&  print ;'
 

Monday, January 26, 2015

Variable assigments

Validating positional parameters.

The below example will print a usage message if there is no 2nd position paramaeter.

PACKAGE=${2:?$(usage ${SERVERLIST} [Package name:Version:Package file[:Dependent service]])}