Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0
    It's a bash shell on linux. I think using parameters with exe.. Of course I want to know how to do this myself I just want you to help me complete this activity and learn from it. The deadline will be on friday.
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,395
    Rep Power
    1688
    We could get all clever and 'tweaky', using the getopts function, but as you have just two possible parameters that is a bit too much, so ... a skeleton script:

    Code:
    #!/bin/bash
    #
    if [ $# -eq 0 ]
    then
      HIDDEN=1
      UNHIDDEN=1
    elif [ $# -eq 1 ]
    then
      case $1 in
        -h)
           HIDDEN=1
           UNHIDDEN=0
        ;;
        -u)
           HIDDEN=0
           UNHIDDEN=1
        ;;
        *)
          echo "Invalid parameter: use either -h or -u"
          exit 2
        ;;
      esac
    else
      echo "Use one, or no parameters: -h or -u"
      exit 2
    fi
    
    if [ $HIDDEN -eq 1 ]
    then
      echo "Searching for hidden files"
    fi
    
    if [ $UNHIDDEN -eq 1 ]
    then
      echo "Searching for unhidden files"
    fi
    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
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,395
    Rep Power
    1688
    Sorry, meant to add some commentary, so you know what we are doing ...

    The first line: #!/bin/bash is there to coerce the system to run the script under the bash shell interpreter, regardless of what shell you are actually using. This can be a good habit to get into as it can help prevent strange happenings later down the line, such as adding the script into being run by cron (which tends not to use bash as the interpreter of shell scripts).

    We then test the 'system' variable $# (which contains the number of supplied parameters) to see what we have. We are concerned with, at this level, just three states, so we: see if we have been passed 0 parameters (and thus will be checking for both hidden and unhidden files); just one parameter, which we will want to be either -h or -u; or some other amount, which we do not want. Any errors we flag with a comment to the user and a graceful exit.

    Having got this far we know we have valid parameters, so can go ahead and start counting files. Or, in this skeleton script, just say what we would be doing, just to confirm that all runs as we'd expect.
    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
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0
    thanks this script can help me finish my activity..
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,395
    Rep Power
    1688
    You are happy with what it is doing and how?
    You are also happy to amend it to do what is needed? What method do you plan on using to find and count the files?
    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
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo