[ale] Looking for a one line script

David A. De Graaf dad at datix.2y.net
Mon Jun 13 20:30:38 EDT 2005


On Mon, Jun 13, 2005 at 07:31:55PM -0400, James Sumners wrote:
> I need to get the base path of a filename. For example, if I have
> "/path/to/some/random/file.ext" I need to trim off "file.ext" so that
> I am left with "/path/to/some/random/". However, the filename could
> contain spaces and other odd characters. Any suggestions?
> 
> -- 
> James Sumners
> http://james.roomfullofmirrors.com/

Whoever allowed blanks to be part of dirnames and filenames should be
strung up by the thumbs.  (Are you listening, Billy?)

These abominations can be dealt with in scripts by very careful use
of quotes.  Here's an example.  Put this data into /tmp/filelist:

/dir/bad dir/file.txt
/dir2/dir3/bad file.txt
/dir1/bad dir/bad file.txt

and this script into /tmp/split:   (be sure to make it executable)

while read line ; do
    DIR=`dirname "$line"`
    FILE=`basename "$line"`
    echo "$line ->  DIR: $DIR      FILE: $FILE"
done < /tmp/filelist


Then run /tmp/split to get this output:

$ /tmp/split
/dir/bad dir/file.txt ->  DIR: /dir/bad dir      FILE: file.txt
/dir2/dir3/bad file.txt ->  DIR: /dir2/dir3      FILE: bad file.txt
/dir1/bad dir/bad file.txt ->  DIR: /dir1/bad dir      FILE: bad file.txt


Note that dirname and basename have correctly parsed the contents of
"$line" because it's quoted.

Not one line.  Sorry.  But maybe you'll get some insight.

-- 
	David A. De Graaf    DATIX, Inc.    Hendersonville, NC
	dad at datix.2y.net     www.datix.us



More information about the Ale mailing list