I have below bash script. I want you to change it so it wont be so complicated. I want function checks as return 1 not empty variables. Also make variable names more explaining. If possible do not use too much ifs inside if.


source ~/scripts/.functions.sh
script_start

# help input
print_help() {
  lecho 1 "Usage: $0 [phone_number|help] [date1(YYYY-MM-DD)|days(1-30)|today] [date2(YYYY-MM-DD)|days(1-30)]"
}

# Convert date from either YYYY-MM-DD or DD-MM-YYYY to YYYY-MM-DD. Expects only digits and - as separator.
convert_to_yyyy_mm_dd() {
  local input=$1
  # Validate format: only digits and -
  if [[ ! "$input" =~ ^[0-9\-]+$ ]]; then
    echo ""
    return
  fi
  # Count parts split by -
  IFS='-' read -r part1 part2 part3 <<< "$input"
  # Parts must be 3
  if [[ -z "$part1" || -z "$part2" || -z "$part3" ]]; then
    echo ""
    return
  fi

  # Determine formats:
  # If first part has length=4, assume YYYY-MM-DD
  # Else assume DD-MM-YYYY
  if [[ ${#part1} -eq 4 ]]; then
    # YYYY-MM-DD
    yyyy=$part1
    mm=$part2
    dd=$part3
  else
    # DD-MM-YYYY
    dd=$part1
    mm=$part2
    yyyy=$part3
  fi

  # Basic validation
  if ! [[ $yyyy =~ ^[0-9]{4}$ && $mm =~ ^[0-9]{1,2}$ && $dd =~ ^[0-9]{1,2}$ ]]; then
    echo ""
    return
  fi

  # Pad mm and dd with leading zeros if needed
  mm=$(printf "%02d" $mm)
  dd=$(printf "%02d" $dd)

  echo "${yyyy}-${mm}-${dd}"
}

# Check if date1 <= date2, output properly as date_var_first and date_var_last (YYYY-MM-DD)
# Inputs are dates in YYYY-MM-DD format
set_date_vars_order() {
  local d1=$1
  local d2=$2

  # Convert to timestamps to compare
  local t1=$(date -d "$d1" +%s 2>/dev/null)
  local t2=$(date -d "$d2" +%s 2>/dev/null)
  if [[ -z "$t1" || -z "$t2" ]]; then
    # invalid dates
    date_var_first=""
    date_var_last=""
    return
  fi

  if (( t1 <= t2 )); then
    date_var_first=$d1
    date_var_last=$d2
  else
    date_var_first=$d2
    date_var_last=$d1
  fi
}

# Global outputs variables
digit=""
date_var_first=""
date_var_last=""
day_interval=""

# Input arguments (max 4)
arg1=${1:-}
arg2=${2:-}
arg3=${3:-}

# Warn and ignore inputs beyond first 3
if (( $# > 3 )); then
  echo "Warning: Only the first 3 inputs will be used. Ignoring extra inputs."
fi

# Start processing

if [[ -z "$arg1" || "$arg1" == "help" ]]; then
  # Scenario 1 or no input = help
  print_help
  return 0
fi

# Validate and normalize phone number in arg1

if ! [[ "$arg1" =~ ^[0-9]+$ ]]; then
  lecho 2 "Phone number must contain digits only."
  print_help
  script_end
fi

len=${#arg1}
if (( len < 10 || len > 12 )); then
  lecho 2 "Phone number length must be between 10 and 12 digits."
  print_help
  script_end
fi

digit="$arg1"

# Transformations for phone number prefix
if [[ ${digit:0:1} == "5" && $len -eq 10 ]]; then
  digit="90$digit"
elif [[ ${digit:0:1} == "0" && $len -eq 11 ]]; then
  digit="9$digit"
fi

# Helper functions for input type detection
is_valid_date() {
  # Accept format YYYY-MM-DD or DD-MM-YYYY, digits and - only, uses convert function to test
  local d="$1"
  local conv
  conv=$(convert_to_yyyy_mm_dd "$d")
  if [[ -z "$conv" ]]; then
    return 1
  fi
  return 0
}

is_day_number() {
  local d="$1"
  if [[ "$d" =~ ^[0-9]{1,2}$ ]]; then
    if (( d >= 1 && d <= 30 )); then
      return 0
    fi
  fi
  return 1
}

is_today() {
  [[ "$1" == "today" ]]
}

today_date=$(date +%F) # YYYY-MM-DD today


db_connection_host_name="pep"
db_connection_query="SELECT TO_CHAR(TIME, 'YYYY-MM-DD HH24:MI:SS') AS TIME , TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS TIMESTAMP , ORIG_ADDRESS , DEST_ADDRESS , IS_STATUS_REPORT , SUCCESS , RETRY_COUNT , ERROR , CAUSE , ERROR_TYPE , CONCAT_INFO , NVL(SMPP_SYSTEM_ID, '@') AS SMPP_SYSTEM_ID , SMSC_NAME , MESSAGE_REF , SM_FROM_SERVICE AS DELIVERY_SM , NVL(SMPP_MSG_ID, '@') AS SMPP_MSG_ID , RECV_TIMESTAMP , NVL(DEST_OPERATOR_ID, '@') AS DEST_OPERATOR_ID, NVL(LOAD_SHARE_RULE, '@') AS LOAD_SHARE_RULE , NETWORK_SOURCE , PART , TO_CHAR(INSERT_DATE, 'YYYY-MM-DD HH24:MI:SS') AS INSERT_DATE , FILE_NAME , NVL(CLIENT_NAME, '@') AS CLIENT_NAME , NVL(SMPP_SYSTEM_ID, '@') || '(' || NVL(DEST_OPERATOR_ID, '@') || ')' AS DEST_ID , ERROR || '/' || CAUSE || '/' || ERROR_TYPE AS ERROR_DETAIL FROM COPS_SMS.SMS_CDR WHERE (DEST_ADDRESS LIKE '$digit' OR ORIG_ADDRESS LIKE '$digit') AND INSERT_DATE BETWEEN trunc(sysdate-$days) AND SYSDATE ORDER BY TIME DESC;"
db_connection_query_col="
    COL SUCCESS format 99
    COL ERROR format a5
    COL CAUSE format 999
    COL SMSC_NAME format a9
    COL CONCAT_INFO format a11
    COL SM_FROM_SERVICE format a11
    COL ERROR_TYPE format a10
    COL SMPP_MSG_ID format a28
    COL FILE_NAME format a28
    COL LOAD_SHARE_RULE format a15
    COL DEST_OPERATOR_ID format a16
    COL NETWORK_SOURCE format a14
    COL PART format 9999
    COL DEST_ID format a17
    COL ERROR_DETAIL format a12"

# 1st input only
if [[ -n "$arg1" && -z "$arg2" && -z "$arg3" ]]; then
  day_interval=7
  date_var_first=$today_date
  d_calc=$(date -d "$date_var_first - $day_interval days" +%F 2>/dev/null)
  if [[ -z "$d_calc" ]] ; then lecho 2 "Invalid date calculation." ; script_end ; fi
  db_connection_query="SELECT TO_CHAR(TIME, 'YYYY-MM-DD HH24:MI:SS') AS TIME , TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS TIMESTAMP , ORIG_ADDRESS , DEST_ADDRESS , IS_STATUS_REPORT , SUCCESS , RETRY_COUNT , ERROR , CAUSE , ERROR_TYPE , CONCAT_INFO , NVL(SMPP_SYSTEM_ID, '@') AS SMPP_SYSTEM_ID , SMSC_NAME , MESSAGE_REF , SM_FROM_SERVICE AS DELIVERY_SM , NVL(SMPP_MSG_ID, '@') AS SMPP_MSG_ID , RECV_TIMESTAMP , NVL(DEST_OPERATOR_ID, '@') AS DEST_OPERATOR_ID, NVL(LOAD_SHARE_RULE, '@') AS LOAD_SHARE_RULE , NETWORK_SOURCE , PART , TO_CHAR(INSERT_DATE, 'YYYY-MM-DD HH24:MI:SS') AS INSERT_DATE , FILE_NAME , NVL(CLIENT_NAME, '@') AS CLIENT_NAME , NVL(SMPP_SYSTEM_ID, '@') || '(' || NVL(DEST_OPERATOR_ID, '@') || ')' AS DEST_ID , ERROR || '/' || CAUSE || '/' || ERROR_TYPE AS ERROR_DETAIL FROM COPS_SMS.SMS_CDR WHERE (DEST_ADDRESS LIKE '$digit' OR ORIG_ADDRESS LIKE '$digit') AND insert_date between TO_DATE('$d_calc','YYYY-MM-DD') AND TO_DATE('$date_var_first','YYYY-MM-DD') ORDER BY insert_date desc;"
  func_db_connection
  script_end
fi

# 1st and 2nd input
if [[ -n "$arg1" && -n "$arg2" && -z "$arg3" ]]; then
  if is_valid_date "$arg2"; then
    # phone number + date + day_interval=7
    day_interval=7
    date_var_first=$(convert_to_yyyy_mm_dd "$arg2")
    if [[ -z "$date_var_first" ]]; then
      echo "Error: Invalid date format for second input."
      exit 1
    fi
    d_calc=$(date -d "$date_var_first - $day_interval days" +%F 2>/dev/null)
    if [[ -z "$d_calc" ]]; then lecho 2 "Date calculation failed." ; script_end ; fi
    db_connection_query="SELECT TO_CHAR(TIME, 'YYYY-MM-DD HH24:MI:SS') AS TIME , TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS TIMESTAMP , ORIG_ADDRESS , DEST_ADDRESS , IS_STATUS_REPORT , SUCCESS , RETRY_COUNT , ERROR , CAUSE , ERROR_TYPE , CONCAT_INFO , NVL(SMPP_SYSTEM_ID, '@') AS SMPP_SYSTEM_ID , SMSC_NAME , MESSAGE_REF , SM_FROM_SERVICE AS DELIVERY_SM , NVL(SMPP_MSG_ID, '@') AS SMPP_MSG_ID , RECV_TIMESTAMP , NVL(DEST_OPERATOR_ID, '@') AS DEST_OPERATOR_ID, NVL(LOAD_SHARE_RULE, '@') AS LOAD_SHARE_RULE , NETWORK_SOURCE , PART , TO_CHAR(INSERT_DATE, 'YYYY-MM-DD HH24:MI:SS') AS INSERT_DATE , FILE_NAME , NVL(CLIENT_NAME, '@') AS CLIENT_NAME , NVL(SMPP_SYSTEM_ID, '@') || '(' || NVL(DEST_OPERATOR_ID, '@') || ')' AS DEST_ID , ERROR || '/' || CAUSE || '/' || ERROR_TYPE AS ERROR_DETAIL FROM COPS_SMS.SMS_CDR WHERE (DEST_ADDRESS LIKE '$digit' OR ORIG_ADDRESS LIKE '$digit') AND insert_date between TO_DATE('$d_calc','YYYY-MM-DD') AND TO_DATE('$date_var_first','YYYY-MM-DD') ORDER BY insert_date desc;"
    func_db_connection
    script_end
  elif is_day_number "$arg2"; then
    # Scenario 6: phone_number + day_number
    day_interval=$arg2
    date_var_first=$today_date
    d_calc=$(date -d "$date_var_first - $day_interval days" +%F 2>/dev/null)
    if [[ -z "$d_calc" ]]; then
      echo "Error: Date calculation failed."
      exit 1
    fi
    db_connection_query="SELECT TO_CHAR(TIME, 'YYYY-MM-DD HH24:MI:SS') AS TIME , TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS TIMESTAMP , ORIG_ADDRESS , DEST_ADDRESS , IS_STATUS_REPORT , SUCCESS , RETRY_COUNT , ERROR , CAUSE , ERROR_TYPE , CONCAT_INFO , NVL(SMPP_SYSTEM_ID, '@') AS SMPP_SYSTEM_ID , SMSC_NAME , MESSAGE_REF , SM_FROM_SERVICE AS DELIVERY_SM , NVL(SMPP_MSG_ID, '@') AS SMPP_MSG_ID , RECV_TIMESTAMP , NVL(DEST_OPERATOR_ID, '@') AS DEST_OPERATOR_ID, NVL(LOAD_SHARE_RULE, '@') AS LOAD_SHARE_RULE , NETWORK_SOURCE , PART , TO_CHAR(INSERT_DATE, 'YYYY-MM-DD HH24:MI:SS') AS INSERT_DATE , FILE_NAME , NVL(CLIENT_NAME, '@') AS CLIENT_NAME , NVL(SMPP_SYSTEM_ID, '@') || '(' || NVL(DEST_OPERATOR_ID, '@') || ')' AS DEST_ID , ERROR || '/' || CAUSE || '/' || ERROR_TYPE AS ERROR_DETAIL FROM COPS_SMS.SMS_CDR WHERE (DEST_ADDRESS LIKE '$digit' OR ORIG_ADDRESS LIKE '$digit') AND insert_date between TO_DATE('$d_calc','YYYY-MM-DD') AND TO_DATE('$date_var_first','YYYY-MM-DD') ORDER BY insert_date desc;"
    func_db_connection
    script_end
  elif is_today "$arg2"; then
    # Scenario 7: phone_number + today
    db_connection_query="SELECT TO_CHAR(TIME, 'YYYY-MM-DD HH24:MI:SS') AS TIME , TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS TIMESTAMP , ORIG_ADDRESS , DEST_ADDRESS , IS_STATUS_REPORT , SUCCESS , RETRY_COUNT , ERROR , CAUSE , ERROR_TYPE , CONCAT_INFO , NVL(SMPP_SYSTEM_ID, '@') AS SMPP_SYSTEM_ID , SMSC_NAME , MESSAGE_REF , SM_FROM_SERVICE AS DELIVERY_SM , NVL(SMPP_MSG_ID, '@') AS SMPP_MSG_ID , RECV_TIMESTAMP , NVL(DEST_OPERATOR_ID, '@') AS DEST_OPERATOR_ID, NVL(LOAD_SHARE_RULE, '@') AS LOAD_SHARE_RULE , NETWORK_SOURCE , PART , TO_CHAR(INSERT_DATE, 'YYYY-MM-DD HH24:MI:SS') AS INSERT_DATE , FILE_NAME , NVL(CLIENT_NAME, '@') AS CLIENT_NAME , NVL(SMPP_SYSTEM_ID, '@') || '(' || NVL(DEST_OPERATOR_ID, '@') || ')' AS DEST_ID , ERROR || '/' || CAUSE || '/' || ERROR_TYPE AS ERROR_DETAIL FROM COPS_SMS.SMS_CDR PARTITION FOR (TO_DATE('$today_date', 'YYYY-MM-DD')) WHERE (DEST_ADDRESS LIKE '$digit' OR ORIG_ADDRESS LIKE '$digit') ORDER BY insert_date desc;"
    func_db_connection
    script_end
  else
    lecho 2 "Second input must be a valid date(YYYY-MM-DD), day number(1-30), or 'today'."
    print_help
    script_end
  fi
fi

# All 3 inputs
if [[ -n "$arg3" && -n "$arg2" ]]; then
  # arg2 & arg3 can be date or day_number or today according to your rules

  # Check if arg2 and arg3 both dates:
  if is_valid_date "$arg2" && is_valid_date "$arg3"; then
    d1=$(convert_to_yyyy_mm_dd "$arg2")
    d2=$(convert_to_yyyy_mm_dd "$arg3")
    if [[ -z "$d1" || -z "$d2" ]]; then
      echo "Error: Invalid dates given."
      exit 1
    fi
    set_date_vars_order "$d1" "$d2"
    if [[ -z "$date_var_first" || -z "$date_var_last" ]]; then
      echo "Error: Unable to parse or order dates."
      exit 1
    fi
    db_connection_query="SELECT TO_CHAR(TIME, 'YYYY-MM-DD HH24:MI:SS') AS TIME , TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS TIMESTAMP , ORIG_ADDRESS , DEST_ADDRESS , IS_STATUS_REPORT , SUCCESS , RETRY_COUNT , ERROR , CAUSE , ERROR_TYPE , CONCAT_INFO , NVL(SMPP_SYSTEM_ID, '@') AS SMPP_SYSTEM_ID , SMSC_NAME , MESSAGE_REF , SM_FROM_SERVICE AS DELIVERY_SM , NVL(SMPP_MSG_ID, '@') AS SMPP_MSG_ID , RECV_TIMESTAMP , NVL(DEST_OPERATOR_ID, '@') AS DEST_OPERATOR_ID, NVL(LOAD_SHARE_RULE, '@') AS LOAD_SHARE_RULE , NETWORK_SOURCE , PART , TO_CHAR(INSERT_DATE, 'YYYY-MM-DD HH24:MI:SS') AS INSERT_DATE , FILE_NAME , NVL(CLIENT_NAME, '@') AS CLIENT_NAME , NVL(SMPP_SYSTEM_ID, '@') || '(' || NVL(DEST_OPERATOR_ID, '@') || ')' AS DEST_ID , ERROR || '/' || CAUSE || '/' || ERROR_TYPE AS ERROR_DETAIL FROM COPS_SMS.SMS_CDR WHERE (DEST_ADDRESS LIKE '$digit' OR ORIG_ADDRESS LIKE '$digit') AND insert_date BETWEEN TO_DATE('$date_var_first','YYYY-MM-DD') AND TO_DATE('$date_var_last','YYYY-MM-DD') ORDER BY insert_date desc;"
    func_db_connection
    script_end
  fi

  # Check if arg2 is date and arg3 is day_number
  if is_valid_date "$arg2" && is_day_number "$arg3"; then
    d1=$(convert_to_yyyy_mm_dd "$arg2")
    if [[ -z "$d1" ]]; then
      echo "Error: Invalid date1."
      exit 1
    fi
    date_var_first=$d1
    day_interval=$arg3

    d_calc=$(date -d "$date_var_first - $day_interval days" +%F 2>/dev/null)
    if [[ -z "$d_calc" ]]; then
      echo "Error: Invalid date calculation."
      exit 1
    fi

    db_connection_query="SELECT TO_CHAR(TIME, 'YYYY-MM-DD HH24:MI:SS') AS TIME , TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS TIMESTAMP , ORIG_ADDRESS , DEST_ADDRESS , IS_STATUS_REPORT , SUCCESS , RETRY_COUNT , ERROR , CAUSE , ERROR_TYPE , CONCAT_INFO , NVL(SMPP_SYSTEM_ID, '@') AS SMPP_SYSTEM_ID , SMSC_NAME , MESSAGE_REF , SM_FROM_SERVICE AS DELIVERY_SM , NVL(SMPP_MSG_ID, '@') AS SMPP_MSG_ID , RECV_TIMESTAMP , NVL(DEST_OPERATOR_ID, '@') AS DEST_OPERATOR_ID, NVL(LOAD_SHARE_RULE, '@') AS LOAD_SHARE_RULE , NETWORK_SOURCE , PART , TO_CHAR(INSERT_DATE, 'YYYY-MM-DD HH24:MI:SS') AS INSERT_DATE , FILE_NAME , NVL(CLIENT_NAME, '@') AS CLIENT_NAME , NVL(SMPP_SYSTEM_ID, '@') || '(' || NVL(DEST_OPERATOR_ID, '@') || ')' AS DEST_ID , ERROR || '/' || CAUSE || '/' || ERROR_TYPE AS ERROR_DETAIL FROM COPS_SMS.SMS_CDR WHERE (DEST_ADDRESS LIKE '$digit' OR ORIG_ADDRESS LIKE '$digit') AND insert_date between TO_DATE('$d_calc','YYYY-MM-DD') AND TO_DATE('$date_var_first','YYYY-MM-DD') ORDER BY insert_date desc;"
    func_db_connection
    script_end
  fi

  lecho 2 "Invalid combination of inputs."
  print_help
  script_end
fi

# Refining table
awk -F'|' '{
    if ($6 == " 1       " || $6 == " 6       ") {
        $6 = " OK      "
    } else if ($6 == " 3       ") {
        $6 = " TIMEOUT "
    }
    if ($15 == " 1           ") {
        $15 = " OK          "
    } else if ($15 == " 0           ") {
        $15 = " NOK         "
    }
    print $2"|"$3"|"$4"|"$6"|"$7"|"$11"|"$13"|"$15"|"$25"|"$26
}' $output_refined > $output_finnished

echo ""
echo "To see full output cat $output_refined"
echo ""
cat $output_finnished
echo ""

script_end