The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 01-06-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,374
Quote:
Originally Posted by sera1711 View Post
Hi,

currently I'm trying to write a csh script that should do the following:

set i = 1
while ($i < 100) {
cp AAA BBB
set i = $i +1
}

where AAA is a string like this file.11.txt, file.21.txt, ...
and BBB is a string like this file_0001, file_0002, ...

Is it possible to generate the string AAA and BBB automatically from the variable i?

It's simple in a Bourne-type shell:


Code:
i=1
while [ $i -lt 100 ]
do
  case $i in
     ?) nf=000$i ;;
    ??) nf=00$i ;;
   ???) nf=0$i ;;
  esac
  cp "file.$1.txt" "file_$nf"
done

You'll find it easier to write scripts in a standard shell and much easier to get help.