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

    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    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

    or

    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 Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    The most obvious way of doing it is (not tested):

    Code:
    # We only want exactly one parameter
    if [ $# -ne 1 ]
    then
      echo "Usage: $0 [larger|largest]"
      exit 2
    fi
    
    # 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
      larger)
        du -a /example/dir | sort -n -r | head -n 20
      ;;
      largest)
        find /example/dir -size +1024
     ;;
      *)
        echo "Usage: $0 [larger|largest]"
        exit 3
      ;;
    esac
    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