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 have got to write this lines often:

# from project1 dir
scp app/models/user.rb root@mydomain.com:/var/www/project1/app/models
# from project2 dir
scp app/views/posts/index.html.erb root@mydomain.com:/var/www/project2/app/views/posts
scp -r app/mailers root@mydomain.com:/var/www/project2/app/

so I want to wrap it as a script

scpd project1 app/models/user.rb
scpd project2 app/views/posts/index.html.erb
scpd project2 -r app/mailers

so scpd will pass all options to scp and take first argument as a dirname on server's /var/www/ path.

So the question is: what is a common way for writing terminal scripts on macos? And some links to read about it. Bash? And maybe I can write it on Ruby? :)

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Any of these work. Bash, ruby, python, perl, they all come with the system. Do it any way you know how.

share|improve this answer
    
It might be worth mentioning that OS X bundles specific versions of those, not always the newest ones (especially Ruby in 1.8.7, Python up to 2.6, etc.) –  slhck Jun 8 '11 at 11:32
    
Is there is guideline for writing such scripts? And how to define my own named script (such as scpd)? –  fl00r Jun 8 '11 at 11:33
    
@fl00r Just write the scripts. From your question I figured you know how to do that. Otherwise I'd recommend you to read the Advanced Bash-Scripting Guide first. –  slhck Jun 8 '11 at 11:35
    
@fl00r I also assumed you were already familiar with the technologies you mentioned. Sorry about that. But any guide and tutorial that works with the versions of these environments you have should work for you, except a few system-specific exceptions (e.g. bash scripts calling linux-specific programs) –  Daniel Beck Jun 8 '11 at 12:27
    
@Daniel Beck, I can write on Python/Ruby/Php/C# but I thought unix way is to use bash here :) so I wanted to see some guides for my first steps –  fl00r Jun 8 '11 at 12:34

Here's a bash function for you:

scpd () {
    # the first argument is the project
    # the last argument is the file/dir to send
    # any arguments in the middle are scp options

    args=( "$@" )

    proj="${args[0]}"
    file="${args[@]: -1}"
    scp_args=( "${args[@]): 1: $(( ${#args[@]} - 2 ))}" )
    remote_dir="${file%/*}"

    scp "${scp_args[@]}" "$file" "root@mydomain.com:/var/www/$proj/$remote_dir"
}

You can paste it into your ~/.bashrc

share|improve this answer

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.