Skip to content

Instantly share code, notes, and snippets.

@andres-fr
Last active June 17, 2022 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andres-fr/2a69d43a97b3d71fba66b6f355b85b94 to your computer and use it in GitHub Desktop.
Save andres-fr/2a69d43a97b3d71fba66b6f355b85b94 to your computer and use it in GitHub Desktop.
Example of a bash function with named parameters+defaults
interactive_gpu()
{
# needed by getopt: https://stackoverflow.com/a/5048356
local OPTIND
# default parameter values
seconds=300
partition="large"
# parse flags to optionally override param defaults
while getopts "p:t:" f; do
case "${f}" in
p)
partition=${OPTARG}
;;
t)
seconds=${OPTARG}
;;
esac
done
shift $((OPTIND-1))
# parameter transformations, when needed
timestamp=$(date -d@${seconds} -u +%H:%M:%S) # convert seconds to hh:mm:$
# print and execute main call
cmd="srun --partition=${partition} --nodes=1 --ntasks-per-node=1 --cpus-per-task=8 --mem=45GB --gres=gpu:1 --time=${timestamp} --pty bash -i"
echo $cmd
eval "$cmd"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment