Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    how to solve this problem ( getopts )

    Hi there, i'm newbe to linux script programming.

    ./which [-a] cmd

    - The script should scan the ( with optional) command name provided and report PATH(s) that
    includes the command
    - If the "-a" option is provided, it should report all possible paths.
    - If the "-a" option is not provided, it should report that (only one) path in which cmd is
    also i want to display appropriate error messages as required

    can anyone give me a clue or show me some code to get an idea who to get use getopts so far


  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Seattle, WA, USA

    That link describes the glibc implementation of getopt. It has information on using it, as well as an example.

    I feel like I should mention that for this particular program, getopt may be overkill. This is pretty easy to parse.


    Ah. Are you doing Bash scripting? Oopsie. The above is for C.

    For Bash scripting, check out the getopts man page ("man getopts"). Also, a Google for "getopts" gets a ton of hits with explanations and examples.

  3. #3
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris

    The link below will provide a number of hits for getopts, examples, etc for the shell builtin, but you'll need to search for getopts once you get to that page.

    This is almost always a source of internal tension for me -- if the script is truly single-shot that I will toss after a use or two, I will not use getopts. On the other hand, I toss scripts so rarely that I often use getopts, just in case I'll add something else.

    All that being said, I agree with Cabhan, I'd probably just look for the argument being "-a" ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. $spacer_open
  5. #4
    thanx for your post, my problems are solved

    btw, i try to do another jobs.

    for example :

    i have a file that contains 10line of " Hello World " :helloworld
    i want to scan the file (count the number )and print the out the result.

    $ ./wordfreq helloworld
    Hello 10
    World 10

    so help me out plz.

    thanx again.

  6. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Seattle, WA, USA
    These sound a great deal like homework questions. Posting homework questions is not allowed under the Forum Rules, and for this reason, I am not going to give you any exact code.

    However, it seems to me that in order to solve this problem, you need to determine what qualifies as a word, then parse each line and find each word on that line. For each word, check if you've encountered it before: if you have, increment that counter, and if not, start a new counter for this word at 1.

  7. #6
    how do u get each word separate by whitespace , which command do we use to get each word to compare with the other

    can you give me some clues .


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts