无法在 bash 中将 bash 变量作为 python 参数传递

Bri*_*ris 4 python shell-script amazon-s3

出于某种原因,我无法$FOLDER在以下代码中将 bash 变量作为 python 参数传递。python 脚本从亚马逊 s3 下载一些文件。

bash脚本:

#!/bin/bash
FOLDER=$(./aws get $BUCKET"/location.txt")
python /data/pythonscript.py $FOLDER
#
Run Code Online (Sandbox Code Playgroud)

的输出$FOLDER是重新生成的日期,例如。2014/07/31/14-16-34用作路径。

这是python脚本:

#!/usr/bin/python

import boto, sys
from boto.s3.connection import S3Connection

access_key = 'accesskey'
secret_key = 'secretkey'
bucket_name = 'a name'
folder_path =  str(sys.argv[1]) if len(sys.argv) > 1 else ''

print("Forwarded folder path " + folder_path)

conn = S3Connection(access_key, secret_key)
bucket = conn.get_bucket(bucket_name)

print("Bucket Location:" + bucket.get_location())

for key in bucket.list(prefix=folder_path, delimiter=''):
        if '.' in key.name:
                file_name = key.name[len(folder_path)+1:]
                print("Downloading file " + file_name)
                key.get_contents_to_filename('/data/temp/' + file_name)
Run Code Online (Sandbox Code Playgroud)

当我在不更改python /data/pythonscript.py $FOLDER行的情况下执行 bash 脚本时,我得到以下输出:

Forwarded folder path 2014/07/31/14-16-34 
Buckect Location: 
Run Code Online (Sandbox Code Playgroud)

但是当我将其更改为 时python /data/pythonscript.py 2014/07/31/14-16-34,一切正常:

Forwarded folder path 2014/07/31/14-16-34
Bucket Location: 
Downloading 2014/07/31/14-16-34/FacetedSearch.zip 
Downloading file FacetedSearch.zip
Downloading 2014/07/31/14-16-34/Location.zip
Downloading file Location.zip
Downloading 2014/07/31/14-16-34/LocationPage.zip
Downloading file LocationPage.zip
Run Code Online (Sandbox Code Playgroud)

drs*_*drs 7

也许awsbash 命令正在返回您看不到的不可打印字符print()。尝试使用以下方法删除它们tr

FOLDER=$(./aws get $BUCKET"/location.txt" | tr -cd "[:print:]")
Run Code Online (Sandbox Code Playgroud)