Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have got to write this lines often:

# from project1 dir
scp app/models/user.rb
# from project2 dir
scp app/views/posts/index.html.erb
scp -r app/mailers

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
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=( "$@" )

    file="${args[@]: -1}"
    scp_args=( "${args[@]): 1: $(( ${#args[@]} - 2 ))}" )

    scp "${scp_args[@]}" "$file" "$proj/$remote_dir"

You can paste it into your ~/.bashrc

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .