Prev: Evaluating variable
Next: Substituting characters
From: Kenny McCormack on 9 Aug 2010 11:20 i=+1 let I=1?I$i:$i ; echo $I works and displays 1 (and running the last line again displays 2, and so on) Now, note that: [[ $i == +* ]] && echo hi does the expected thing (displays hi). But what I want is (for what should be obvious reasons; if they are not, let me know) let I=[[ $i == +* ]]?I$i:$i ; echo $I But this generates the usual syntax errors. What is the right syntax? -- "The anti-regulation business ethos is based on the charmingly naive notion that people will not do unspeakable things for money." - Dana Carpender Quoted by Paul Ciszek (pciszek at panix dot com). But what I want to know is why is this diet/low-carb food author doing making pithy political/economic statements? But the above quote is dead-on, because, the thing is - business in one breath tells us they don't need to be regulated (that they can morally self-regulate), then in the next breath tells us that corporations are amoral entities which have no obligations to anyone except their officers and shareholders, then in the next breath they tell us they don't need to be regulated (that they can morally self-regulate) ...
From: Stephane CHAZELAS on 9 Aug 2010 15:19 2010-08-9, 15:20(+00), Kenny McCormack: > i=+1 > let I=1?I$i:$i ; echo $I > > works and displays 1 (and running the last line again displays 2, and so on) > > Now, note that: > > [[ $i == +* ]] && echo hi > > does the expected thing (displays hi). > > But what I want is (for what should be obvious reasons; if they are not, > let me know) > > let I=[[ $i == +* ]]?I$i:$i ; echo $I > > But this generates the usual syntax errors. What is the right syntax? [...] One thing to bear in mind is that "let" is just a command, so it's parsed like any other command. Moreover it's not standard. There is a standard way to do arithmetic expansion that doesn't suffer from the problems inherent with simple command parsing. In let I=1?I$i:$i If there's a file called I=1+I+1:+1 in the current directory, you'll get a different behavior because of filename generation. Similarly, the expansion of $i is subject to word splitting and filename generation there. case $i in (+*) I=$(($I$i));; (*) I=$i;; esac seems to answer the functional requirement, be standard, and be more legible. -- Stephane
From: Kenny McCormack on 9 Aug 2010 16:03 In article <slrni60l6p.ds5.stephane.chazelas(a)spam.is.invalid>, Stephane CHAZELAS <stephane_chazelas(a)yahoo.fr> wrote: .... >One thing to bear in mind is that "let" is just a command, so >it's parsed like any other command. Moreover it's not standard. Did I not put the word "bash" in the subject line? Do I not make myself clear that I am talking about a bash script and do not give a fig about "standard"s? >There is a standard way to do arithmetic expansion that doesn't >suffer from the problems inherent with simple command parsing. And that would be? >case $i in > (+*) I=$(($I$i));; > (*) I=$i;; >esac Yes, of course, anyone could do it with "case", but I wanted to use the new fancy syntax. That's the whole point. -- Just for a change of pace, this sig is *not* an obscure reference to comp.lang.c...
From: Chris F.A. Johnson on 9 Aug 2010 16:09 On 2010-08-09, Kenny McCormack wrote: > In article <slrni60l6p.ds5.stephane.chazelas(a)spam.is.invalid>, > Stephane CHAZELAS <stephane_chazelas(a)yahoo.fr> wrote: > ... >>One thing to bear in mind is that "let" is just a command, so >>it's parsed like any other command. Moreover it's not standard. > > Did I not put the word "bash" in the subject line? > Do I not make myself clear that I am talking about a bash script and do > not give a fig about "standard"s? Are you saying that the soution given doesn't work in bash? If not, what's your point? Why do you want a lesser solution? >>There is a standard way to do arithmetic expansion that doesn't >>suffer from the problems inherent with simple command parsing. > > And that would be? > >>case $i in >> (+*) I=$(($I$i));; >> (*) I=$i;; >>esac > > Yes, of course, anyone could do it with "case", but I wanted to use the > new fancy syntax. That's the whole point. Why? -- Chris F.A. Johnson, author <http://shell.cfajohnson.com/> =================================================================== Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
From: Kenny McCormack on 9 Aug 2010 17:07
In article <i3pnak$s1f$1(a)news.eternal-september.org>, Chris F.A. Johnson <cfajohnson(a)gmail.com> wrote: >On 2010-08-09, Kenny McCormack wrote: >> In article <slrni60l6p.ds5.stephane.chazelas(a)spam.is.invalid>, >> Stephane CHAZELAS <stephane_chazelas(a)yahoo.fr> wrote: >> ... >>>One thing to bear in mind is that "let" is just a command, so >>>it's parsed like any other command. Moreover it's not standard. >> >> Did I not put the word "bash" in the subject line? >> Do I not make myself clear that I am talking about a bash script and do >> not give a fig about "standard"s? > > Are you saying that the soution given doesn't work in bash? If not, > what's your point? Why do you want a lesser solution? Because. >>>There is a standard way to do arithmetic expansion that doesn't >>>suffer from the problems inherent with simple command parsing. >> >> And that would be? >> >>>case $i in >>> (+*) I=$(($I$i));; >>> (*) I=$i;; >>>esac >> >> Yes, of course, anyone could do it with "case", but I wanted to use the >> new fancy syntax. That's the whole point. > > Why? Because. -- Just for a change of pace, this sig is *not* an obscure reference to comp.lang.c... |