1

我正在尝试将一些终端命令链接在一起,以便我可以将文件解压缩,然后直接同步到 amazon s3。这是我到目前为止所拥有的,我已经正确安装了 s3cmd 工具并且可以正常工作。这对我有用。

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/

那么我该如何创建一个可以只使用变量的简单脚本?

4

1 回答 1

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.

于 2012-03-16T08:53:37.210 回答