【问题标题】:AWS CLI command inside bash script can't locate filebash脚本中的AWS CLI命令找不到文件
【发布时间】:2017-11-01 23:08:46
【问题描述】:

我需要将一些文件从一台服务器移动到 S3 并满足一些要求:

  • 连接断开时保存传输进度
  • 如果连接断开,下次运行必须继续处理未上传的文件
  • 作为服务运行 -- TODO

所以这就是我所拥有的,它不完整,因为我在从脚本中运行命令时定位文件时遇到问题,但命令在原始运行时有效。

如何让脚本在这个脚本中找到 $source 文件?另外,有没有更好的方法呢?

命令:

aws s3 cp '/var/www/files/folder/file1.mp4' 's3://s3-public/folder/file1.mp4'

错误:

script.sh: line 20: aws s3 cp '/var/www/files/folder/file1.mp4' 's3://s3-public/folder/file1.mp4': No such file or directory

代码:

#!/bin/bash

aws_bucket='s3://s3-public/'
home='/home/user/'
files='/var/www/files/'
input="${home}videos_list.txt"
output="${home}videos_done.txt"

videos_list='()'
videos_done='()'

arrays_hydrate() {
    mapfile -t videos_list < "$input"
    mapfile -t videos_done < "$output"
}

aws_init() {
    for index in "${!videos_list[@]}"
    do
        source="${videos_list[$index]}"
        destination=$( echo "${videos_list[$index]}" | sed -e "s#^$files##" )
        length="${#source}"

        if [ "$length" -ne "0" ]; then
            command="aws s3 cp '${source}' '${aws_bucket}${destination}'"

            echo "$command"
            $( "$command" ) &
        fi
    done
}

init() {
    arrays_hydrate
    aws_init
}

init

exit 0

【问题讨论】:

  • @ruakh,关于您的编辑历史的问题。 Rev.2中的评论是错误的还是应该清理一下这个帖子?
  • 我回滚了我的编辑,因为代码转储的 部分 是必要的,这样问题才有意义。但您仍应按stackoverflow.com/help/mcve 编辑问题;您发布的代码比相关代码多得多。

标签: linux bash amazon-web-services amazon-s3 aws-cli


【解决方案1】:

如果command 参数包含aws s3 cp '/var/www/files/folder/file1.mp4' 's3://s3-public/folder/file1.mp4'(就像你的那样),那么:

$( "$command" ) &

等价于:

$( "aws s3 cp '/var/www/files/folder/file1.mp4' 's3://s3-public/folder/file1.mp4'" ) &

这显然不是你想要的:你没有任何名为 aws s3 cp '/var/www/files/folder/file1.mp4' 's3://s3-public/folder/file1.mp4' 的程序。

相反,您想使用参数s3cp/var/www/files/folder/file1.mp4s3://s3-public/folder/file1.mp4 调用aws 程序:

command=(aws s3 cp "$source" "$aws_bucket$destination")

echo "${command[@]}"
"${command[@]}" &

(其中<em>parameter_name</em>=(<em>value</em> <em>value</em> <em>value</em> <em>value</em>) 表示法将名为<em>parameter_name</em> 的参数设置为包含指定值的数组变量

【讨论】:

  • 好消息,谢谢。我明天早上试试。
猜你喜欢
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-01
  • 2016-10-07
  • 1970-01-01
  • 2023-03-10
  • 2013-11-10
相关资源
最近更新 更多