Find the answer to your Linux question:
Results 1 to 7 of 7
Most of the software I used in Linux terminal supports wildcard characters (example: "cat a*" outputs the contents of every file that starts with an a). I doubt every developer ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    12

    C++ wildcard support

    Most of the software I used in Linux terminal supports wildcard characters (example: "cat a*" outputs the contents of every file that starts with an a). I doubt every developer is rewriting that functionality from scratch. I'm trying to write a program for Linux as well, and need to add wildcard support into it. Is there a library I can include for this? Additionally, I already implemented a lot of support for getting parameters from the user (like -f option for example) as well as ability to specify multiple options (example: -la vs -l -a vs -al), but I can't help to wonder if I just reinvented the wheel there as well? Surely a feature this common (and this annoying to implement after addressing all possible bugs) in Linux software would make it to a common library. Can someone suggest if there are libraries available for this? Thanks.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Hello,

    this wildcard functionality, the broader term being "filename extension", is a functionality of the command line shell.

    For example, if you type "cat a*", it is the shell that looks which files do start with an 'a' and it calls the cat program once for any such file.
    (cat arnold; cat andreas; cat angela)

    Getopt - The GNU C Library
    is already part of the GNU C runtime library.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    For example, if you type "cat a*", it is the shell that looks which files do start with an 'a' and it calls the cat program once for any such file.
    More precisely, it calls the cat program only once, but places each file name on the command line. Furthermore, it sorts the names in ASCII order.

    For example,
    Code:
    cat a*
    becomes
    Code:
    cat andreas angela arnold
    I'm about to use another example that's almost correct. You can use the echo command to see what's going on here:
    Code:
    echo a*
    does not call the echo program three times, once with each file name. If it did, you'd see
    Code:
    andreas
    angela
    arnold
    Instead, it calls the echo program once, with each file name appearing on the same command line, so you see
    Code:
    andreas angela arnold
    Why is that example only almost correct? Because bash (the shell program you're most likely using) doesn't really call the echo program at all. It uses an interal implementation of "echo", built right into bash. If you want to use the regular echo program, you can do this at the command prompt:
    Code:
    which echo
    When I do that, I get this output:
    Code:
    /usr/bin/echo
    Your output may be something different, like
    Code:
    /bin/echo
    depending on your Linux distribution. At any rate, just remember what that response is and use it in a command line like this:
    Code:
    /usr/bin/echo a*
    and you'll get (for example)
    Code:
    andreas angela arnold.
    Two more points of curiosity. First, bash has this excellent feature where you can use the output of a shell command directly as part of another shell command. You do that by placing the first shell command into this syntax:
    Code:
    $(your command goes here)
    So if you enter this command at the bash prompt:
    Code:
    $(which echo) a*
    then you'll be using the actual echo program.

    The second point of curiosity is that you can tell whether you're using the builtin bash echo command or the echo program by throwing the --version option at it. If you enter
    Code:
    $(which echo) --version
    the output will be similar to
    Code:
    echo (GNU coreutils) 6.9
    Copyright (C) 2007 Free Software Foundation, Inc.
    This is free software.  You may redistribute copies of it under the terms of
    the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by FIXME unknown.
    but if you enter
    Code:
    echo --version
    you'll get simply
    Code:
    --version
    because the echo program implements the --version option, but the bash builtin echo command doesn't.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    You are right, wje. Very nice examples by the way.
    I stand corrected.
    Debian GNU/Linux -- You know you want it.

  5. #5
    Just Joined!
    Join Date
    Apr 2007
    Posts
    12
    Thank you guys, this cleared up a lot. But now I'm confused about something else. If bash automatically handles characters like * before even letting the program run, how does bash not interfere with sed functionality where you can use * in the string you're trying to locate or as part of the regex?

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If bash automatically handles characters like * before even letting the program run, how does bash not interfere with sed functionality where you can use * in the string you're trying to locate or as part of the regex?
    Fair question.

    Bash will leave the asterisk alone if it's included in a quoted string (single or double quotes), which isn't your situation, or if it results in no file being found (which is your case).

    Try this at the command line:
    Code:
    touch arnold
    touch andreas
    touch angela
    
    echo a* asdfwxyzwje*
    You'll get something like:
    Code:
    andreas angela arnold asdfwxyzwje*
    That's why I like to put command line regular expressions in quotes. You won't have to worry about asterisk, or question mark, or dollar sign, or percent sign.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    very nice, I'm a shell scripter myself, and this clarified alot that i had questions of.
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

Posting Permissions

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