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?

  • 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 '16 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. – Jonathan Hartley Jan 29 '16 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 '16 at 12:35
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
  • 3
    Within the function you need command python ... so that the function is not called recursively. And, of course, it must be python "$@". – Hauke Laging Jan 29 '16 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. – Monty Harder Jan 29 '16 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 '16 at 16:39
  • @kasperd Oh, joy. An application that demands stupidity. – Monty Harder Jan 29 '16 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.