The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Detecting incoming files without busy polling baldyeti Shell Programming and Scripting 9 08-15-2008 11:06 AM
script to find the average number or files? bbbngowc Shell Programming and Scripting 2 03-27-2008 12:57 PM
run a script from incoming email jojo77 UNIX for Dummies Questions & Answers 7 02-22-2008 01:48 PM
find number of incoming requests to a server laddu UNIX for Dummies Questions & Answers 1 03-10-2007 01:48 AM
awk script to find the number of files uni_ajay_r Shell Programming and Scripting 4 10-31-2006 06:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-18-2009
hootdocta5 hootdocta5 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 5
Script to number incoming files

Hey guys,

I am working on a Cshell script and I am stuck on this one part. I need to be able to copy in files to my directory but give them different names so they don't overwrite each other. For example, my folder already contains FILE.1 I want my script to name the next file copied over FILE.2 and the next one FILE.3 and so on. It should be able to tell what files are there and named the new one the next highest value. So I have FILE.1, FILE.2, and FILE.3 in my directory now, If I copy in another file, my script should rename it FILE.4 This script has been driving me nuts all day I can't figure it out. Here is what I have so far. Thanks in advance. New code would be appreciated because I obviously have no clue what I'm doing in this piece.

Code:
while (-d FILE.0)
cp -r FILE FILE.1
 
set ctr = 1
foreach f(*)
   set var1 = 'file $f | awk '{printf "%s\n",$3}''
   if ($var1 == 1) then
      FILE = 'ls $f | cut -f1 -d "."'
      1 = 'ls $f | awk -F "." '{printf "%s\n",$2}''
      mv $f $FILE$ctr.$1
      ctr = 'expr $ctr + 1'
  endif
end
end
  #2 (permalink)  
Old 03-19-2009
wabard wabard is offline
Registered User
  
 

Join Date: Mar 2009
Location: Adelaide, SA & Perth, WA - Australia
Posts: 25
Exclamation

Code:
#!/bin/sh

i=1

while(a=1)
do
     if test -e  "FILE."$i
     then
       let i+=1
     else
       break
     fi
done

cp -f FILE FILE.$i

Last edited by wabard; 03-19-2009 at 08:53 PM.. Reason: FIXED The cp command line argument to -f from -r (I typed the wrong option, r and f are close on the keyboard .. lol )
  #3 (permalink)  
Old 03-19-2009
Goldorakk's Avatar
Goldorakk Goldorakk is offline
Registered User
  
 

Join Date: Feb 2009
Location: France
Posts: 43
In Korn Shell:
Code:
#!/bin/ksh

if [ ! $# -eq 2 ]
then
        echo "Usage: $(basename $0) <filename> <target directory>"
        exit 1
fi

fname=$(basename $1)
current=$(pwd)

cd $2

lastval=$(ls $fname* 2>/dev/null |sort | tail -1 | cut -d. -f2)

if [ "$lastval" = "" ]
then
        lastval=1
else
        (( lastval = lastval + 1 ))
fi

cd $current
cp -f $1 $2/$1.$lastval
If your script is for example "mycopyfile", use it like:
Code:
./mycopyfile FILE /path/to/files
  #4 (permalink)  
Old 03-19-2009
hootdocta5 hootdocta5 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 5
Quote:
Originally Posted by wabard View Post
Code:
#!/bin/sh
 
i=1
 
while(a=1)
do
     if test -e  "FILE."$i
     then
       let i+=1
     else
       break
     fi
done
 
cp -r FILE FILE.$i
what exactly does the a represent in this code?
  #5 (permalink)  
Old 03-19-2009
wabard wabard is offline
Registered User
  
 

Join Date: Mar 2009
Location: Adelaide, SA & Perth, WA - Australia
Posts: 25
Quote:
Originally Posted by hootdocta5 View Post
what exactly does the a represent in this code?

Dummy variable to force the while to loop forever... you exit out of the infinite loop with the "break" statement.

Note: I made a mistake in the "cp" command line option in the example above (corrected and noted).
  #6 (permalink)  
Old 03-19-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,052
below may help you some

Code:
name=`for i in dir1/*
do
	echo $i | sed 's/^\(.*\/\)\([^0-9]*\)\(.*\)/\2 \3/'
done | sort -n +1 | tail -1 | awk '{print $1""($2+1)}'`
cp file_to_be_copied dir1/$name
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 11:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0