I would like to run obj2opengl.pl with all files in

/Users/KanZ/Desktop/Project/Test/Model/Object

which are name A001.obj A002.obj ... After run the script, I will get A001.h A002.h ... and I would like to move these files to

/Users/KanZ/Desktop/Project/Test/Model/Header

How can I adapt this code below *the problems are at A${i}.obj and A${i}.h

cd /Users/KanZ/Desktop/Project/Test/Model/Object
start=1
n=$(find . -name "*.obj" | wc -l)
end=$(($n));
for((i=$start;i<=$end;i++))
do
./obj2opengl.pl "A${i}.obj"
mv "A${i}.h" /Users/KanZ/Desktop/Project/Test/Model/Header
done

If I adapt my code by using if statement , How can I fix this code for the right syntax For example:

cd /Users/KanZ/Desktop/Project/Test/Model/Object
start=1
a=0
b=0
c=1
n=$(find . -name "*.obj" | wc -l)
end=$(($n));
for((i=$start;i<=$end;i++))
do
   ./obj2opengl.pl "A${a}${b}${c}.obj"
   mv "A${i}.h" /Users/KanZ/Desktop/Project/Test/Model/Header
   c++
if[$c > 9] then
   b++
   c=0
fi
if[$b>9] then
   a++
   b=0
   c=0
fi
done
share|improve this question
feedback

4 Answers

bash is not C, for, if and [ are all commands and require whitespace to separate them from other words.

Use

cd /Users/KanZ/Desktop/Project/Test/Model/Object
for file in *.obj
do
    h=${file%.obj}.h
    ./obj2opengl.pl "$file"
    mv "$h" ../Header
done

See the bash manual to explain the ${file%.obj} part

If you want to count, don't keep track of leading zeroes manually, use printf:

max_width=${#end}
for ((i=$start; i<=$end; i++)); do
    printf -v n "%0*d" $max_width $i
    obj="A$n.obj"
    h="A$n.h"
    ./obj2opengl.pl "$obj" 
    mv "$h" ../Header
done
share|improve this answer
feedback
if[$c > 9] then
   b++

I think here is the syntax error. [ is a command in shell, it takes one or more args, and the last arg must be [, so you must use space to seperate the command and args, and also need space between if and [.

Another error, shell do not support c style ++, you can use b=$[b+1].

Here is the correct code:

cd /Users/KanZ/Desktop/Project/Test/Model/Object
start=1
a=0
b=0
c=1
n=$(find . -name "*.obj" | wc -l)
end=$(($n));
for((i=$start;i<=$end;i++))
do
   ./obj2opengl.pl "A${a}${b}${c}.obj"
   mv "A${i}.h" /Users/KanZ/Desktop/Project/Test/Model/Header
   c=$[c+1]
if [ $c > 9 ] then
   b=$[b+1]
   c=0
fi
if [ $b > 9 ] then
   a=$[a+1]
   b=0
   c=0
fi
done
share|improve this answer
Also need space after for command – glenn jackman Jan 4 at 11:51
It's error. It told me that line 17: syntax error near unexpected token `fi' – kantawit Jan 6 at 4:54
feedback

If I adapt my code by using if statement , How can I fix this code for the right syntax For example:

cd /Users/KanZ/Desktop/Project/Test/Model/Object
start=1
a=0
b=0
c=1
n=$(find . -name "*.obj" | wc -l)
end=$(($n));
for((i=$start;i<=$end;i++))
do
   ./obj2opengl.pl "A${a}${b}${c}.obj"
   mv "A${i}.h" /Users/KanZ/Desktop/Project/Test/Model/Header
   c++
if[$c > 9] then
   b++
   c=0
fi
if[$b>9] then
   a++
   b=0
   c=0
fi
done
share|improve this answer
feedback
cd /Users/KanZ/Desktop/Project/Test/Model/Object

find . -name "A*.obj" -exec obj2opengl.pl {} \;
find . -name "A*.h" -exec mv {} /Users/KanZ/Desktop/Project/Test/Model/Header \;
share|improve this answer
it's error. find: obj2opengl.pl: No such file or directory. But I already put obj2opengl.pl in /Users/KanZ/Desktop/Project/Test/Model/Object – kantawit Jan 4 at 6:46
@kantawit, change it to -exec ./obj2opengl.pl – perreal Jan 4 at 6:53
There are not .h file .It seem I should type the file name eg. dog.obj after find . -name "A*.obj" -exec ./obj2opengl.pl because It told that Usage: obj2opengl [options] file use -help or -man for further information – kantawit Jan 4 at 7:04
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.