V
V
Valeriu1472017-06-11 12:22:40
linux
Valeriu147, 2017-06-11 12:22:40

Why doesn't the for loop work?

So, I'll start over with what my script does.
There is such a .html file.

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

in it:
Type_Account - variable name;
new - the content of the variable;
click - action (will not be used in my script)
Essentially I need to find a variable and put a value there, whatever I want.
In the script below, I am using arrays, array2, not variables. why? Yes, because I will be entering several variables from the .html file (Type_Account, it could be Payment_Account, etc.) and several values ​​that I will need to replace
#!/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 $line3; do
# получаем </tr> тег в переменную w. если он <tr>, это переменная
w=`grep -A 1 "<td>${array[$i]}</td>" $y | grep -v "${array[$i]}"`
# получаем содержмое переменной
w2=`grep -B 1 "<td>${array[$i]}</td>" $y | grep -v "${array[$i]}"`
# получаем номер линии содержимого переменной
number=`grep -n -B 1 "<td>${array[$i]}</td>" $y | grep -v "${array[$i]}" | cut -d "-" -f 1`

if ; 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

Debug this script:
+ 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=
+ 
+ echo 'not ok'
not ok
+ (( ++i ))
+ (( i<1 ))

If I don’t use a loop, but just do these operations for one file, then it works, and if I want to do this for several files, through a loop, then it doesn’t try to iterate through each one, but takes the value Please tell me. Where did I make a mistake? I fight the third day. Thanks for y in $line3; doindex.html index2.html

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2017-06-11
@Valeriu147

if I want to do this for several files, through a loop, then he does not try to iterate over each, but takes the value of index.html index2.html Please tell me. Where did I make a mistake?

I forgot to restore IFS to its original value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question