Quick programming/scripting question; what's this and where is it from?
(I'm sticking this in general because I don't think it's important enough for any of the main forums.)
I have a probably-simple question I've been meaning to ask for a while. I haven't been able to find an answer and it's really starting to bug me. Unfortunately Google and the other search engines are very frustrating on this point. They absolutely refuse to accept most non-alphanumeric characters within search terms. What exactly is this expression and what language(s)/environments use it? ${?variable:value} It looks very similar to some of the bash parameter substitutions, but I know it's not found in bash. I see it used for example in the display setting field in audacious. I use it for example to do this: ${?date: (${date})} ...which I take to mean "if date exists, print date(in parentheses and with a space in front of it), else nothing". In this respect it seems equivalent to bash's ${variable:+alt_value} expression. So is it the same, or is it different in some way? And where does it come from? Also, does anyone have any suggestions on how to search for, or what search engines to use to search for, things like this? Thanks. |
I believe you're on the right track.
I am not familiar with the first example you give, with the question mark in it, but I have seen the other examples used in bash, and what I learned is that it is a way of setting a default value for a variable if none is provided by user input or whatever means would otherwise provide a value. Like: var=${hello:-3} would set $var=3 if $hello is empty NOTE: My exact syntax is likely slightly off, but you get the idea. Sasha |
Yes indeed. It's very similar. As I mentioned, I'm familiar with the bash parameter substitution structures this resembles, but this one definitely isn't in bash. I believe I've pretty much figured out what it means. Now I just want to know which shell or language it comes from, and confirm my guess.
Google is absolutely useless in this situation. It won't let you search for strings like ${? at all. This isn't the first time I've had trouble searching for a computer-related string. |
Indeed, the regular Google is tricky, but have you tried Google Codesearch? It's pretty good with this stuff.
http://www.google.com/codesearch |
bash does appear to have a ${parameter:?word} which looks very similar.
Perhaps what you've found could be an older syntax for the same expansion? (Sort of similar to how they deprecated $[ ] in favour of $(( )) ) but they still both do the same thing. As you say, it's not an easy thing to search on. |
er,
well in what context? where's it from? |
bigearsbilly, that's exactly what I'm asking. I've seen the construct around in a few places, such as in the audacious example, and I noticed how similar it is to bash's parameter substitutions. Now I just want to know what language(s) use it and what the exact meaning is.
@GazL. While ${parameter:?word} does look similar, it appears that the function is almost exactly the reverse. ${parameter:?word} means, if the parameter isn't found, print "word" to standard error. But ${?parameter:word} seems to say that if the parameter is found, print "word". In this way it's really the same function as ${parameter:+word} in bash. GrapefruiTgirl, thanks for the google codesearch. It's not exactly what I wanted, but it may help me to find the things I'm looking for. If only they'd implement regex searching for their regular search engine! So far using it, I've turned up ${?var} in csh, which appears to test for the existence of the variable, outputting 1 or 0 if found or not, but the csh on my system doesn't recognize the expanded substitution part with the colon. Very close, but not an exact match. It's also turning up some C files with similar structures, but I haven't found any exact matches yet. |
Right, I didn't see any matches for the exact syntax you gave in your OP, but that was using 'var' and 'value', and it returned a page of code in like 8 different languages from ASP to Ruby and beyond...
If you actually had a piece of code that had some real variables in there (like something where your example is used in a real context), you might be able to find the actual piece of source code which contains it using Google-Codesearch, hence finding what language it is. Good luck searching, I'm curious too :) |
Looks similar to this, not?
http://en.wikipedia.org/wiki/Ternary_operation |
@rsciw; It certainly seems to be a kind of ternary operator, although what I've seen seems to show it to be specifically a variable testing+substitution expression. In any case, the wikipedia article doesn't show this specific pattern.
to tell the truth I'm surprised my query hasn't generated a clear answer yet. I felt sure that some experienced programmer would quickly come in with a "yeah, we use that in $LANGUAGE, and it means $FOO", or something like that. The narrowest regex I could come up with in googlecodesearch pulls up only a handful of examples, including the default audacious config file, a makefile or two (language unknown), and a couple of C and C++ hits that may or may not be the same thing. So it's starting to look like it's mostly found in C-based languages, but I still haven't narrowed it down exactly. Maybe I'm just getting too obsessed by this question, but it's so frustrating not being able to find a definitive answer. |
Quote:
I can't find it in the GNU make manual... |
Thanks for pointing that out ntubski.
That's an interesting observation. I've become pretty good at shell scripting recently, but I still don't know much about real coding, so I can't really read languages like C. So the strings in the C program code are all external to the program functions themselves. I guess that means I'm back to square one. :( What about the hits in the makefile pages? They seem identical to the usages I've seen. What do they do? And where would the variables they reference be defined? |
Well, audacious has its own “programming language” called Tuplez which is used by its config parser, so this is why the construct is used there.
The makefiles that you are showing probably are being used in rpm spec files, which have their own (very varied and sometimes cryptic) syntax. |
Quote:
Quote:
|
Quote:
|
All times are GMT -5. The time now is 09:21 PM. |