Showing posts with label basename. Show all posts
Showing posts with label basename. Show all posts

Monday, July 25, 2011

Python Superscript

similar to my last post about bash scripting with "basename" functionality, here is an example python module to determine the name of the link being called as well as the arguments supplied:


#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
        PROJECT - MODULE:
                Python-SuperScript.py

        DESCRIPTION:
                example basename functionality handling in python

        @copyright: 2011 by nairb <code@nairb.us>
        @license: GNU GPL, see COPYING for details.
"""

import os, sys, string

class App(object):
        """ Class doc """
     
        def __init__ (self):
                """ Class initialiser """
                pass
             
        def progName(self):
                """  Return the program name of running script
                @param: none
                @return: base name of the running script
                """
                argvz=string.split(sys.argv[0], '/')
                return argvz[len(argvz)-1]
             
        def progArgs(self):
                """ Return the array of arguments given
                @param: none
                @return: the arguments passed to script
                """
                return sys.argv[1:]

        def Main(self):
                """ Main Function
                @param PARAM:
                @return RETURN:
                """
                print "This program was called as:\n\t" + str(self.progName()) + "\n\nArguments where passed of:\n\t" + str(self.progArgs())
     
if __name__ == '__main__':
    a=App()
    a.Main()

hope someone finds this useful

Sunday, July 24, 2011

Basename function processing without basename binary installed

OK, so Im going to start posting some howto pages to this blog.

starting with a bash scripting case statement to decide script function based on "basename" (or the name of the file running as the script) without the "basename" binary installed. This way you can create many links to the same script, and make the that script do many different things determined by the link name it is being called as. Then you just need to make sure these links to the script are in a directory somewhere within your $PATH.

example:
case $(echo ${0##*/} |cut -d '/' -f $(echo ${0##*/} |wc -w)) in
    scriptlinkname1)
        echo "doing the function scriptlinkname1"
        ;;
    *)
        echo "did not define that links function yet"
        ;;
esac


hope you find this helpful.