using a bash array inside a git filter command (i.e. inside quotation marks) -


i'm trying loop through array within git filter command means inside of "" , can't seem syntax right. i'm trying value of array @ current iteration in loop, variable keeps coming empty, giving me error when run sed command. please if can. note other variables in script work.

declare -a oldfsarray=(principiumi principiumii principiumiii principiumiv prollecture1 prollecture2 prollecture3 prollecture4 prollecture4b prollecture5 prollecture6 prollecture7 prollecture8 prollecture9 prollecture10 prollecture11 prollecture12 prollecture13 prollecture14 prollecture15 prollecture16 prollecture17 prollecture18 dthreelecture19 dthreeaonelecture20 dthreeaonelecture21 dthreeaonelecture22 dthreeaonelecture23 dthreeaonelecture24 dthreeatwolecture25 dthreeathreelecture26 dthreeafourlecture27 dthreeafivelecture28 dthreeafivelecture29 dthreeasixlecture30 dthreesixlecture30b dthreeasixlecture31 donelecture32 donelecture33 donelecture34 donelecture35 donelecture36 donelecture37 donelecture38 donelecture39 donelecture40 donelecture41 donelecture42 donelecture43 donelecture44 donelecture45 donelecture46 donelecture47 donelecture48 donelecture49 donelecture50 donelecture51 donelecture52 donelecture53 dtlecture54 dtlecture55 dtlecture56 dtlecture57 dtlecture58 dtlecture59 dtlecture60 dtlecture61 dtlecture62 dtlecture63 dtlecture64 dtlecture65 dtlecture66 dtlecture67 dtlecture68 dtlecture69 dtlecture70 dtlecture71 dtlecture72 dtlecture73 dtlecture74 dtlecture75 dtlecture76 dtlecture77 dclecture78 dclecture79 dclecture80 dclecture81 dclecture82 dclecture83 dclecture84 dclecture85 dclecture86 dclecture87 dclecture88 dclecture89 dclecture90 dclecture91 dclecture92 dllecture93 dllecture94 dllecture95 dllecture96 dllecture97 dllecture98 dllecture99 dllecture100 dllecture101 dllecture102 dllecture103 dllecture104 dllecture105 dllecture106 dllecture107 dllecture108 dllecture109 dllecture110 dllecture111 dllecture112 dllecture113 dllecture114 dllecture115 dllecture116 dllecture117 dllecture118 dllecture119 dllecture120 dllecture121 dllecture122 dllecture123 dllecture124 dllecture125 dllecture126 dllecture127 dilecture128 dilecture129 dilecture130 dilecture131 dilecture132) declare -a newfsarray=(principiumi principiumii principiumiii principiumiv lectio_1 lectio_2 lectio_3 lectio_4 lectio_5 lectio_6 lectio_7 lectio_8 lectio_9 lectio_10 lectio_11 lectio_12 lectio_13 lectio_14 lectio_15 lectio_16 lectio_17 lectio_18 lectio_19 lectio_20 lectio_21 lectio_22 lectio_23 lectio_24 lectio_25 lectio_26 lectio_27 lectio_28 lectio_29 lectio_30 lectio_31 lectio_32 lectio_33 lectio_34 lectio_35 lectio_36 lectio_37 lectio_38 lectio_39 lectio_40 lectio_41 lectio_42 lectio_43 lectio_44 lectio_45 lectio_46 lectio_47 lectio_48 lectio_49 lectio_50 lectio_51 lectio_52 lectio_53 lectio_54 lectio_55 lectio_56 lectio_57 lectio_58 lectio_59 lectio_60 lectio_61 lectio_62 lectio_63 lectio_64 lectio_65 lectio_66 lectio_67 lectio_68 lectio_69 lectio_70 lectio_71 lectio_72 lectio_73 lectio_74 lectio_75 lectio_76 lectio_77 lectio_78 lectio_79 lectio_80 lectio_81 lectio_82 lectio_83 lectio_84 lectio_85 lectio_86 lectio_87 lectio_88 lectio_89 lectio_90 lectio_91 lectio_92 lectio_93 lectio_94 lectio_95 lectio_96 lectio_97 lectio_98 lectio_99 lectio_100 lectio_101 lectio_102 lectio_103 lectio_104 lectio_105 lectio_106 lectio_107 lectio_108 lectio_109 lectio_110 lectio_111 lectio_112 lectio_113 lectio_114 lectio_115 lectio_116 lectio_117 lectio_118 lectio_119 lectio_120 lectio_121 lectio_122 lectio_123 lectio_124 lectio_125 lectio_126 lectio_127 lectio_128 lectio_129 lectio_130 lectio_131 lectio_132 lectio_133 lectio_134)  function gitnamechangefilter() { oldfs=$1 newfs=$2 oldln=$3 newln=$4 basedirectory=$5 mv $basedirectory/$oldfs $basedirectory/$newfs cd $basedirectory/$newfs     git filter-branch --tree-filter "         #test , change edited text         if [ -e $oldfs.xml ]                             mv $oldfs.xml $newfs.xml                  in {0..137}                                             old=$oldfsarray[$i] //here problem                         echo $old                         new=$newfsarray[$i] //here                         echo $new                         sed -i.bak 's/$old/$new/g' $newfs.xml //error comes says $old empty                     done                  sed -i.bak 's/lectio $oldln/lectio $newln/g' $newfs.xml               else                 echo 'nothing $oldfs not exist'         fi   } 

in bash, when use $var inside double-quoted string ("the value of var $var"), bash expands immediately. happen of $variable references in argument pass git, so, example, old=${oldfsarray[$i]} replaced $ith element of oldfsarray using value of $i when git command run.

however, doesn't matter old set in subshell because use of $old in next line (echo $old) replaced current value of $old (if any), part of script reads

old=$oldfsarray[$i] echo $old 

is expanded this:

old=principiumi[] echo 

(note: script uses old=$oldfsarray[$i], not syntax subscripting array. correct syntax in first paragraph above, ${oldfsarray[$i]})

i believe expecting git receive bash script variable references, , execute them. in order work, need surround script apostrophes ('), not double quotes.

however, need aware git execute script in subshell, , subshell have access exported shell variables. of bash 4.2, @ least, array variables cannot exported. (that is, can export them, has no effect.) you'll need create arrays within script pass git.

that might seem intimidating, fortunately declare -p prints out values in format can directly evaluated. can this:

oldfsarray=(value1 value2 value3...) export make_oldfsarray="$(declare -p oldfsarray)" #... git filter-branch --treefilter="    $make_oldfsarray    $make_newfsarray"'    # here on down, $foo not substituted    ' 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -