The GNU version of date
that you're used to on Linux supports a lot more date format than the version of date
on most other Unix variants. It also has many options that aren't present on other Unix variants. The only standard usage of date
is to display the current date according to a format specified with a +…
argument (and also a way for the system administrator to set the system date), and the -u
option to specify UTC instead of the local timezone.
On FreeBSD, you can use the date
utility to convert between date formats with the -f
option, e.g. date -f %s "$datum" +%Y-%m-%d
to convert a date expressed in seconds from the epoch to a human-readable format.
If you want fancy date parsing, you can install the coreutils
package, which contains GNU date. Or you can use the Date::Parse
module in Perl or dateutil
in Python.
To make a shell script stop on most errors, add set -e
just below the #!
line.
If you want to display an error explicitly, it's up to you to then quit the script with an error status. Print errors to stderr, not to stdout.
date -d "$datum" +"%Y-%m-%d" >/dev/null 2>&1 || {
echo "ERROR wrong format" >&2
exit 1
}
or
if ! date -d "$datum" +"%Y-%m-%d" >/dev/null 2>&1; then
echo "ERROR wrong format" >&2
exit 1
fi