Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active January 11, 2022 11:52
Show Gist options
  • Save kou1okada/24a76e5ff5eaac0ba43282fcc0d4c7f5 to your computer and use it in GitHub Desktop.
Save kou1okada/24a76e5ff5eaac0ba43282fcc0d4c7f5 to your computer and use it in GitHub Desktop.
run_once.bash - run command only once
#!/usr/bin/env bash
#
# run_once.bash - run command only once
# Copyright (c) 2019 Koichi OKADA. All right reserved.
# This script distributed under the MIT license.
#
function run_once () # [<command> [<args> ...]]
# Run [<command> [<args> ...]] only once.
# Return:
# Return exit status which was returned first time call for [<command> [<args> ...]].
{
declare -gA RUN_ONCE
declare -n retval=RUN_ONCE[$(IFS=$'\x1c'; printf "%s" "$*")]
[ -z "$retval" ] && { "$@"; retval=$?; }
return $retval
}
[ "$(readlink -f -- "$0")" = "$(readlink -f -- "$BASH_SOURCE")" ] && echo "Usage: source run_once.bash"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment