Quick programming/scripting question; what's this and where is it from?
GeneralThis forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
Last edited by GrapefruiTgirl; 07-02-2009 at 11:48 AM.
Reason: typo
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.
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.
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.
Last edited by David the H.; 07-02-2009 at 02:40 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.
@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.
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.
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?
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.