forked from epety/100-shell-script-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path050-set-date.sh
executable file
·36 lines (28 loc) · 906 Bytes
/
050-set-date.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/sh
# setdate - friendly front-end to the date command
# Date wants: [[[[[cc]yy]mm]dd]hh]mm[.ss]
askvalue()
{
# $1 = field name, $2 = default value, $3 = max value,
# $4 = required char/digit length
echo -n "$1 [$2] : "
read answer
if [ ${answer:=$2} -gt $3 ] ; then
echo "$0: $1 $answer is invalid"; exit 0
elif [ "$(( $(echo $answer | wc -c) - 1 ))" -lt $4 ] ; then
echo "$0: $1 $answer is too short: please specify $4 digits"; exit 0
fi
eval $1=$answer
}
eval $(date "+nyear=%Y nmon=%m nday=%d nhr=%H nmin=%M")
askvalue year $nyear 3000 4
askvalue month $nmon 12 2
askvalue day $nday 31 2
askvalue hour $nhr 24 2
askvalue minute $nmin 59 2
squished="$year$month$day$hour$minute"
# or, if you're running a Linux system:
# squished="$month$day$hour$minute$year"
echo "Setting date to $squished. You might need to enter your sudo password:"
sudo date $squished
exit 0