[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: shell program help wanted!

Nianwei Xing <linux_debian@yahoo.com> writes:

> Hi, debians:
> I have a question about the shell program. The problem
> is I have lots of files with long file name under on
> directory and I want to changes the names of the files
> to temp0, temp1.gif,temp2.gif,.....tempN.gif,etc and I
> wrote the following shell program, but it doesn't
> work(actually, I don't know how to make the number at
> tempN.gif), can anybody help me to figure the problem?
> Thanks in advance!
> set i=0
> for file in `ls`
> do
>    if [ -f $file ]; then
>      cp $file ./temp$i.gif
>      if [ $? -ne 0 ]; then
>         echo "copy file failed"
>      fi
>    fi
>  i = 'expr $i+1'
The quotes are the wrong direction, and you need spaces between each arg
to expr in order for expr to evaluate the expression numerically rather than
as a string.

> done

The following bash script worked for me:


for file in `ls`
        cp $file temp$i.gif
        i=`expr $i + 1`


Reply to: