0

I have a text file on the server created scp_command.txt which I need to use as the content changes dynamically.

So retrieving the content this way.

copy_command="$(cat $CI_PROJECT_DIR/scp_command.txt)"

And the content looks this way.

echo $copy_command
scp -q -i ssh_key.pem %s ec2-user@ec2-35-86-96-6.us-west-2.compute.amazonaws.com:~

To use this command, I tried like this.

 $(echo ${copy_command/"%s"/"~/.project-n/1.txt"})

But I am getting error as below.

~/.project-n/1.txt: No such file or directory

new_copy_command=${copy_command/"%s"/"~/.project-n/1.txt"}
echo $new_copy_command
scp -q -i ssh_key.pem ~/.project-n/1.txt ec2-user@ec2-35-86-96-6.us-west-2.compute.amazonaws.com:~
$new_copy_command
~/.project-n/1.txt: No such file or directory
$($new_copy_command)
~/.project-n/1.txt: No such file or directory

But if I ran the content directly, it works,

echo $new_copy_command
scp -q -i ssh_key.pem ~/.project-n/1.txt ec2-user@ec2-35-86-96-6.us-west-2.compute.amazonaws.com:~

scp -q -i ssh_key.pem ~/.project-n/1.txt ec2-user@ec2-35-86-96-6.us-west-2.compute.amazonaws.com:~
Jor-El
  • 187
  • 3
  • 11
  • did you try with `eval` ? – fra May 20 '22 at 06:55
  • yes, eval $new_copy_command seems to working. Can you suggest how to use the same for folders? I am getting this error when I try to copy the folder. /c/Users/uday1/.project-n: not a regular file Changed to -r also but same issue – Jor-El May 20 '22 at 07:00
  • 1
    I'd avoid `eval` -- it has a well-deserved reputation as a bug magnet. The basic problem here is that `~` doesn't expand to the actual home folder path when it's quoted, so [use `$HOME` instead](https://stackoverflow.com/questions/8409024/desktop-test-txt-no-such-file-or-directory). Also, [don't use `$(echo something)`](https://www.shellcheck.net/wiki/SC2116) -- the `echo` and the `$( )` basically cancel each other out (except there's an opportunity for weird parsing between them). As for folders, what so you mean by "Changed to -r"? – Gordon Davisson May 20 '22 at 07:53
  • Some post they mentioned to add argument -r for copying folders but that too didn't work. – Jor-El May 20 '22 at 08:27

1 Answers1

-1

Do not quote the filename, remove the quotes which associated with your file name ~/.project-n/1.txt

Ex:

 $(echo ${copy_command/"%s"/~/.project-n/1.txt})
S. Mondal
  • 54
  • 5