Question about testing with variable operators
I am using arithmetic tests where the test operator is passed in as an arg and it works just fine. foo() { (( $1 $2 $3 )) } foo 5 '>' 7 But I figured that if this works for arithmetic, it should also work with the test operator. It seems to not work: 514 > bar() > { > [[ $1 $2 ]] bash: conditional binary operator expected bash: syntax error near `$2' As a work around, I can use eval or the builtin test, but my question is this: Is this a bug or is there a reason that it should work for arithmetic but not for the test [[ operator? GNU bash, version 4.0.35(1)-release (x86_64-redhat-linux-gnu) TIA -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net
Re: pushd, popd and dirs of different bash processes communicate by an external file
On 6/9/2011 11:42 PM, Peng Yu wrote: Hi, I'm wondering if there is a way that different bash processes share the same stack. One way of doing so is to use a file to save the stack. It is not difficult to implement this function myself, but I'm just wondering if there is any simpler solution or anybody has done it already. Thanks! .dirstack_fns: # # directory stack aliases and functions # # pd is just pushd and pd[2-9] do pushd's with the numeric value # ds is dirs, the short dir stack listing # dl is the long dirstack listing with directory numbers # sd saves the dirstack in ~/.dirstack for use in another shell or a login # gd gets the saved directory stack # dset assigns the current directory to its argument (variable name) # # future versions of pushd will label the window with the current # directory make_aliases() { typeset -i ii=1 alias pd=pushd for ((ii=1; ii < 20; ii++)) do alias pd${ii}="pushd +${ii}" done alias ds=dirs alias dl='dirs -v' alias sd="dirs -l > ~/.bash_dirs" } make_aliases unset -f make_aliases function dset() { eval $1=$PWD } function gd() { local readlist declare -a dir_list declare -i dir_cnt declare -i ii local dir if [ ! -e ~/.bash_dirs ] then echo "~/.bash_dirs does not exist" return fi read readlist < ~/.bash_dirs set ${readlist} let dir_cnt=$#-1 let ii=0 while [ ${ii} -le ${dir_cnt} ] do dir_list[${ii}]=$1 shift let ii+=1 done dir=${dir_list[${dir_cnt}]} if [ -d "${dir}" ] then dirs -c # Clear the stack before reassigning. builtin cd ${dir} fi let dir_cnt-=1 while [ ${dir_cnt} -ge 0 ] do dir=${dir_list[${dir_cnt}]} if [ -d "${dir}" ] then builtin pushd ${dir} >/dev/null fi let dir_cnt-=1 done } -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net
Re: Question about testing with variable operators
"Steven W. Orr" writes: > As a work around, I can use eval or the builtin test, but my question is > this: Is this a bug or is there a reason that it should work for > arithmetic but not for the test [[ operator? [[ is a reserved word like if, which triggers special parsing rules, so you cannot use it like this. Andreas. -- Andreas Schwab, sch...@linux-m68k.org GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Re: Question about testing with variable operators
Andreas Schwab wrote: "Steven W. Orr" writes: As a work around, I can use eval or the builtin test, but my question is this: Is this a bug or is there a reason that it should work for arithmetic but not for the test [[ operator? [[ is a reserved word like if, which triggers special parsing rules, so you cannot use it like this. However, it should work for the good ol' test command ([ ... ]) -- Be conservative in what you do, be liberal in what you accept from others. - jbp, master of the net, in RFC793