Prev: Evaluating variable
Next: Substituting characters
From: Stephane CHAZELAS on 11 Aug 2010 16:28 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? If you'd rather have illegible code: [[ $i = +* ]] let "I=$??$i:I$i" or let "I=${I#${i##[!+]*}"$I"}$i" -- Stephane
From: Stephane CHAZELAS on 11 Aug 2010 16:32 2010-08-9, 20:03(+00), Kenny McCormack: > 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? Oh, you meant "bash" as in "recent enough versions of bash and hopefully future versions as well"? [...] >>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. I suppose "let" existed in ksh long before $((...)) was added to POSIX. If you want something /new/ and non-standard, use ((...)). If you want something bash-specific, you can always use $[...] (though zsh eventually added it as well). -- Stephane
From: Kenny McCormack on 11 Aug 2010 17:58
In article <slrni66203.lo.stephane.chazelas(a)spam.is.invalid>, Stephane CHAZELAS <stephane_chazelas(a)yahoo.fr> suggested these possibilities: .... >[[ $i = +* ]] >let "I=$??$i:I$i" > >or > >let "I=${I#${i##[!+]*}"$I"}$i" Good! Thanks. I knew you could do it. -- "We should always be disposed to believe that which appears to us to be white is really black, if the hierarchy of the church so decides." - Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order - |