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

Re: More Bash Scripting

Normally, if I'm writing a script that has some kind of
counting or incrementing (like in a loop) I'll do it like
this... (I'd also use a file to keep the value of the current
counter) ... in the case below, if you want to move all 
files to a specific filename scheme that is somehow numbered
you could do this...


if [ ! -s "COUNTERFILE.txt" ] #make counter file if it doesn't exist
    echo "0" > COUNTERFILE.txt

if [ ! -s "filestore.dir" ]   #make file storing directory file
    touch filestore.dir

CTR=`/bin/cat COUNTERFILE.txt`
TEST=`/bin/ls -1 | grep -v "filestore" | wc -l`  
if [ $TEST -gt 0 ]
    for FILE in `/bin/ls -1 | grep -v "filestore"`  
      CTR=`expr $CTR + 1`
      echo "Value of CTR is $CTR"
      /bin/mv $FILE filestore$CTR
      echo "OLD [$FILE] NEW [filestore$CTR]" >> filestore.dir
    echo "$CTR" > COUNTERFILE.txt

The *expr* is the key to get the mathmetics to occur.
I've never tried multiplication or division though
I do know that addition and subtraction work just as well.

The above script is untested, but it's awfully close to
use as far as something that I'd set up in a cronjob that
look for files in directories in order to catalog them and
move them around with a unique naming convention.


----- Original Message ----- 
From: Rodney Chatman <rchatman corecomm net>
To: Red Hat <redhat-install-list redhat com>
Sent: Thursday, October 07, 1999 3:31 PM
Subject: More Bash Scripting

> I'm trying to write a script that allows me to rename a file using a
> predefined beginning followed by a sequencial number.  The problem I
> seem to be having is at the variable initialization portion of the
> script, everytime the loop goes back to the beginning the variable is
> re-initialized and I'm stuck in a loop that consistantly tries to
> overwrite the same file with the same file name because the variable
> isn't incrementing.  How does incrementation work in bashs scripting so
> that I can initialize a variable at one point and have the variable
> continue to increment by one?
> --
> Rodney Chatman
> CoreComm Internet Group
> If you wanna hide something, put it in a book.
> -- 
>   PLEASE read the Red Hat FAQ, Tips, Errata and the MAILING LIST ARCHIVES!
> http://www.redhat.com http://archive.redhat.com
>          To unsubscribe: mail redhat-install-list-request redhat com with 
>                        "unsubscribe" as the Subject.

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