[Ma-linux] Scripting redux Approaching
Lee Burton
lburton at mrow.org
Thu Mar 17 09:48:03 EDT 2011
Just for fun:
#!/bin/bash
echo $(cd "$(dirname "$1")"; pwd | tr -d '\n'; echo "/$(basename "$1")")
Works just as well :)
-Lee
On Thu, Mar 17, 2011 at 09:19, James Ewing Cottrell 3rd
<JECottrell3 at comcast.net> wrote:
> Actually, there is another issue; that of Program Design.
>
> My initial thoughts were that this is like Shooting a Sparrow with an
> Elephant Gun. Bruce evidently found a few Library Calls he liked in a
> Language and wrapped a program around it. Kudos for "getting things done
> quickly".
>
> In the case where you are using that as a command, great. But if it is to be
> used as a part of some other program, it must be
> called as "import fullname; fullname.main('/some/path')"
>
> I would probably mv fullname.py pathfns.py and then s/main/fullname/
>
> Or just take the 'unit test' test out and don't even pretend that it can be
> used as a module; only as a command.
>
> As a Fully Blown piece of code it might be better to define a class, pass a
> filename to the constructor and have __init__ save it. Then have __repr__
> return the normalized representation of it.
>
> But *now*'s who's got the Elephant Gun?
>
> Bang Bang, Shoot Shoot.
>
> JIM
>
> On 3/12/2011 2:37 AM, Bruce Israel wrote:
>
> Boy, seems like these lists have been pretty dead, except for
> various meetings and conferences announcements.
>
> [ Alan, you can stop reading now. ]
>
> So I figured I'd share a useful script that I wrote recently.
>
> I keep on running into the issue where I have commands or scripts
> that take filenames and they need to be able to manipulate the
> filenames properly (sometimes sending them to something else that
> may not be working off of the same directory, or things like that),
> so I wanted to be able to consistently and correctly convert them to
> their full names. Previously when I did that, I'd just prepend the
> current working directory if a relative directory, but when I needed
> to extract the parent directory name, that didn't work (after all,
> the simple parent directory name of /etc/./passwd ends up as ".").
>
> So I put together the following python script that does it all
> correctly and in a standardized fashion, and thought I'd share it
> here if anyone else might find it useful. Enjoy.
>
> Bruce
>
>
> #!/usr/bin/env python
> #
> # fullname.py - get the full name of a file
> # Written by: Bruce Israel <israel at tux.org>, Wed Feb 23 2011
> #
> # Usage: fullname.py <file> ...
> #
> # fullname handles both relative and absolute file addressing, e.g.
> # cd /etc; fullname ./passwd ==> /etc/passwd
> # fullname /tmp/../etc/./passwd ==> /etc/passwd
>
> import sys
> import os
>
> def usage(prog):
> print "Usage: %s <files>..." % (prog)
> sys.exit(1)
>
> def fullname(file, workingDir):
> if not os.path.isabs(file):
> file = "%s/%s" % (workingDir, file)
> return os.path.normpath(file)
>
> def main(args):
> args.pop(0)
> if len(args) == 0: usage(prog)
> for file in args:
> print fullname(file, os.getcwd())
>
> if __name__ == '__main__':
> main(sys.argv)
> _______________________________________________
> Ma-linux mailing list
> Ma-linux at calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/ma-linux
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 9.0.872 / Virus Database: 271.1.1/3502 - Release Date: 03/12/11
> 02:36:00
>
>
>
> _______________________________________________
> Ma-linux mailing list
> Ma-linux at calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/ma-linux
>
>
--
Lee Burton
lburton at mrow.org
301 910 0246
More information about the Ma-linux
mailing list