UsiScript
#!/bin/bash ## Original author = Sylvain H. ## description ## This template give an example of severals arguments given to a script, with a getops loop and shift keywords. It check and get the varibales ## Usage function ## A check if done to see if the arguments are empty or not. ## A lot of checks are done to be sure that the variables contains good values. ## A log file is created to have a trace. ################################# #-------FUNCTIONS---------- ################################# usage() { echo echo "$PGR_NAME [-h] -p
-v
" echo echo "This script do things" echo " Example : $PGR_NAME -p /usr/product/php/ -v 5.6.12" echo " " echo " -p : The main folder" echo " -v : The version" exit 1 } ################################# #-------END - FUNCTIONS---------- ################################# #Get Arguments Parameters #If there is no argument, we display the Usage if [ $# -eq 0 ]; then usage fi #Check the arguments given by the user while [[ $# > 0 ]] do opt="$1" case $opt in -h) usage ;; -p) varMYFOLDER=$2 shift;; -v) varVERSION=$2 shift;; *) usage ;; esac shift done # CHECK VARIABLES CONTENT if [ "$varMYFOLDER" = "" ] then varMYFOLDER=$varMYBASEDEF fi if [ "$varMYFOLDER" = "" ] then echo "No folder given" exit 31 fi if [ "$varVERSION" = "" ] then echo "No version given" exit 32 fi ######################################### #------VARIABLES-AND-CONFIGURATION------ ######################################### NOWDATE=$(date +"%Y%m%d-%Hh%M") PGR_NAME=$(basename "$0") CURRENTDIR_PRG=$(pwd) #LOG_COMMANDS = list all the commands in order to use it after LOG_COMMANDS=${CURRENTDIR_PRG}"/listes_commands.log" ################################# #-------END VARIABLES------------ ################################# ################################# #-----MAIN----- ################################# ## CHECKS #Check if the logfile $LOG_COMMANDS exist. if yes, we backup it if [ -f "$LOG_COMMANDS" ]; then cp $LOG_COMMANDS ${LOG_COMMANDS}_$NOWDATE".old" rm -f $LOG_COMMANDS fi #Create a new logfile LOG_COMMANDS echo " " > $LOG_COMMANDS #Check if the directory $PHPPREFIXBASE exist if [ ! -d "$varMYFOLDER" ]; then echo "! The folder $varMYFOLDER doesn't exist." echo "We create it" mkdir -p $varMYFOLDER fi #second check to see if the folder has been created if [ ! -d "$varMYFOLDER" ]; then echo "! The folder $varMYFOLDER doesn't exist." exit 41 fi