#!/bin/bash #change time using hwclock and date -s clear echo -e -n "Please enter the date and time you wish to set\n" echo -e -n "You need to set the time according to the UTC/GMT timezone please check https://time.is/UTC\n\n" get_date () { local field_name min max digits field_name="$1" min="$2" max="$3" digits="$4" echo -e -n "Please insert $field_name (between $min-$max) (Enter key to accept $min)\n" read -r value echo -e "\n" #if enter if [[ $value = "" ]]; then value=$min fi #must be a number between $2 and $3 while [[ ! $value =~ ^[0-9]*$ ]] || [[ ${value#0} -lt $min ]] || [[ ${value#0} -gt $max ]]; do echo -e -n "$field_name is wrong: you entered \"$value\". Please try again, it must be a number between $min and $max (press Enter to accept $min) \n" read -r value echo -e "\n" if [[ $value = "" ]]; then value=$min fi done # Pad with zeroes to digits value="$(printf "%0${digits}u" "$value")" } get_date "year" "2024" "2200" "4" year=$value get_date "month" "01" "12" "2" month=$value get_date "day" "01" "31" "2" day=$value get_date "hour" "00" "23" "2" hour=$value get_date "minute" "00" "59" "2" min=$value get_date "second" "00" "59" "2" sec=$value if ! date -s "$year-$month-$day $hour:$min:$sec" &>/dev/null; then echo "The date is not correct, press any key to try again" echo -e "\n" read -n 1 nothing clear change-time else hwclock -w echo -e "The system date has been sucessfully set to $year-$month-$day $hour:$min:$sec" echo -e "\n" echo -e "Press any key to return to the menu" echo -e "\n" read -n 1 nothing fi