If you want to capture user input in a shell script (I prefer bash! Do what you want, but this script is bash), you’re looking for the command read
.
This version gives the user a default start date 30 days in the past and will adjust the end date to be 30 days after whatever they enter.
#!/bin/bash
# Prompt a user for start and end dates
# Initialize start date variable with a default that is *30 days* ago
start_date=$( date -v -30d +"%Y-%m-%d" )
# Prompt the user for the start date
echo "What start date should I use? (format: YYYY-mm-dd, default: $start_date)"
read start_date_in
if [ "$start_date_in" != "" ]; then
# If the user entered a date, use it
start_date=$start_date_in
fi
# Initialize the end date as *30 days* after the start date
end_date=$( date -j -u -f "%Y-%m-%d" -v +30d "${start_date}" +"%Y-%m-%d" )
# Prompt the user for the end date
echo "What end date should I use? (format: YYYY-mm-dd, default: $end_date)"
read end_date_in
if [ "$end_date_in" != "" ]; then
# Again, if the user entered a date, use it
end_date=$end_date_in
fi
# Display the date span so the user knows!
echo "Processing between $start_date and $end_date"
# If you need to, convert these dates to timestamp for computer-friendly biz.
start_date_timestamp=$( date -j -u -f "%Y-%m-%d" "${start_date}" +"%s" )
end_date_timestamp=$( date -j -u -f "%Y-%m-%d" "${end_date}" +"%s" )