4

I'll explain my question with example.

I run $ python manage.py to perform some action.

I want to run another command $ xyz , just when any user runs the previous command.

Thus, from now on, whenever user runs $ python manage.py

It should run like:

$ python manage.py && xyz

I thought of creating an alias. But alias variable can't have space between them?

3
  • Why are you typing python manage.py in the first place? It is really not appropriate to include python in the command. Instead it should be invoked as ./manage.py if it is in the current directory or manage.py if it is found through PATH.
    – kasperd
    Jan 29, 2016 at 16:41
  • ...plus, if manage.py starts with a hashbang line (#!/usr/bin/env python3) and is given execute permissions, then it could be named simply manage, and the user could invoke it using just ./manage or manage. Jan 29, 2016 at 18:26
  • Thats just an example. I am not really working with python manage.py. It could have been anything, like grep a etc.
    – yask
    Jan 31, 2016 at 12:35

1 Answer 1

14

You could put a shell wrapper around a call to python (put this in .zshrc or .bashrc... depending on your shell),

python () {
    if [ "$1" = manage.py ]; then
        command python manage.py && xyz
    else
        command python "$@"
    fi
}

But I suspect you are better off changing manage.py to run xyz at the end (see python subprocess library), or creating a shell script,

manage.sh :

#! /bin/sh
python manage.py && xyz
4
  • 3
    Within the function you need command python ... so that the function is not called recursively. And, of course, it must be python "$@". Jan 29, 2016 at 13:12
  • Or better yet, name your shell command manage and leave off the Windows-style extension. Your users should not know nor care when you change from a python script to a shell script, perl, or even a compiled binary. They should just invoke the name of the command and you take care of the rest. Jan 29, 2016 at 16:19
  • @MontyHarder Given the name it sounds like it is likely a django application, in which case manage.py is the name django will use for that file, and renaming it to something non-standard is probably not a good idea.
    – kasperd
    Jan 29, 2016 at 16:39
  • @kasperd Oh, joy. An application that demands stupidity. Jan 29, 2016 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .