LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   General (https://www.linuxquestions.org/questions/general-10/)
-   -   Quick programming/scripting question; what's this and where is it from? (https://www.linuxquestions.org/questions/general-10/quick-programming-scripting-question%3B-whats-this-and-where-is-it-from-737229/)

David the H. 07-02-2009 11:44 AM

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.

GrapefruiTgirl 07-02-2009 11:48 AM

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

David the H. 07-02-2009 12:55 PM

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.

GrapefruiTgirl 07-02-2009 01:22 PM

Indeed, the regular Google is tricky, but have you tried Google Codesearch? It's pretty good with this stuff.

http://www.google.com/codesearch

GazL 07-02-2009 02:06 PM

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.

bigearsbilly 07-02-2009 02:13 PM

er,
well in what context?

where's it from?

David the H. 07-02-2009 02:39 PM

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.

GrapefruiTgirl 07-02-2009 02:48 PM

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 :)

rsciw 07-03-2009 07:15 AM

Looks similar to this, not?
http://en.wikipedia.org/wiki/Ternary_operation

David the H. 07-03-2009 03:07 PM

@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.

ntubski 07-04-2009 01:31 PM

Quote:

Originally Posted by David the H. (Post 3595856)
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.

The C(++) examples have this construct inside of string literals or comments, it's not a C expression.

I can't find it in the GNU make manual...

David the H. 07-05-2009 01:18 AM

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?

osor 07-05-2009 02:10 PM

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.

ntubski 07-05-2009 02:17 PM

Quote:

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?
One of the Makefiles (cc65) appears to be a substitution reference
Quote:

It has the form `$(var:a=b)' (or `${var:a=b}') and its meaning is to take the value of the variable var, replace every a at the end of a word with b in that value, and substitute the resulting string.
Makefile variables can have "?" in their name. This one is using ?F which is an automatic variable

osor 07-05-2009 02:26 PM

Quote:

Originally Posted by ntubski (Post 3597601)
One of the Makefiles (cc65) appears to be a substitution reference

Interesting. I seemed to have glanced over the cc65 makefile. The other one looks to be a template for building an rpm specs file.


All times are GMT -5. The time now is 09:21 PM.