Skip to content

Instantly share code, notes, and snippets.

@barronh
Created March 6, 2024 16:17
Show Gist options
  • Save barronh/8acdbfa7407237a51312afb19d6ae7f0 to your computer and use it in GitHub Desktop.
Save barronh/8acdbfa7407237a51312afb19d6ae7f0 to your computer and use it in GitHub Desktop.
Run Scripts with strftime to make day-specific environments from a template file.
#!/bin/bash
POSITIONAL_ARGS=()
PREFIX="ENV"
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
echo "Usage"
echo "-----"
echo " $0 [--prefix=PREFIX] TEMPLATE_ENV STARTDAY [ENDDAY [SCRIPT]]"
echo ""
echo " PREFIX : Environments output as PREFIX_DAY.env. Defaults to ENV"
echo " TEMPLATE_ENV : file with environment variables on individual lines"
echo " STARTDAY : Day to start making day specific environments"
echo " ENDDAY : Optional day to end (defaults to STARTDAY)"
echo " SCRIPT : Script to run with environment in Optional day to end (defaults to STARTDAY)"
echo ""
echo "Example"
echo "-------"
echo "This example uses strftime to copy a single template environment and make"
echo "it day-specific. A simple example is shown below and assumes that temp.env"
echo "and temp.sh exist as show below. temp.sh must be executable."
echo ""
echo " > cat temp.env"
echo " GR_EMIS_001=\"a/%Y-%m-%d.nc\""
echo " GR_EMIS_003=\"b/%Y-%m-%d.nc\""
echo " GR_EMIS_002=\"c/%Y-%m-%d.nc\""
echo ""
echo " > cat temp.sh"
echo " echo \$GR_EMIS_001"
echo " echo \$GR_EMIS_002"
echo " echo \$GR_EMIS_003"
echo ""
echo " # Run temp.sh with each day specific"
echo " > $0 temp.env 2022-01-01 2022-01-02 temp.sh"
echo ""
echo " Or create a day-specific environment, load and run"
echo ""
echo " > $0 --prefix=EX temp.env 2022-01-01"
echo " # Set environmental variables from file"
echo " > export \$(cat EX_2022-01-01.env | xargs)"
echo " # Run an script in updated environment"
echo " > bash temp.sh # inherit"
exit 1
;;
-p=*|--prefix=*)
PREFIX="$2"
shift # past argument=value
shift # past argument=value
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
# Restore positional arguments
set -- "${POSITIONAL_ARGS[@]}"
if [ $# -lt 2 ]; then
echo "Expected 2 or 3 positional arguments. Run $0 --help for more details."
exit 1
fi
# Assign positional arguments to meaningful names
TMPLENV=$1
STARTDAY=`date -d "${2}" +%Y-%m-%d`
if [ $# -lt 3 ]; then
ENDDAY=`date -d "${2}" +%Y-%m-%d`
else:
ENDDAY=`date -d "${3}" +%Y-%m-%d`
fi
if [ $# -gt 3 ]; then
SCRIPT=$4
HAS_SCRIPT=Y
else
HAS_SCRIPT=N
fi
# Loop over all days creating copies of the environment
DAY=${STARTDAY}
until [[ "${DAY}" > "2022-01-02" ]]; do
penv=${PREFIX}_${DAY}.env
if [ ! -e $penv ]; then
echo "Making ${penv}"
printf "" > $penv
cat $TMPLENV | while read line; do
date -d "$DAY" +$line >> $penv
done
else
echo "Keeping cached $penv"
fi
# If a script is provided, load the environment and run the script
if [ ${HAS_SCRIPT} == "Y" ]; then
export $(cat $penv | xargs)
$SCRIPT
fi
DAY=`date -d "${DAY} +1day" +%Y-%m-%d`
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment