The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Come and work for me! (UK) TonyChapman Linux 2 03-25-2008 02:08 AM
How does it work? nat123 Linux 1 11-04-2007 10:13 PM
Script doesn't work, but commands inside work cheongww UNIX for Dummies Questions & Answers 2 11-14-2006 07:52 PM
sed doesn't work billy5 Shell Programming and Scripting 3 08-26-2005 12:44 AM
How does tee work Foxgard UNIX for Dummies Questions & Answers 1 06-18-2005 12:58 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-15-2003
Registered User
 

Join Date: May 2003
Location: Lake District, U.K.
Posts: 42
Will this work?

I'm trying to check if files already exist in a directory. They have the same basename (exsyctr1), but 4 different extensions. If the files exist, then I make backups of them, then copy them from another directory ($livecomp/data) to the current one ($copycomp/data). If they don't exist, just perform the copy.

Will the script below work?

Code:
  if [ -f $copycomp/data/exsyctr1.* ]
  then
	echo "Changing to $copycomp/data/"
	cd $copycomp/data
	echo "Backing up existing files"
	cp exsyctl1.* *.bak
	echo "Copying from $livecomp/data/ to $copycomp/data/"
	cp $livecomp/data/exsyctr1.* .
  else
	echo "Copying from $livecomp/data/ to $copycomp/data/"
	cp $livecomp/data/exsyctr1.* .
  fi
Reply With Quote
Forum Sponsor
  #2  
Old 07-15-2003
oombera's Avatar
Registered User
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
I would use a loop like the following, but it's just one possible way I suppose...
Code:
echo "Changing to $copycomp/data/"
cd $copycomp/data

for FILE in `ls $copycomp/data/exsyctr1.*`
do
  if [ -f $FILE ]; then
    echo "Backing up existing files"
    cp $FILE ${FILE%.*}.bak
  fi
done

echo "Copying from $livecomp/data/ to $copycomp/data/"
cp $livecomp/data/exsyctr1.* .
If you're not using KSH, then replace "cp $FILE ${FILE%.*}.bak" with "cp $FILE `echo $FILE | sed 's/\([^.]*\).*/\1/'`.bak"

If you're serious about finding out different ways to do things, read this post. Maybe you'll like one way more than another.
Reply With Quote
  #3  
Old 07-18-2003
Registered User
 

Join Date: May 2003
Location: Lake District, U.K.
Posts: 42
Question

Only the first file is renamed, Oombera...
Reply With Quote
  #4  
Old 07-18-2003
Registered User
 

Join Date: May 2003
Location: Lake District, U.K.
Posts: 42
ignore that, fixed it

thanks oombera
Reply With Quote
  #5  
Old 07-24-2003
criglerj's Avatar
Registered User
 

Join Date: May 2002
Location: Atlanta
Posts: 129
In oombera's solution, the "if" is redundant, since `ls ...` only returned the files that were there, not the ones that weren't.

Assuming the "extensions", i.e., the filename endings after a period, are fixed, my solution would be something like this:
Code:
cd $copycomp/data
for ext in a b c d ; do
    if [ -f exsyctr1.$ext ] ; then
        # Create backup file per oombera, notice shell differences.
	cp $livecomp/exsyctrl.$ext .
    fi
done
Reply With Quote
  #6  
Old 07-24-2003
oombera's Avatar
Registered User
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
Well, it might not be redundant if the particular $copycomp/data/exsyctr1.* the loop is currently testing happens to be a directory instead of a regular file.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0