shell-script – 无法在bash中将bash变量作为python参数传递

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

Bash脚本:

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

$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)

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

Forwarded folder path 2014/07/31/14-16-34 
Buckect Location: 

但当我将其更改为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
最佳答案
也许aws bash命令返回了print()没有看到的不可打印的字符.尝试用tr删除它们:

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

转载注明原文:shell-script – 无法在bash中将bash变量作为python参数传递 - 代码日志