Skip to content

Instantly share code, notes, and snippets.

@eduardoaugustojulio
Last active September 28, 2023 09:52
Show Gist options
  • Select an option

  • Save eduardoaugustojulio/fa83cf85efa39919d6a70ca679e91f28 to your computer and use it in GitHub Desktop.

Select an option

Save eduardoaugustojulio/fa83cf85efa39919d6a70ca679e91f28 to your computer and use it in GitHub Desktop.
dialog interface to set date in linux based system.
#!/bin/bash
#Licensed to the Apache Software Foundation (ASF) under one
#or more contributor license agreements. See the NOTICE file
#distributed with this work for additional information
#regarding copyright ownership. The ASF licenses this file
#to you under the Apache License, Version 2.0 (the
#"License"); you may not use this file except in compliance
#with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing,
#software distributed under the License is distributed on an
#"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#KIND, either express or implied. See the License for the
#specific language governing permissions and limitations
#under the License.
TimeZoneOptionsByRegion ()
{
options=$(cd /usr/share/zoneinfo/$1 && find . | sed "s|^\./||" | sed "s/^\.//" | sed '/^$/d')
}
TimeZoneRegions ()
{
regions=$(find /usr/share/zoneinfo/. -maxdepth 1 -type d | cut -d "/" -f6 | sed '/^$/d')
}
TimeZoneSelectionMenu ()
{
TimeZoneRegions
regionsArray=()
while read name; do
regionsArray+=($name "")
done <<< "$regions"
region=$(dialog --stdout \
--title "Timezones" \
--backtitle " " \
--ok-label "Next" \
--no-cancel \
--menu "Select a continent or ocean from the menu:" \
20 30 30 \
"${regionsArray[@]}")
TimeZoneOptionsByRegion $region
optionsArray=()
while read name; do
offset=$(TZ="$region/$name" date +%z | sed "s/00$/:00/g")
optionsArray+=($name "($offset)")
done <<< "$options"
tz=$(dialog --stdout \
--title "Timezones" \
--backtitle " " \
--ok-label "Next" \
--cancel-label "Back to Regions" \
--menu "Select your timezone in ${region}:" \
20 40 30 \
"${optionsArray[@]}")
if [[ -z "${tz// }" ]]; then
TimeZoneSelectionMenu
else
echo "/usr/share/zoneinfo/$region/$tz"
fi
}
DateSelectMenu()
{
DATE=$(date "+%d %m %Y")
dat=$(dialog --stdout --date-format "%Y-%m-%d" --title "Calendar" --calendar "Select a date:" 0 0 $DATE)
case $? in
0)
echo "$dat" ;;
esac
}
TimeSelectMenu()
{
TIME=$(date "+%H %M %S")
tim=$(dialog --stdout --title "Time" --timebox "Set the time:" 0 0 $TIME)
case $? in
0)
echo "$tim" ;;
esac
}
if [ "$(whoami)" != 'root' ]; then
echo "Sorry, only the superuser can $0."
exit 1;
fi
tZone="$(TimeZoneSelectionMenu)"
ln -fs $tZone /etc/localtime
dat="$(DateSelectMenu)"
date -s $dat
tim="$(TimeSelectMenu)"
date +%T -s "$tim"
hwclock -w
echo "$dat $tim $tZone"
@eduardoaugustojulio
Copy link
Copy Markdown
Author

eduardoaugustojulio commented Feb 5, 2018

TO DO
1 - Select timezone.
2 - Set date after all work.
3 - Add ASF License.
4- Add update hardware clock from system.

@seamaner
Copy link
Copy Markdown

at the time dialog, minutes and senconds do not change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment