samedi 10 juin 2017

Does not work loop in a loop BASH

It duplicate of my other question. But it is other kind of question. Please don't ban, and please read it.

My script:

#!/bin/bash

cd ~/dir

line1="Type_Account"
IFS=","
array=( $line1 )

line2="pro"
IFS=","
array2=( $line2 )

line3=`ls ~/dir/ | egrep '\.html'`

for ((i=0;i<"${#array[@]}";++i)); do
for y in $dir; do
# get </tr> tag. if we have <tr>, this is a variable
w=`grep -A 1 "<td>${array[$i]}</td>" $y | grep -v "${array[$i]}"`
# get content of variable
w2=`grep -B 1 "<td>${array[$i]}</td>" $y | grep -v "${array[$i]}"`
# get number of line, of content of variable
number=`grep -n -B 1 "<td>${array[$i]}</td>" $y | grep -v "${array[$i]}" | cut -d "-" -f 1`

if [[ "$w" == *"</tr>"* ]]; then
        echo "ok"
        echo $w2
        sed -i -e "$number s/\(<td>\).*\(<\/td>\)/<td>${array2[$i]}<\/td>/g" $y
else
        echo "not ok"
fi
done
done

For one file it works fine. But if i am trying to these operation for several files in a loop, i have:

<tr>
    <td>click</td>
    <td>new</td>
    <td>Type_Account</td>
</tr>


+ for y in $line3
++ grep -A 1 '<td>Type_Account</td>' 'index.html
index2.html'
++ grep -v Type_Account
grep: index.html
index2.html: No such file or directory
+ w=
++ grep -B 1 '<td>Type_Account</td>' 'index.html
index2.html'
++ grep -v Type_Account
grep: index.html
index2.html: No such file or directory
+ w2=
++ grep -n -B 1 '<td>Type_Account</td>' 'index.html
index2.html'
++ grep -v Type_Account
++ cut -d - -f 1
grep: index.html
index2.html: No such file or directory
+ number=
+ [[ '' == *\<\/\t\r\>* ]]
+ echo 'not ok'
not ok
+ (( ++i ))
+ (( i<1 ))

But if i am trying to it in one file, i have in debug:

++ grep -A 1 '<td>Type_Account</td>' index.html
++ grep -v Type_Account
+ w='</tr>'
++ grep -B 1 '<td>Type_Account</td>' index.html
++ grep -v Type_Account
+ w2='    <td>pro</td>'
++ grep -n -B 1 '<td>Type_Account</td>' index.html
++ grep -v Type_Account
++ cut -d - -f 1
+ number=3
+ [[ </tr> == *\<\/\t\r\>* ]]
+ echo ok
ok
+ echo '    <td>pro</td>'
    <td>pro</td>
+ sed -i -e '3 s/\(<td>\).*\(<\/td>\)/<td>pro<\/td>/g' index.html
+ (( ++i ))
+ (( i<1 ))

Where i have error ? Please help.

Aucun commentaire:

Enregistrer un commentaire