3

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}
7

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
  • Loads of Thanks for Cool script. What would be the case for 2 levels? I mean I sent $1 and $2 from terminal. I assume case inside case? – prayagupd Jun 19 '14 at 6:39
  • Yes, you can do nested case. – cuonglm Jun 19 '14 at 6:42
  • Ok, I will dry if I could do ./nepleaks runApp development or ./nepleaks runApp production etc. Anyways this is coolest script on earth. – prayagupd Jun 19 '14 at 6:43
  • @PrayagUpd: Do you try another case with $2 in runApp)? – cuonglm Jun 19 '14 at 6:56
  • 1
    @PrayagUpd: You missed an ;; after nested case, line 17 in your gist. – cuonglm Jun 19 '14 at 8:10
1
$ cat <./nepleaks.sh && chmod +x ./nepleaks 
> #!/usr/bin/sh
>
> clean() { ... ; }
> runApp() { ... ; }
> 
> ${clean+clean}
> ${runApp+runApp}

...

$ env - clean= ./nepleaks.sh
# ^runs only clean()^ # 

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.