Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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 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 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 at 12:35
up vote 14 down vote accepted

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
share|improve this answer
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 at 13:12
    
Thanks. Change made. – Mark Perryman Jan 29 at 13:14
    
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 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 at 16:39
    
@kasperd Oh, joy. An application that demands stupidity. – Monty Harder Jan 29 at 17:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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