Passion/bash

bash (return, sendmail. getopt)

sunshout 2013. 3. 12. 19:36

o  Parameter 개수 ($#)

 

#!/bin/bash


echo "Param count:$#"


if [ $# != 2 ]; then

        echo "Not 2"

else

        echo "2"

fi


 

 

o  Process 탐지

 

function chk_process()

{

        SERVICE="$1"

        RESULT=`ps ax | sed -n /${SERVICE}/p`

        if [ "${RESULT:-null}" == null ]; then

                echo "$1 not running"

                return -1

        else

                echo "$1 running"

                return 1

        fi

}

 

 

o  Email 보내기

 

#!/bin/sh

 

function mailer {

MAIL="/usr/sbin/sendmail -t$1 -f$2"

 

SUBJECT=$3

BODY=$(/bin/cat $4)

 

${MAIL} << EOF

From: Sender <$1>

To: Receiver <$2>

Subject: ${SUBJECT}

 

${BODY}

EOF

}

 

#########################################################################

# Usage

# mailer <from> <to> <title> <body: file_path>

# ex) mailer test@example.com receiver@example2.com Hello /tmp/mail_text

##########################################################################

mailer choonho.son@kt.com choonho.son@gmail.com "mail test" /tmp/vdbench/output/flatfile.html

 

 

o  options

 

usage()

{

cat << EOF

usage: $0 options

 

This script installs vdbench and execute benchmarking.

 

OPTIONS:

   -h      Show this message

   -e      email address for receiving result

   -d      benchmark device (/dev/xvdb | /dev/hdb ...)

   -v      Verbose

EOF

}

 

EMAIL=

DEV="/dev/xvdb"

VERBOSE=

while getopts "he:d:v" OPTION

do

     case $OPTION in

         h)

             usage

             exit 1

             ;;

         e)

             EMAIL=$OPTARG

             ;;

         d)

             DEV=$OPTARG

             ;;

         v)

             VERBOSE=1

             ;;

         ?)

             usage

             exit

             ;;

     esac

done

 

if [[ -z $TEST ]]

then

        usage

        exit 1

fi