Register
It is currently Fri Oct 31, 2014 6:51 am

function to retrieve a filename


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon May 16, 2005 8:40 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Originally posted by jbsnake

Code:
### checks to see if a character is a / ###
#                                         #
#                                         #
#                                         #
### call like #############################
# if notAForwardSlash $char               #
# then                                    #
#    echo "char passes"                #
# else                                    #
#    echo "fails"                      #
# fi                                      #
###########################################
function notAForwardSlash
{
   if [[ $1 != $fSlash ]]
        then
      return 0
        else
           return 1
        fi
}
### end notAForwardSlash ###

### grabs filename from a /path/to/file.name #
# note: can either take a string or a        #
#       variable string.  Requires above     #
#       notAForwardSlash function.           #
### call like ################################
# getFileName /home/user/somefile.sh         #
# echo $FileName                             #
### prints out ###############################
# somefile.sh                                #
##############################################
function getFileName
{
   STRING=$1
        LENGTH=${#STRING}
   for ((n=0;n <= $LENGTH; n++))
        do
      CHAR=${STRING:$n:1}
      if notAForwardSlash $CHAR
      then
         FileName=$FileName$CHAR
      else
         FileName=""
      fi
   done
}
### end getFileName ###


Top
 Profile WWW  
 PostPosted: Mon May 16, 2005 8:41 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
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]


Top
 Profile WWW  
 PostPosted: Mon May 16, 2005 8:42 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
ha ha ha .... i get it :) thanks


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP