python - A bash function that runs script -


i'm trying write bash function named myrun, such doing

myrun script.py 

with python file:

#myrun:nohup python -u script.py &  import time print 'hello world' time.sleep(2) print 'once again' 

will run script with command specified in first line of file, after #myrun:.

what should insert in .bashrc allow this? here have now:

myrun () { [[ "$1" = "" ]] && echo "usage: myrun python_script.py" && return 0 <something awk here or else?> } 

a minimalist version:

$ function myrun {   [[ "$1" = "" ]] && echo "usage: myrun python_script.py" && return   local cmd=$(head -n 1 < "$1" | sed s'/# *myrun://')   $cmd }  $ myrun script.py appending output nohup.out $ cat nohup.out hello world once again  $ 

(it's not clear me whether you're better off using eval "$cmd" or $cmd in last line of function, if want include "&" in mycmd directive, $cmd simpler.)

with basic checking:

function myrun {   [[ "$1" = "" ]] && echo "usage: myrun python_script.py" && return   local cmd=$(head -n 1 <"$1")   if [[ $cmd =~ ^#myrun: ]] ; cmd=${cmd#'#myrun:'}   else echo "myrun: #myrun: header not found" >&2 ; false; return ; fi   if [[ -z $cmd ]] ; echo "myrun: no command specified" >&2 ; false; return; fi   $cmd  # or eval "$cmd" if prefer } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -