LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices


Reply
  Search this Thread
Old 07-02-2009, 11:44 AM   #1
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Question 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.
 
Old 07-02-2009, 11:48 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 07-02-2009, 12:55 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Original Poster
Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 07-02-2009, 01:22 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

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

http://www.google.com/codesearch
 
Old 07-02-2009, 02:06 PM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
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.
 
Old 07-02-2009, 02:13 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
er,
well in what context?

where's it from?
 
Old 07-02-2009, 02:39 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Original Poster
Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 07-02-2009, 02:48 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 07-03-2009, 07:15 AM   #9
rsciw
Member
 
Registered: Jan 2009
Location: Essex (UK)
Distribution: Home: Debian/Ubuntu, Work: Ubuntu
Posts: 206

Rep: Reputation: 44
Looks similar to this, not?
http://en.wikipedia.org/wiki/Ternary_operation
 
Old 07-03-2009, 03:07 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Original Poster
Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
@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.
 
Old 07-04-2009, 01:31 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by David the H. View Post
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...
 
Old 07-05-2009, 01:18 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Original Poster
Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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?
 
Old 07-05-2009, 02:10 PM   #13
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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.
 
Old 07-05-2009, 02:17 PM   #14
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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
 
Old 07-05-2009, 02:26 PM   #15
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by ntubski View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick shell scripting question Komelore Programming 3 03-12-2007 08:39 AM
Quick Programming Question Komelore Programming 8 02-25-2007 11:00 PM
scripting or programming question - running a program ewto Linux - Newbie 2 09-05-2003 11:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 09:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration