Bash function execution from command line

bashshell-script

I love to play with bash script a lot. But I want to enhance the way I use bash script. I have a script(nepleaks.sh) as below used during my programming,

~   1 clean(){                                                                                                                                                                  
~   2  lein clean                                                                                         
+   3  lein deps                                                                                          
+   4 }    
    5                                                                                                     
    6 runApp(){                                                                                           
    7   echo("[info] : make sure you've started neo4j.")                                                 
    8   #lein run -m nepleaks-engine.core  
    ............
   34         
   35 clean                                                                                              
   36 #runApp  

When I need to clean my project, I have been commenting on runApp and vice-versa and then fire

$./nepleaks

Now, I want to amend this to something like

$ nepleaks clean or $ nepleaks runApp

I didn't like idea of sourcing nepleaks.sh and then call clean or runApp.

Here's one of the lovely thing I used https://github.com/cosmin/s3-bash/blob/master/s3-get, I am looking into but their script looked complex to me.
They support something as below,

s3-get -k {accesskey} -s /{path}/{secretid} /{bucketname}

Best Answer

Try this:

case "$1" in
  (clean) 
    clean
    exit 1
    ;;
  (runApp)
    runApp
    exit 0
    ;;
  (*)
    echo "Usage: $0 {clean|runApp}"
    exit 2
    ;;
esac

Now you can do:

$ ./nepleaks clean # only run clean
$ ./nepleaks runApp # only runApp
Related Question