Replace 如何在zsh中查找和替换字符串中的特殊字符

Replace 如何在zsh中查找和替换字符串中的特殊字符,replace,zsh,Replace,Zsh,我正在尝试构建一个安全复制协议快速功能。当我运行该命令时,它将处理单个文件或整个目录,但当我将/*放在本地_repo之后,它将返回zsh:no matches found:hackingedu/* 如果我使用scp hackingedu\/\*hackingedu命令,该命令将正常工作。我想我的思路是对的,但无法让它发挥作用 contains() { string="$1" substring="$2" if test "${string#*$substring}" !=

我正在尝试构建一个安全复制协议快速功能。当我运行该命令时,它将处理单个文件或整个目录,但当我将/*放在本地_repo之后,它将返回zsh:no matches found:hackingedu/*

如果我使用scp hackingedu\/\*hackingedu命令,该命令将正常工作。我想我的思路是对的,但无法让它发挥作用

contains() {
    string="$1"
    substring="$2"
    if test "${string#*$substring}" != "$string"
    then
        # echo '$substring is in $string'
        return 1    # $substring is in $string
    else
        # echo '$substring is not in $string'
        return 0    # $substring is not in $string
    fi
}


# Quickly scp files in Workspace to Remote
function scp() {
    local_repo="$1"
    remote_repo="$2"

    # find all the `*` and replace with `/*`
    if [ contains $local_repo '*' ]; then
        # replace all instances of * with \*                 <- HOW TO DO
    fi
    command scp -r $LOCAL_REPOS/$local_repo $ALEX_SERVER_UNAME@$ALEX_SERVER_PORT:$ALEX_REMOTE_ROOT_PATH/$remote_repo

    # Description: $1: Local Repo  |  $2:  Remote Repo
    # Define ex: scpp local/path/to/file/or/directory/* remote/path/to/file/or/directory/*
    # Live ex: scpp alexcory/index.php alexcory/index.php
    # Live ex: scpp alexcory/* alexcory/*
    #
    # This Saves you from having long commands that look like this:
    # scp -r ~/Google\ Drive/server/Dev/git\ repositories/hackingedu/* alexander@alexander.com:/home2/alexander/public_html/hackingedu/beta
}
试图执行的命令:scp-r~/Google\Drive/server/Dev/git\repositories/hackingedu/*alexander@alexander.com:/home2/alexander/public_html/hackingedu/beta

关于如何查找和替换*,有什么想法吗?如果有更好的方法,请告诉我!:

如果您知道如何在bash中执行此操作,我也希望您的输入

参考资料:


您可以使用noglob作为scp调用的前缀,noglob将关闭该命令的globing,例如noglob ls*或使用

当您使用scp时,上述内容应使zsh自动报价*

[……]

顺便说一句,在任何情况下,您都应该了解可以使用${qvariable_name}轻松地引用特殊字符,例如


您确定~/Google\Drive/server/Dev/git\repositories/hackingedu/中有任何非隐藏文件吗?错误来自shell,甚至在调用函数之前,它本质上告诉您那里没有文件。
autoload -U url-quote-magic
zle -N self-insert url-quote-magic
zstyle -e :urlglobber url-other-schema '[[ $words[1] == scp ]] && reply=("*") || reply=(http https ftp)'
% foo='*&$%normal_chars'
% echo $foo
*&$%normal_chars
% echo ${(q)foo}
\*\&\$%normal_chars