I want to create a for loop that runs from 1 to a number stored in a variable. But it produces this error:
Syntax error: Bad for loop variable
This is my script:
#!/bin/bash
count_lines ()
{
local f=$1
l=`wc -l $f | sed 's/^\([0-9]*\).*$/\1/'`
}
echo "$0 counts the lines of code"
l=0
while [ "$*" != "" ]
do
count_lines $1
echo "$1: $l"
shift
done
for(( i= 0 ; i <= ${l} ; i++))
do
title=$(grep -oPm1 "(?<=<title>)[^<]+" <<< "$0")
echo "$title"
done
wc -l <filename
(note the left-angle) outputs just the count and you don't need to fiddle withsed
or even something cheaper like${var% *}
. That said, if you really want to look for XML construct(s?) in the file(s?) they are unlikely to be related to the number of lines; one of the main features of XML is that it is stream-oriented, not line-oriented.for (( i= 0 ; i <= ${l} ; i++))
space missing here. And in(( ))
usei <= l
--${}
is unnecessary and a missing l actually messes up with the syntax.