1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Rep Power

    Two scripts but only want to run one or the other

    Hi, I'm new to the forum and new to scripting. I am trying to write a .ksh that does two things but will only do one or the other depending on what i'm trying to get done. How would I go about that? would it just be an If then elif else statement that I would use? These are the two codes i'm trying to run

    du -a /example/dir | sort -n -r | head -n 20


    find /example/dir -size +1024

    Lets say the .ksh file is named twojobs.ksh so I want to do something like add another parmeter to choose which code to use like:

    twojobs.ksh largest
    twojobs.ksh larger

    How can I go about it to make it work?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Rep Power
    The most obvious way of doing it is (not tested):

    # We only want exactly one parameter
    if [ $# -ne 1 ]
      echo "Usage: $0 [larger|largest]"
      exit 2
    # Only one parameter given, now check it is what we want
    # Probably better to use tr to set it to lower (or upper) to
    #    be more user-friendly
    case $1 in
        du -a /example/dir | sort -n -r | head -n 20
        find /example/dir -size +1024
        echo "Usage: $0 [larger|largest]"
        exit 3
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo