Skip to content

Instantly share code, notes, and snippets.

@kannangce
Last active August 25, 2022 07:36
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 kannangce/b8c1bb211c8993954417375b36ae41f8 to your computer and use it in GitHub Desktop.
Save kannangce/b8c1bb211c8993954417375b36ae41f8 to your computer and use it in GitHub Desktop.
Handy utility scripts
## Handy utility scripts.
retry_with_timeout () {
# Retries the given command until it returns the given value for the given number of times.
# Usage retry_with_timeout 'Command to be executed with quotes' <expected value> <number of times to retry>
local command_to_try="$1"
local expected_value="$2"
local expected_timeout="$3"
local total_time=0
until [[ $expected_value -eq `eval $command_to_try` ]]
do
if [[ 10#$total_time -gt 10#$expected_timeout ]]
then
echo "time-out $expected_timeout exceeded"
return -1
fi
total_time=`expr $total_time + 1`
sleep 1
done
echo "success"
return 0
}
get_crtime() {
# Gets the create time of a given file.
for target in "${@}"; do
inode=$(stat -c '%i' "${target}")
fs=$(df --output=source "${target}" | tail -1)
crtime=$(sudo debugfs -R 'stat <'"${inode}"'>' "${fs}" 2>/dev/null |
grep -oP 'crtime.*--\s*\K.*')
printf "%s\t%s\n" "${target}" "${crtime}"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment