Last active
August 29, 2015 13:59
-
-
Save halloleo/10700468 to your computer and use it in GitHub Desktop.
create unique new day-stamped file from a given file stem and extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# bash snippet: | |
# create unique new day-stamped file from a given file stem and extension | |
# | |
# note the for-loop fto find the first unique (=non-existent) file with | |
# a given name | |
# | |
fnstem="a-file" # here goes the file stem before unifiying | |
fnext="txt" # here goes the file extenmstion before unifiying | |
fnstem="${fnstem}_`date +%Y%m%d`" | |
foundpostfix=false | |
for postfix in "" "_1" "_2" "_3" "_4" "_5" "_6" "_7" "_8" "_9"; do | |
fn="${fnstem}$postfix.$fnext" | |
if [ ! -f "$fn" ]; then | |
foundpostfix=true | |
break | |
fi | |
done | |
if [ ! "$foundpostfix" = true ]; then | |
echo "error: could not generate a free filename (last try \"$fn\")" >&2 | |
exit 98 | |
fi | |
echo "$fn" # use the unique name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment