shell script with "while" loop and numeric test does not work
can someone help me spot the problem here ?
#!/bin/sh
find . -name '*ABC*' > replace_temp.file
num_of_lines=`cat replace_temp.file | wc -l`
i=0
while $i<$num_of_lines
do
tc=`expr $i + 1`
line=`tail -$tc replace_temp.file |head -1`
line1=$line
sed -e 's/\(.*\)ABC/\1DEF/' $line
#mv -f $line1 $line
done
#rm -f replace_temp.file
i get the error replace.sh: line 6: 20: No such file or directory
purpose of this script: search and replace all files directories containing ABC
in their name to DEF
it goes through the lines of find results file from bottom to top so there wont be any problems with dir dependencies (eg changing name of 开发者_StackOverflowone dir will mess up changing the name of it's sub dir)
thanks
while $i<$num_of_lines
should be something like
while [ $i -lt $num_of_lines ]
or if you insist
while (($i < $num_of_lines))
It should be
while [ $i -lt $num_of_lines ]
do
done
or
while (( $i -lt $num_of_lines ))
do
done
or
while test $i -lt $num_of_lines
do
done
You are running an infinite loop, you have never updated $i
. Probably you would like to do something like i=$((i+1))
inside the body of the loop, or update $i
in any other way.
Use for tc in ((1..$num_of_lines)); do ... ; done
精彩评论