I started scripting a 2 weeks ago and wrote a script which checks every sub directory until a certain level for the largest files.
My question would be if I'm using the getopts correctly and/or if I could improve the script in general
I'm using a getopt function for the specify options like:
c= part of the start directory for the searchl= the number of output lines -> default value is 10 if option not useds= for the search string which is used in the grep command -> default value is today's date
I am not sure if I used the getopt correctly and would like to improve the code or completely rewrite it.
#!/usr/bin/bash
LNE=--------------------------------------------------------------------------
NUMBER_RESULT_DEFAULT=10;
#------------------------
DATE=#$(date '+%y%m%d');
INTEGER_CHECK='^[0-9]+$';
CHAR_CHECK='^[a-z]+$';
#------------------------
# [---CodeBlock Input check for valid arguments and their values---]
echo "$LNE";
HelpFunction ()
{
echo -e "\n"
echo -e "--------------------------------------\n"
echo -e "Script for tracking filegrowth on system \n"
echo -e "--------------------------------------\n"
echo -e "-h process option for user manual \n"
echo -e "-c process option for CUSTOMER - 3- Letter token for tenant directory \n"
echo -e "-l process option for setting lengh of result - INTEGER values only \n"
echo -e "-s process option for search string \n"
}
# Failsafe for mandatory option "-c" to specifiy directory starting point
if ! [[ $@ =~ '-c ' ]]; then
echo "#>> Mandatory argument missing. Exit ReturnCode 8";
HelpFunction;
exit 8;
fi
# Set default value for output length if "-l" option not used
if ! [[ $@ =~ '-l ' ]]; then
NUMBER_RESULT="$NUMBER_RESULT_DEFAULT";
echo "#>> Output length set to [ -DEFAULT- ] value [ $NUMBER_RESULT_DEFAULT ]";
echo "$LNE":
fi
if ! [[ $@ =~ '-s ' ]]; then
SEARCH_STRING="$DATE";
echo "#>> No [-s] search string specified - set [-DEFAULT-] to today's date;"
echo "$LNE";
fi
while getopts ":hc:l:s:" opt; do
case ${opt} in
h ) # process option -h for user manual
HelpFunction
exit 0
;;
c ) # process option -c for tenant
TENANT=$OPTARG
! [[ $TENANT =~ $CHAR_CHECK ]] &&
echo "#>> 3-Letter tenant directory missing - Non-Letter character are not allowed" &&
echo "#>> Exiting with ReturnCode: 8" &&
echo $LNE && exit 8;
;;
l ) # process option -l for number of output lines
NUMBER_RESULT=$OPTARG
! [[ $NUMBER_RESULT =~ $INTEGER_CHECK ]] &&
echo "#>> Value for for output length has to be an INTEGER number" &&
echo "#>> Exiting with ReturnCode: 8" &&
echo $LNE && exit 8;
;;
s ) # process option -s for search string
SEARCH_STRING=$OPTARG
;;
\? ) # undefined option used
echo -e "\n"
echo -e "#>> ERROR unknown options used \n" 1>&2
echo -e "use [ $0 -h ] for help\n"
exit 8
;;
esac
done
echo "# *** Starting directory set to [ $SHARED_BASE_DIR/$TENANT ]";
echo "# *** Output length set to value [ $NUMBER_RESULT ]";
echo $LNE;
#------------------------
# Get basedirecory including subdir "/data"
ARRAY_DATA_DIR=($(find $SHARED_BASE_DIR/$TENANT -maxdepth 3 -type d -name 'data' 2> /dev/null));
for DATA_DIR in "${ARRAY_DATA_DIR[@]}"; do
# Get all sub directories
ARRAY_HLQ_DIR=($(ls -1 $DATA_DIR/))
for HLQ_DIR in ${ARRAY_HLQ_DIR[@]}; do
if ls $DATA_DIR/$HLQ_DIR/*$SEARCH_STRING* 1> /dev/null 2>$1; then
# Get all files in directory and sort by size - output only n number of lines specified as argument
OUTPUT=$(ls -1fsh $DATA_DIR/$HLQ_DIR/*$SEARCH_STRING* 2> /dev/null | sort -rh | head -$NUMBER_RESULT)
# Split ls output get result formatted without whitespace
echo "[ SIZE ],[ --- $HLQ_DIR --- ]" | awk -F "," '{printf("%8s %s\n", $1,$2)}'
echo "${OUTPUT[0]}" | awk '{printf("%8s %s\n", $1,$2)}'
echo "$LNE"
fi
done
done