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