---
# System prepended metadata

title: 'https://linuxize.com/post/bash-case-statement/'

---

```shell=bash
#!/bin/sh

##Setting global/main variables###
BIN_DIR=/apps/mktsys_app/scripts/sem/adobe_sem_hourly/bin
LOG_DIR=/apps/mktsys_app/scripts/sem/adobe_sem_hourly/log

adobe_sem_hourly_load=$BIN_DIR/adobe_sem_hourly_load.sh
hist_load_adobe_sem_hourly_by_loop=$BIN_DIR/hist_load_adobe_sem_hourly_by_loop.sh

todays_date=$(date +'%Y_%m_%d_%H%M')


##Checking command line arguments to make sure exactly 2 arguments are supplied##
if [ "$#" -ne 2 ]; then
    echo "Supplied invalid number of parameters. Expected 2. Found $#: $@"
fi

LOAD_TYPE=$1
LOAD_CONFIG=$2

##Setting log file name as hourly and historical load have distinct log file names
if [ $LOAD_TYPE == "adobe_sem_hourly_load" ]; then

    LOG_FILE=$LOG_DIR/manual_${LOAD_TYPE}_${todays_date}.log
else
    LOG_FILE=$LOG_DIR/${LOAD_TYPE}_${todays_date}.log
fi



##Logger function to log script output
function Log {

  LEVEL=$1
  MSG=$2
  DT=`date +"%Y-%m-%d %H:%M:%S"`
  echo "$DT $LEVEL ${FUNCNAME[0]} ${BASH_LINENO[0]} $MSG" >> $LOG_FILE
  
}


##Running reload script
function Run_Reload {

  LOAD_TYPE=$1
  BEGIN_DATE=$2
  END_DATE=$3
  PERIOD=$4
  
  Log INFO "Passing following variables to function: $LOAD_TYPE $BEGIN_DATE_FRMTTD $END_DATE_FRMTTD $PERIOD"
  
  case $LOAD_TYPE in
  
    adobe_sem_hourly_load)
    Log INFO "Running adobe_sem_hourly_load..."
    if [[ -z "$PERIOD" ]]; then
      Log ERROR "PERIOD is empty. Value is required for this load type"
      return 1
    fi
    
    Log INFO "nohup sh $adobe_sem_hourly_load $BEGIN_DATE $END_DATE $PERIOD >> \"${manual_adobe_sem_hourly_load}\" 2>&1 &"

    nohup sh $adobe_sem_hourly_load $BEGIN_DATE $END_DATE $PERIOD >> "${LOG_FILE}" 2>&1 &
    ;;
  
    hist_load_adobe_sem_hourly_by_loop)
    Log INFO "Running hist_load_adobe_sem_hourly_by_loop..."

    Log INFO "nohup sh $hist_load_adobe_sem_hourly_by_loop $BEGIN_DATE $END_DATE $PERIOD >> \"${hist_load_adobe_sem_hourly_by_loop}\" 2>&1 &"

    nohup sh $hist_load_adobe_sem_hourly_by_loop $BEGIN_DATE $END_DATE $PERIOD >> "${LOG_FILE}" 2>&1 &

    ;; 
  
    *)
      Log ERROR "Unknown load type: $LOAD_TYPE"
      return 1
      ;;
  esac
  
  
}

##Main (entry point)
Log INFO "Starting..."
Log INFO "Log file is $LOG_FILE"

Log INFO "Checking for any running process" 


#Checking to see if this LOAD_TYPE script is running
is_schdld_jobrunning=(`pgrep -f $LOAD_TYPE.sh`)

if [[ ! -z $is_schdld_jobrunning ]]; then
    is_schdld_job=$( ps $is_schdld_jobrunning )
    Log INFO "Scheduled hourly job is currently running" 
    Log INFO "Current Running Job: $is_schdld_job"
    Log INFO "Exiting..."
    return 1
fi

Log INFO "No hourly job currently running"

#Reading and Loading 
for line in $( cat ${LOAD_CONFIG} )
do
  Log INFO "Reading  ${LOAD_CONFIG} configuration file"
  BEGIN_DATE=$( echo $line | cut -d ',' -f 1 )
  PERIOD=$( echo $line | cut -d ',' -f 2 )
  
  ##formatted date
  BEGIN_DATE_FRMTTD=$( date -d $BEGIN_DATE +%F )
  END_DATE_FRMTTD=$( date -d "$BEGIN_DATE_FRMTTD next day" +%F )

  Run_Reload $LOAD_TYPE $BEGIN_DATE_FRMTTD $END_DATE_FRMTTD $PERIOD
done

Log INFO "Done..."

```

# https://linuxize.com/post/bash-case-statement/

File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).