Linux Shell Script

This post will document some useful data in order to make shell scripts such a best practices and tools

Sample shell script for bash

​#!/bin/bash
# Arguments = -n name -l lastname -p password

usage()
{
cat << EOF
usage: $0 options

This script receives name and lastname

OPTIONS:

 -h Show this message
 -n Name
 -l Last name
 -p Password
 -v Verbose

EOF
}

NAME=
LASTNAME=
PASSWORD=
VERBOSE=

while getopts "ht:n:l:p:v" OPTION
do
 case $OPTION in
  h)
   usage
   exit 1
   ;;
  n)
   NAME=$OPTARG
   ;;
  l)
   LASTNAME=$OPTARG
   ;;
  p)
   PASSWORD=$OPTARG
   ;;
  v)
   VERBOSE=$OPTARG
   ;;
 esac
done

if [[ -z $NAME ]] || [[ -z $LASTNAME ]] || [[ -z $PASSWORD ]]
then
  usage
  exit 1
fi

echo "your name is : $NAME"
echo "your lastname is : $LASTNAME"
echo "your password is : $PASSWORD"

Getopt VS Getopts

Getopt do handle long double dash parameters (like --name) while getopts handle short parameter (like -n)

Reference page

Best Practices

Reference page

Conditional Expressions

Reference page