using
Code:
$( seq <start> <stop> )
is one way to do it. By the way, i prefer the $( ... ) notation over the back quote, because sometimes back quotes are incorrectly interpreted/read/remembered/copied by readers.
Sometimes you'll want to allow the loop-code to adjust the counter, which then asks for something like:
Code:
for (( counter=0; ${counter} < 20; counter=$(( ${counter}+1 )) )); do
echo "this is loop ${counter}.";
done
in this code, mind the separating spaces around (( and )).
Or you do it completely by hand:
Code:
counter=0;
while [ ${counter} -lt 20 ]; do
echo "this is loop ${counter}.";
counter=$(( ${counter}+1 ));
done
OR, if you have some files, but want to get the number that's in the file, I'd do it like this:
Code:
for file in file[0-9]*; do
i=$(echo "${file}"|sed 's/^file//;');
echo "file index ${i}";
done
so decrease all files by one for instance:
Code:
for file in file[0-9]*; do
i=$(echo "${file}"|sed 's/^file//;');
i=$(( ${i} - 1 ));
if [ ! -f file${i} ]; then
mv ${file} file${i};
else
echo "file${i} exists already.";
fi
done
or then, when increasing files:
Code:
function increase_file {
local newfile;
fn=increase_file;
if [ 1 != ${#} ]; then echo "${fn} invalid nr of arguments ${#}.";
elif [ ! -f ${1} ]; then echo "${fn} called with non-file argument.";
else
i=$(echo "${1}"|sed 's/^file//;');
i=$(( ${i}+1 ));
newfile=file${i};
if [ -f ${newfile} ]; then
echo "first increase existing file ${newfile}.";
increase_file ${newfile};
fi
mv ${1} ${newfile};
fi
}
# calling with:
increase_file file1;