拆分目录路径并使用正则表达式,bash

时间:2014-12-12 17:20:46

标签: bash amazon-s3

我正在尝试将一些文件上传到s3并拥有此bash脚本:

#!/bin/bash
s3upload() {
    echo $1
    for f in $(find $d \( ! -regex '.*/\..*' \) -type f)
    do
        extension=$(file $f | cut -d ' ' -f2 | awk '{print tolower($0)}')
        mimetype=$(file --mime-type $f | cut -d ' ' -f2)
        echo $mimetype
        fullpath=$(readlink -f $f)
        #response=$(s3cmd put -v setacl --acl-public \
        #       --add-header="Expires: $(date -u +"%a, %d %b %Y %H:%M:%S GMT" --date "+1 years")" \
        #       --add-header="Cache-Control: max-age=1296000, public" \
        #       --mime-type=$mimetype \
        #       $fullpath \
        #       s3://ccc-public/catalog/)
        #echo $response
    done
}
BASE='./nas/cdn/catalog'
echo $BASE
for d in $(find . -type d -regex '\{$BASE}/[^.]*')
do
    echo "Uploading $d"
    s3upload $d

done

问题是我无法将$ BASE传递给正则表达式

基本上我想将目录之后的目录路径追加到s3路径s3:// ccc-public / catalog /

./nas/cdn/catalog/swatches
./nas/cdn/catalog/product_shots
./nas/cdn/catalog/product_shots/high_res
./nas/cdn/catalog/product_shots/high_res/back
./nas/cdn/catalog/product_shots/high_res/front
./nas/cdn/catalog/product_shots/low_res
./nas/cdn/catalog/product_shots/low_res/back
./nas/cdn/catalog/product_shots/low_res/front
./nas/cdn/catalog/product_shots/thumbs
./nas/cdn/catalog/full_length
./nas/cdn/catalog/full_length/high_res
./nas/cdn/catalog/full_length/low_res
./nas/cdn/catalog/cropped
./nas/cdn/catalog/drawings

到s3:// ccc-public / catalog /

任何建议非常感谢

1 个答案:

答案 0 :(得分:1)

永远不会评估“单引号”中的变量。 $BASE需要“双引号”。

请参阅http://mywiki.wooledge.org/Quoteshttp://mywiki.wooledge.org/Argumentshttp://wiki.bash-hackers.org/syntax/words

此外,您应该使用for来处理具有特殊字符(如空格和其他惊喜)的文件,而不是使用while IFS= read -r循环。


此外,可以单独完成整个工作:

BASE='./nas/cdn/catalog'

find . -type d -regex "${BASE}/[^.]*" -exec s3upload {} \;