Using cp command inside shell scrip | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Using cp command inside shell scrip

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-20-2006
yoavbe yoavbe is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 9 April 2013, 8:13 AM EDT
Posts: 70
Thanks: 4
Thanked 0 Times in 0 Posts
Using cp command inside shell scrip

Hi,
First i would like to say that im a unix begginer.

I have a file named /tmp/sample.lst that contain about 20 rows like the following two :
'/tmp/aa.txt' '/temp/aa.txt'
'/tmp/xx.txt' '/temp/xx.txt'

Inside a ksh script i would like to do the following task:
add the cp command at the begging of each line, and after that check if the
copy command successed.

for example:

cp `cat /tmp/sample.lst`

if [ st$ -eq != 0 ]
then
echo "fail"
else
echo "success"
fi

Could one demonstraite how to run through the lines and check if each copy
succeded ?
Thank You Very Much !!!!
Sponsored Links
    #2  
Old 06-20-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 October 2014, 6:07 PM EDT
Location: NM
Posts: 10,240
Thanks: 281
Thanked 802 Times in 746 Posts

Code:
#!/bin/ksh
while read sourcefile destfile
do
      cp $sourcefile $destfile
      if [ -s $destfile ] ; then
          echo "$destfile copied successfully from $sourcefile"
      fi
done < /tmp/sample.lst

Sponsored Links
    #3  
Old 06-22-2006
thestevew thestevew is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 11 July 2008, 4:53 AM EDT
Location: South Yorkshire, UK
Posts: 114
Thanks: 0
Thanked 1 Time in 1 Post
Might be better to test the return code rather than existence of file - this would pick up errors like unable to overwrite existing file because of permissions, lack of space etc, also I'd like a specific error message - but them I'm just picky

Quote:
Originally Posted by jim mcnamara
Code:
#!/bin/ksh
while read sourcefile destfile
do
      cp $sourcefile $destfile


Code:
      if [ $? -eq 0 ] ; then
         echo "$destfile copied successfully from $sourcefile"
      else
         echo  "ERROR: failed to copy $destfile from $sourcefile"
      fi
done < /tmp/sample.lst

[/QUOTE]
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Get file name in shell scrip loop: bad substitution soueric Solaris 4 05-02-2011 11:20 PM
How to monitor a command inside shell script sussus2326 Shell Programming and Scripting 3 01-27-2011 05:41 AM
how to combine two files into one file using shell scrip netbanker Shell Programming and Scripting 2 12-06-2010 05:34 PM
How to convert shell scrip to binaric command raghunsi UNIX for Advanced & Expert Users 1 02-03-2010 01:27 AM
C Shell - Command Inside a Loop hobbers Shell Programming and Scripting 3 06-18-2009 12:04 PM



All times are GMT -4. The time now is 01:20 AM.