0

I have this script for automating a simple process where I scan a number into the command line, grep that number out of a catalog file which gives me a path, and then copy the DVD which the above mentioned number corresponds to, to the path. The cp command keeps failing. I've looked at other posts where cp is not working in a script but they all seem to have to do with quotes around filenames. I don't think that's my problem here.

I've added some comments for I hope will turn out to be clarity:

\#!/bin/bash
\# accepts a single command line parameter:  six-digit DVD number, ex. 987110
\# the empty echo lines are just whitespace for readability**

clear

file=/public/TAPES/batch1/Shipment_1_catalog.txt.dvd

echo
echo DVD number is       $1

echo
echo catalogfile is      $file

>\# this grep instruction greps $1 out of $file
>\# output from this is 'XXXXXX file-path-to-copy-dvd-contents-to'
>\# qwk puts the file-path... into $pth

pth=`grep $1 $file | awk ' { print $2 } '`

echo

echo destination path is $pth

echo

mount /dev/sr0 /media     # appears to work

cp -p -r /media/\* $pth   # this always fails - see error text below.

echo

umount /dev/sr0 && eject  # this does work

Everything works except the copy command. It produces the following error output:

DVD number is 987110

catalogfile is /public/TAPES/batch1/Shipment_1_catalog.txt.dvd

destination path is > /proj/T_010/gdm/SAM/BRA/3D/Santos_ESP_3D_BDEP_2010/dvd/legacy/987110_Line_Section_Various_Vintages-Post_Stack_Migration_DVD_14_of_41

mount: block device /dev/sr0 is write-protected, mounting read-only
cp: cannot stat `/media/*': No such file or directory

The permissions for the script are 755. I've tried it as './cpy.sh' and 'bash cpy.sh' with the same results both times.

  • put a sleep 10 between mount and cp command lines. When you mount a DVD it takes a few seconds to discover the contents. – MelBurslan Feb 11 '16 at 18:25
  • 4
    Why are you escaping the *? Do you really want to find a file named * and not use it as a glob? – Eric Renouf Feb 11 '16 at 18:28
  • @MelBurslan - sleep 10 doesn't seem to help. I still get the 'cp: cannot stat /media/* ...` message. – dwesson Feb 11 '16 at 18:50
  • @EricRenouf; un-escaping the * seems to have helped. The cp command proceeds but now gives me a "cp: failed to preserve ownership for blah blah blah, : Operation not permitted" – dwesson Feb 11 '16 at 19:02
  • @EricRenouf; it looks like somehow the '-p' switch for the cp command isn't working. – dwesson Feb 11 '16 at 19:02
1

On UNIX systems wildcards are handled by the shell. The escaped * in the following command is passed to cp which looks a file named /media/* and presumably that doesn't exist.

cp -p -r /media/\* $pth

In Windows, by contrast, the * is not handled by the command processor but by the copy command.

Only root is allowed to change file ownership. Otherwise, any user of the system will be able take any other users files. So cp: failed to preserve ownership ... is expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.