samedi 7 mars 2015

Use of file names ending with a period


Why does Unix allow files with a period at the end of the name? Is there any use for this?


example.


I am asking because I have a simple function that echoes the extension of a file.



ext() {
echo ${1##*.}
}


But knowing it will print nothing if the filename ends in a ., I wondered whether it would be more reliable to write:



ext() {
extension=${1##*.}
if [ -z "$extension" ]; then
echo "$1"
else
echo "$extension"
fi
}


Clearly this depends on what you are trying to accomplish, but if a . at the end of the file name were not allowed, I wouldn't have wondered anything in the first place.



Aucun commentaire:

Enregistrer un commentaire