I have a script that iterates through files and does some string substitution to insert the date.
#!/bin/bash
f="/tmp/file.txt" # with .txt extension
timestamp="$(date +%H%M%S)"
echo "${f%%.*}-$timestamp.${f#*.}"
It provides the following output, which is correct.
/tmp/file-220304.txt
But if a file doesn't have an extension, the script breaks.
#!/bin/bash
f="/tmp/file" # no extension
timestamp="$(date +%H%M%S)"
echo "${f%%.*}-$timestamp.${f#*.}"
$ ./test.sh
/tmp/file-220359./tmp/file
Isn't it possible to use something like ${f:-not found} to fill in the blank if f is not defined? I can't figure out how to solve the above issue with the below method.
#!/bin/bash
f="/tmp/file" # no extension
timestamp="$(date +%H%M%S)"
echo "${f%%.*}-$timestamp.${f#*.:-not found}"
Results:
$ ./test.sh
/tmp/file-221420./tmp/file