0

I need to prompt the user for a folder-path to SCP that folder from a server to my computer.

Since I know that there's a space in the folder-name (which cannot be omitted, unfortunately) I try to make that work, but I cant.

Currently I have

#copy.sh
read -r -p "Plot date: " plot_date
scp -r user@server:/home/user/Documents/projects/pages/$plot_date Plots/

where I know that plot_date is on the form yyyy-mm-dd hh.mm e.g 2021-09-06 10.51, but that does not work e.g:

./copy.sh
Plot date: 2021-09-06 10.51
user@server's password: *******
scp: /home/user/Documents/projects/pages/2021-09-06: No such file or directory
cp: cannot stat '17.10': No such file or directory

which seems like, it splits up the date and the hour.

I have tried include some double-quotes based on this SO answer i.e

#copy.sh
read -r -p "Plot date: " plot_date
scp -r user@server:/home/user/Documents/projects/pages/"$plot_date" Plots/

which throws almost the same error

scp: /home/user/Documents/projects/pages/2021-09-06: No such file or directory
scp: 17.10: No such file or directory

I have tried save the input into another variable and use that (to ommit the string in the path)

read -r -p "Plot date: " plot_date
p_date="$plot_date"
scp -r user@server:/home/user/Documents/projects/pages/$p_date Plots/

which throws the same error as just above.

Atlast I have tried putting the entire path in a string i.e

read -r -p "Plot date: " plot_date
p_date="$plot_date"
scp -r "user@server:/home/user/Documents/projects/pages/$plot_date" Plots/

which throws the same error as the bottom 2 errors.

Now I'm really out of things to try

EDIT:

The answer here is regarding the user input, which forces the user to input as 2021-09-09\ 17\.51. I want to do the handling in the script i.e making the user able to just write 2021-09-06 17.51 as input

CutePoison
  • 4,679
  • 5
  • 28
  • 63
  • Sort of - the issue is though that I want to do that in the script and not in the input (that forces the user to write it as `2021-09-06\ 10\.51`) – CutePoison Sep 06 '21 at 16:16
  • 1
    Look at the answer using parameter expansion to escape spaces automatically. – Shawn Sep 06 '21 at 16:32

1 Answers1

1

If both ends are using bash, you can use @Q parameter expansion:

read -r -p "Plot date: " plot_date
scp -r user@server:/home/user/Documents/projects/pages/"${plot_date@Q}" Plots/
${parameter@operator}
    Parameter transformation.  The expansion is either a transforma‐
    tion of the value of parameter or  information  about  parameter
    itself,  depending on the value of operator.  Each operator is a
    single letter:

    Q      The expansion is a string that is the value of  parameter
            quoted in a format that can be reused as input.
jhnc
  • 11,310
  • 1
  • 9
  • 26