在围绕scp的简单函数包装中使用波浪号〜的问题 [英] Issues using the tilde ~ in a simple function wrapper around scp
问题描述
我想在我的 .bashrc
中放置一个简单的 bash
函数,该函数通过接受'source'参数和'目的地"的论点,到目前为止,两者都尝试过
I want place a simple bash
function in my .bashrc
that wraps around the scp
command by accepting a 'source' argument and 'destination' argument, and so far have tried both
function send() {
eval "scp $1 user@annoyingly-long-server-name:$2"
}
和
function send() {
scp $1 user@annoyingly-long-server-name:$2
}
...但是当我在上述任一情况下称呼
...but when I call either of the above a la
send file.txt ~/
我收到错误消息 scp:远程计算机上的主目录:不支持的操作
.在回显每个参数之后,似乎代字号已扩展到远程计算机的主目录,然后进行评估.我该如何预防?
I get the error scp: home-directory-on-remote-machine: Operation not supported
. After echoing each argument, it seems that the tilde is expanded into the remote machine's home directory before evaluation. How can I prevent this?
推荐答案
我想出了一种方法,可以使用
I figured out a way to convert back from the expanded tilde to the string ~
inside the function using this thread. The function is now
function send() {
dest="$2"
dest="${dest/#$HOME/\~}"
scp $1 user@annoyingly-long-server-name:$dest
}
在第二行中,如果字符串"$ HOME"
出现在第二个参数的开头,则将其替换为波浪号〜
.如果源和目标具有相同的 $ HOME
,并且用户实际 did 明确提供了目标路径,则转换为〜<不会有任何危害./code>,但是如果它们不具有相同的
$ HOME
,则可以解决此问题.
In the second line, if the string "$HOME"
appears at the start of the second argument, it is replaced by a tilde ~
. If the source and destination have identical $HOME
s and the user actually did supply the destination path explicitly, it won't do any harm to convert to ~
, but if they do not have identical $HOME
s, it fixes the problem.
由于某种原因,在执行字符串替换之前,我似乎不得不将 $ 2
分配给变量.
For some reason it seemed that I had to assign $2
to a variable before performing the string replacement.
这篇关于在围绕scp的简单函数包装中使用波浪号〜的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!