Originally posted by jbsnake
these two functions used with the above two functions returns just the file extension (ofcourse if you're just looking for a particular file extension and don't care about files without extensions, you don't need the above two functions)
Code:
### checks to see if a character is a . ###
# #
# #
# #
### call like #############################
# if notADot $char #
# then #
# echo "char passes" #
# else #
# echo "fails" #
# fi #
###########################################
function notADot
{
if [[ $1 != $DOT ]]
then
return 0
else
return 1
fi
}
### end notADot ###
### grabs file extension from filename #######
# note: if no extension exists, it will #
# leave just the filename. Requires #
# above notADot function. #
### call like ################################
# getExtension /home/user/file.name #
# echo $Extension #
### prints out ###############################
# name #
##############################################
function getExtension
{
STRING=$1
LENGTH=${#STRING}
n=0
for ((n=0; n <= $LENGTH; n++))
do
CHAR=${STRING:$n:1}
if notADot $CHAR
then
Extension=$Extension$CHAR
else
Extension=""
fi
done
}
### end getExtension ###
if you wanted to use them all to retrieve a filename and extension (or compare an extension)
you would call them like
Code:
getFileName /home/user/somefilename.txt
echo $FileName # would display somefilename.txt
getExtension $FileName
echo $Extension # would display txt
ofcourse if you just wanted to display the extension without care for the files that don't have one
you could just do this:
Code:
getExtension /home/user/somefile.txt
echo $Extension # would display txt
be careful though...see?
Code:
getExtension /home/user/someextensionlessfile
echo $Extension # would display /home/user/someextensionlessfile
but this would be bad too...
Code:
getExtension /home/user.directories/somefile
echo $Extension # would display directories/somefile
hopefully you can see what these functions are capable of now
infact...some might say you could do some error checking
on a previously written program ::hint hint:: ::wink wink:: ::pokes crouse::[/b]