Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I just read the Linux scp command issue question and it reminded me that I regularily forget to specify the colon in the host part of a scp command, and thus copying a file locally instead of copying to a remote host, e.g. I do

scp foo host

instead of

scp foo host:

But I never use scp to copy a file locally. So I wonder if there is a way to make scp fail if both (the source and destination) arguments refer to local files.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You could write a wrapper function in Bash and name it scp that uses getopts to process the options into a new array leaving the source and destination in the positional parameters, then check those for the presence of "@" and ":" and, if absent, issue an error message. If they're present, then call the real scp using something like command scp "${args[@]}" "$source" "$dest". The command command causes Bash to call the program that's found in the PATH instead of the function by the same name.

Since, at least on my system, scp only supports the short option type, getopts (which also only supports short options) will work fine for this.

The function would look something like this (untested):

scp () {
    # wrapper function to prevent scp of local-only files
    options="1246BCpqrvc:F:i:l:o:P:S:
    while getopts $options option
    do
        case $option in
            1 | 2 | 4 | 6 | B | C | p | q | r | v)
                args+=($option)
                ;;
            c | F | i | l | o | P | S)
                args+=($option "$OPTARG")
                ;;
        esac
    done
    if [[ $1 != *@*:* && $2 != *@*:* ]]
    then
        echo "Local-only copy not permitted"
        echo "to override, use 'command scp ARGS'"
        return 1
    fi
    command scp "${args[@]}" "$1" "$2"
}
share|improve this answer
1  
Nice trick. However, you should consider naming your wrapper something else than "scp" (maybe "scop"). Overloading well-known commands can be a source of tricky problems because most people/scripts will expect the standard functionality. –  sleske May 17 '10 at 8:55
1  
Scripts won't use the overloaded function unless you export the function, for example scp () { ... }; export scp –  Dennis Williamson May 17 '10 at 10:49
1  
Oh, @sleske, in addition to the export information in my previous comment, this type of thing is the purpose of the command and builtin commands rather than being just a nice trick. –  Dennis Williamson May 17 '10 at 11:06
    
Interesting info about "command" and "builtin", didn't know that. Still, I'd be wary of overloading scp, if just for the "principle of least surprise". Well, maybe a matter of taste... –  sleske May 17 '10 at 15:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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