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 ...
- 09-27-2008 #1Just 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.
- 09-27-2008 #2
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.
- 09-27-2008 #3More 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, 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.
For example,
becomesCode:cat a*
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:cat andreas angela arnold
does not call the echo program three times, once with each file name. If it did, you'd seeCode:echo a*
Instead, it calls the echo program once, with each file name appearing on the same command line, so you seeCode: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:andreas angela arnold
When I do that, I get this output:Code:which echo
Your output may be something different, likeCode:/usr/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:/bin/echo
and you'll get (for example)Code:/usr/bin/echo a*
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:andreas angela arnold.
So if you enter this command at the bash prompt:Code:$(your command goes here)
then you'll be using the actual echo program.Code:$(which echo) a*
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
the output will be similar toCode:$(which echo) --version
but if you enterCode: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.
you'll get simplyCode:echo --version
because the echo program implements the --version option, but the bash builtin echo command doesn't.Code:--version
--
Bill
Old age and treachery will overcome youth and skill.
- 09-27-2008 #4
You are right, wje. Very nice examples by the way.
I stand corrected.Debian GNU/Linux -- You know you want it.
- 09-27-2008 #5Just 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?
- 09-27-2008 #6Fair question.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?
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:
You'll get something like:Code:touch arnold touch andreas touch angela echo a* 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.Code:andreas angela arnold asdfwxyzwje*
--
Bill
Old age and treachery will overcome youth and skill.
- 09-28-2008 #7
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


Reply With Quote