Sunday 9 February 2014

bash script with help options example

## Version 2:

#!/bin/bash

## Usage function declaration:

usage () {
  echo "Usage:"
  echo "Syntax: $0 [-s SOURCE_PATCH_DIR] [-d DESTINATION_DIR] -k PUBLIC_KEY -l FILE_HAVING_LIST_OF_SERVERS (-C Command_FIle OR -c \"COMMAND[;COMMAND2;COMMANDn])\"\n"
  echo "Options:"
  echo "-s: source patch folder"
  echo "-d: Destination directory where you want to copy the patch folder."
  echo "-k: Public key to access the remote hosts"
  echo "-l: File where we have hosts, with new line char."
  echo "NOTE: We except only one input command options either -C and filename or -c command(s)"
  echo "NOTE: If you provide both the command input options then we will take the last command input option's values"
  echo "-C: Path of the command file. Commands need to in new line"
  echo "-c: Input command(s) separated by \";\" on remote system(s) and MUST be inside a \"\"\n"
  echo "Example:"
  echo " sh $0 -k key.pem -l serverS.txt -C commandFile.txt"
  echo "$0 -s /home/ubuntu/Burg2/patch1 -d /mnt/PatchBackup/ -k /home/ubuntu/key.pem -l /home/ubuntu/serverList.txt -c \"uptime;hostname;ls -l | grep -i test\""
}



#Get the arguments passed
while getopts ":s:d:k:l:c:C:h:help" args; do
  case $args in
    h)
      usage;
      exit 0;
      ;;
    help)
      usage;
      exit 0;
      ;;
    s)
      SRC_FOLDER="${OPTARG}";;
    d)
        DES_FOLDER="${OPTARG}";;
    k)
      KEY_FILE="${OPTARG}";;
    l)
      SERVER_LIST="${OPTARG}";;
    C)
    CMD_FILE1="${OPTARG}";
    # The following sed command will remove the new line from the input file and update the command.
    # You can also try [ tr '\n' ';' ] ' But over here at the end you will see ; again '
    CMD_FILE=`sed ':a;N;$!ba;s/\n/;/g' ${CMD_FILE1}`;
    COMMAND=`echo ${CMD_FILE}`;;
    c)
        #COMMAND="${OPTARG}";;
        COMMAND1="${OPTARG}";
            # The following command will take multiple command with ; separated.
        COMMAND=`echo ${COMMAND1}|awk -F";" '{print $0}'`;;
       
    :)
      usage;
      exit 1;
      ;;
    ?)
      usage;
      exit 1;
      ;;
  esac
done


#if [ ! -e "${KEY_FILE}" && ! -f "${SERVER_LIST}" && -z "${COMMAND}" ]; then
#    echo "\n Error: Required input missing."
#    exit 1;
#fi

# Key file exists?
if [ ! -e "${KEY_FILE}" ]; then # [ -e FILE ]    True if FILE exists.
  echo "\nError: Key not found. Please provide absolute path of the key.\n";
  usage;
  exit 1;
fi

# server list exists?
if [ ! -f "${SERVER_LIST}" ]; then # [ -f FILE ]    True if FILE exists and is a regular file.
  echo "\nError: File not found. Please provide absolute path of the server_list files\n";
  usage;
  exit 1;
fi

# command provided?
if [ -z "${COMMAND}" ]; then # [ -z STRING ]    True of the length if "STRING" is zero.
  echo "\nError: Input command not found:\n";
  usage;
  exit 1;
 fi

## Executation of deployment.
for machine in `cat ${SERVER_LIST}`;
    do
      echo "\nStart: processing on  ${machine}...\n";
        if [ -d "${SRC_FOLDER}" ] ; then # [ -d FILE ]    True if FILE exists and is a directory.
            #echo "Source Folder: ${SRC_FOLDER}";
            ssh -pport -i ${KEY_FILE} ubuntu@${machine} "sudo mkdir -p ${DES_FOLDER}; sudo chown ubuntu:ubuntu ${DES_FOLDER}";
              scp -r -Pport -i ${KEY_FILE} ${SRC_FOLDER} ubuntu@${machine}:${DES_FOLDER};
        fi
      ssh -pport -i ${KEY_FILE} ubuntu@${machine} "${COMMAND}";
  done
  #ssh -pport -i ${KEY_FILE} ubuntu@${machine} "uptime";
# sudo rsync -vvrlb --backup-dir=/mnt/rsync-backup/patchNumber --exclude=README AbsolutePatchPath/* RemotePath/
  echo "\nFinished...";
  echo "";


-------------------------------------------------------------------------------------------



## Version 1:


#!/bin/bash

## Usage function declaration:

## Following is the usage functions
usage () {
  echo "Usage:"
  echo "Syntax: $0 -s SOURCE_PATCH_DIR -k PUBLIC_KEY -l FILE_HAVING_LIST_OF_SERVERS -c COMMAND[;COMMAND2;COMMANDn]\n"
  echo "Options:"
  echo "-s: source patch folder"
  echo "-k: Public key to access the remote hosts"
  echo "-l: File where we have hosts, with new line char."
  echo "-c: Input command(s) separated by \";\" on remote system(s)\n"
  echo "Example:"
  echo "$0 -s FILE_TO_COPY -k PATH_TO_PRIVATE_KEY -l FILE_HAVING_SERVER_NAME -c uptime;hostname;ls -l | grep -i test"
  echo "Above example will take the patch1 and use the private key and do the patch\
  on all the server that is listed on serverList.txt file and run all the provided command(s).\n"
}


#Get the arguments passed
# : for arg, Its good to have "h" and "help" at the end of the getopts options.
while getopts ":s:k:l:c:h:help" args; do
  case $args in
    h)
      usage;
      exit 0;
      ;;
    help)
      usage;
      exit 0;
      ;;
    s)
     ## Take the argument that is provided after -s with OPTARG
      SRC_FOLDER="${OPTARG}";;
    k)
      KEY_FILE="${OPTARG}";;
    l)
      SERVER_LIST="${OPTARG}";;
    c)
    COMMAND1="${OPTARG}";
# The following command will take multiple command with ; separated.
    COMMAND=`echo ${COMMAND1}|awk -F";" '{print $0}'`;;
   
    :)
      usage;
      exit 1;
      ;;
    ?)
      usage;
      exit 1;
      ;;
  esac
done



# Source folder has to be passed
if [ -z "${SRC_FOLDER}" ]; then # [ -z STRING ] True of the length if "STRING" is zero.
  echo "\nError: Source folder not found. Please provide absolute path of the source folder.\n";
  usage;
  exit 1;
fi

if [ ! -d "${SRC_FOLDER}" ] ; then # [ -d FILE ] True if FILE exists and is a directory.
  echo "\nError: Source need to be a valid folder.";
  usage;
  exit 1;
fi

# Key file exists?
if [ ! -e "${KEY_FILE}" ]; then # [ -e FILE ] True if FILE exists.
  echo "\nKey not found. Please provide absolute path of the key.\n";
  usage;
  exit 1;
fi

# server list exists?
if [ ! -f "${SERVER_LIST}" ]; then # [ -f FILE ] True if FILE exists and is a regular file.
  echo "\nFile not found. Please provide absolute path of the server_list files\n";
  usage;
  exit 1;
fi

# command provided?
if [ -z "${COMMAND}" ]; then # [ -z STRING ] True of the length if "STRING" is zero.
  echo "\nInput command not found:\n";
  usage;
  exit 1;
 fi

## Displaying the current values.
echo "Source Folder: ${SRC_FOLDER}";
echo "Key File: ${KEY_FILE}";
echo "Server list file: ${SERVER_LIST}";
echo "Command: ${COMMAND}";


## Executation of deployment.
for machine in `cat ${SERVER_LIST}`;
do
 echo "Start: Patch deployment on  ${machine}...";
  ssh -pPORT -i ${KEY_FILE} user@${machine} "${COMMAND}";
  done
#ssh -pPORT -i ${KEY_FILE} user@${machine} "uptime";
# sudo rsync -vvrlb --backup-dir=/mnt/rsync-backup/patchNumber --exclude=README AbsolutePatchPath/* RemotePath/
  echo "Finished.";
  echo "";

No comments:

Post a Comment