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

Re: shell program help wanted!



nw x wrote:
> 
> Hi, there:
> I don't think I am in the correct mailing list for this question, but
> I do need help, so if anyone can answer my question, please help me.

Shell scripting is fast becoming a lost art.  I'll bite.

> My question is I have lots of files with long name under one directory
> and I want to change the names of the file to temp0, temp1, temp2,...
> tempN, etc.  so I wrote the following shell code, but it didn't work
> (actually, I don't know how to make the number at the file name temp?)
> 
> thanks in advances!
> 
> the code is:
> set i=0
> for file in `ls`
> do
>      if [ -f $file ]; then
>        cp $file ./temp$i
>        if [ $? -ne 0 ]; then
>            echo "copy $file failed"
>        fi
>      fi
>      i = 'expr $i+1'
       ^^^^^^^^^^^^^^^
> done

Other than that one part, this script is fine.  Try this instead:

	set i=`expr $i + 1`

Three things:
1.  Use set.
2.  Grave marks, not apostrophes.
3.  expr is a command.  Try it on the command line sometime.
    Each thing you pass to it must be a distinct argument.
    In other words, use spaces.

Now, one last thing.
The `ls` at the beginning will include these "tempX" named files
and will start trying to overwrite some the second time you run
this.  Start copying the files somewhere other than "./" or you'll
be wondering what happened.

	- Kevin Colby
	  kevinc grainsystems com





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