1

I am trying to chain some terminal commands together so that i can wget a file unzip it and then directly sync to amazon s3. Here is what i have so far i have s3cmd tool installed properly and working. This works for me.

mkdir extract; wget http://wordpress.org/latest.tar.gz; mv latest.tar.gz extract/; cd extract; tar -xvf latest.tar.gz; cd ..; s3cmd -P sync extract s3://suys.media/

How do i then go about creating a simple script i can just use variables?

DCHP
  • 1,111
  • 6
  • 29
  • 53
  • You should probably use `&&` instead of `;` as the former is safer (it will only continue if a chained process returns 0). See http://biodegradablegeek.com/2009/06/bash-tips-for-power-users for more info – Jwosty Mar 14 '12 at 13:22
  • Also, how _do_ you want to use the variables? – Jwosty Mar 14 '12 at 13:24
  • Hi jwosty thanks for the response obviously new to this hence the chaining, i wanted to write a little script where i could simple type something like this $ sync url s3://bucket; so all they would need to do would be insert a url and the bucket, and the whole download and upload to s3 would take place, hope this makes sense. – DCHP Mar 14 '12 at 13:37
  • mkdir extract; wget $url_var; mv latest.tar.gz extract/; cd extract; tar -xvf latest.tar.gz; cd ..; s3cmd -P sync extract $bucket_var/ kind off like that – DCHP Mar 14 '12 at 13:38

1 Answers1

1

You will probably want to look at bash scripting. This guide can help you alot; http://bash.cyberciti.biz/guide/Main_Page

For your question;

Create a file called mysync,

#!/bin/bash
mkdir extract && cd extract
wget $1
$PATH = pwd
for f in $PATH
do
   tar -xvf $f
   s3cmd -P sync $PATH $2       
done

$1 and $2 are the parameters that you call with your script. You can look at here for more information about how to use command line parameters; http://bash.cyberciti.biz/guide/How_to_use_positional_parameters

ps; #!/bin/bash is necessity. you need to provide your script where bash is stored. its /bin/bash on most unix systems, but i'm not sure if it is the same on mac os x, you can learn it by calling which command on terminal;

→ which bash
/bin/bash

you need to give your script executable privileges to run it;

chmod +x mysync

then you can call it from command line;

mysync url_to_download s3_address

ps2; I haven't tested the code above, but the idea is this. hope this helps.

Muhammet Can
  • 1,304
  • 2
  • 16
  • 30