-3

I can successfully execute this command from the terminal: sudo cp -R -u "/media/gabriela/Windows/Data/Untitled Folder" /media/gabriela/USB

But the following script does not work, with the appropriate entries entered:

sudo bash /home/gabriela/Desktop/copyfolder.sh

(to start the script, not part of the script)

read source

read destination

cp -R -u source destination

The error message says, cp: cannot stat 'source': No such file or directory. This is with the exact same info entered as in the plain cp command which works.

2
  • 2
    To get the values of the variables you need "$source" and "$destination" rather than source and destination - but it is usually preferable to pass arguments to the script (see for example Passing arguments to a script) rather than read them interactively Commented Jun 22 at 0:58
  • Thank you, the first suggestion worked with modification (see below). I am not (yet) smart enough to figure out the suggestion about passing arguments to a script. Thanks again. Commented Jun 22 at 3:06

1 Answer 1

4

If you want to read the values at runtime you should use the following syntax:

read source
read destination
cp -R -u "$source" "$destination"

It's essential to use the dollar sign and the double quotes when you want to use the variables afterwards.

2
  • Thank you. I fixed that syntax but got the same error. Then I tried renaming the source folder to simply Untitled (removing the space so no quote marks were needed) and then it worked. And now I tried it one more time with the original folder name but without quote marks, and that worked too. So I guess spaces are OK without quotation marks when the script is set like this. Thank you for your help, it works now. Commented Jun 22 at 2:57
  • @GabrielaRemow no, spaces are never OK without quotes. Always quote your variables. Also, if one of the answers here solved your issue, please take a moment and accept it by clicking on the checkmark on the left. That is the best way to express your thanks on the Stack Exchange sites.
    – terdon
    Commented Jun 22 at 12:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .