0

I'm trying to build a secure copy protocol quick function. When I run the command it will work with a single file OR with the entire directory, but as soon as I put a /* after the local_repo it returns zsh: no matches found: hackingedu/*.

If I put the command scp hackingedu\/\* hackingedu the command works properly. I think I'm on the right track, but can't get it to work.

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/* [email protected]:/home2/alexander/public_html/hackingedu/beta
}

Command trying to execute: scp -r ~/Google\ Drive/server/Dev/git\ repositories/hackingedu/* [email protected]:/home2/alexander/public_html/hackingedu/beta

Any ideas on how to find and replace an *? If there's a better way to do this please do tell! :)

If you know how to do this in bash I would like your input as well!

References:

1
  • Are you sure that there are any (non-hidden) files in ~/Google\ Drive/server/Dev/git\ repositories/hackingedu/? The error comes from the shell before the function is even called and it essentially tells you that there are no files there.
    – Adaephon
    Aug 25, 2014 at 6:05

1 Answer 1

0

You can either prefix your scp call using noglob (which will turn off globbing for that command, e.g. noglob ls *) or use

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)'

the above should make zsh auto quote * when you use scp.

[...]

BTW, in any case, you should learn that you can easily quote special characters using ${(q)variable_name}, e.g.

% foo='*&$%normal_chars'
% echo $foo
*&$%normal_chars
% echo ${(q)foo}
\*\&\$%normal_chars

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.