In linux, date
can help me to print the current time. If want to print the current time + 1 hour, what option should I give?
If you need to write scripts that work on both Linux servers and macOS workstations, you can silence the error of the first date
call and 'OR' it (||
) with the other. It doesn't matter which comes first.
date -u -d "+${max_age}Seconds" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || \
date -u -v "+${max_age}S" +"%Y-%m-%dT%H:%M:%SZ"
For example, this bash function uploads a file to AWS S3 and sets a Expires:
and Cache-Control:
headers.
s3_upload_cache_control(){
local max_age_seconds="${1}" ;shift # required
local bucket_path="${1}" ;shift # required
local filename="${1}" ;shift # required
local remote_filename="/${1:-${filename}}" # optional
local fmt="+%Y-%m-%dT%H:%M:%SZ"
aws s3 cp \
"${filename}" \
"s3://${bucket_path}${remote_filename}" \
--expires "$( \
date -u -d "+${max_age_seconds}Seconds" $fmt 2>/dev/null || \
date -u -v "+${max_age_seconds}S" $fmt )" \
--cache-control max-age=$max_age,public \
--acl public-read
}