设置计数器值



Hello Programming Profi,我想有计数器的总值,并在最后的第二个脚本的回声,但计数器在做功能运行。有没有办法让它在do函数之前写值。或者如果我在counter下写前2行,我得到了值,但我需要在XML文件上写这行!是否有一种方法可以做到这一点,也许计数器值从脚本的中间到总和最后在echo第二行。

#XML headers
echo '<?xml version="1.0" encoding="utf-8"?>' > $tmp_gigabook
echo "<list response="get_list" type="pr" reqid="1000" total="$counter" first="1" last="$counter">" >> $tmp_gigabook
declare counter=1000
# Read intermediate csv-xml-ish into array of lines
readarray -t lines < $tmp_intermediate
# Walk through lines and output entries
for line in "${lines[@]}"
do
# Split line into columns
# Fix v 0.3 Parentheses bug: explicitly set IFS, don't rely on defaults
IFS=$'t'
read -a columns <<< "$line"
# Output xml
((counter++))
echo "<entry id="$counter">
<ln>${columns[0]}</ln> 
<fn>${columns[1]}</fn>
<mb>${columns[2]}</mb>
<hm>${columns[3]}</hm>
</entry>" >> $tmp_gigabook
done
# XML footer
echo '</list>' >> $tmp_gigabook
# Remove placeholders / markers
sed -i 's/±//g' $tmp_gigabook
## Finalise
# Copy and adjust
cp -f $tmp_gigabook $instance/$phonebook
chown phonesystem:phonesystem $instance/$phonebook
chmod 600 $instance/$phonebook

您可以推迟打印您的标题,直到您完成处理文件,并使用cat将其与文件内容一起打印回文件:

# remove echo statements for the header, create the blank file first
echo -n >"$tmp_gigabook" # or :>"$tmp_gigabook"
# snip - declare / readarray here
for line in "${lines[@]}"
do
# snip - write to file as usual
done
# XML footer
echo '</list>' >> "$tmp_gigabook"
# rewrite the file but prepend the header information w/counter values
echo "<?xml version="1.0" encoding="utf-8"?>
<list response="get_list" type="pr" reqid="1000" total="$counter" first="1" last="$((counter-1000))">
$(cat "$tmp_gigabook")" > "$tmp_gigabook"

最新更新