bash 脚本与 grep 命令不

bash 脚本与 grep 命令不

我有这部分 shell 脚本:

#!/bin/bash
shopt -s extglob
currentDate=$(date +%F)

echo $currentDate
command="grep $currentDate"
gcs3='s3://gc-reporting-pud-production/splunk_printer_log_files'
gcs3ls='aws s3 ls 's3://gc-reporting-pud-production/splunk_printer_log_files/SOUTH_ASIA/' --recursive '
ssyss3=s3://ssyssplunk

gcs3Current=$($gcs3ls|$command|sed 's/^.*\(splunk_printer.*\)/\1/g')

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
s3ls='aws s3 ls --human-readable --summarize 's3://ssyssplunk/' --recursive'
echo "ls: " $s3ls
egrepCommand="'$currentDate|Total'"
echo "grep: " $egrepCommand
totalSize=$($s3ls|egrep $currentDate\|Total|awk -F 'Total Size:' '{print $2}'|sed '/^$/d')
echo "total size: "  $totalSize
IFS=$SAVEIFS

我收到此错误:

2019-05-27 ls:aws s3 ls --人类可读 --summarize s3://ssyssplunk/ --recursive grep:'2019-05-27 |总计'./copyFilesFromS13.sh:第54行:aws s3 ls --人类可读 --summarize s3://ssyssplunk/ --recursive:没有这样的文件或目录总大小:

我究竟做错了什么 ?

答案1

您已将 IFS 设置为仅换行符和退格键。因此$s3ls,在扩展和分词之后,将被aws s3 ls --human-readable --summarize s3://ssyssplunk/ --recursive视为单个单词。 Bash 尝试将这个单词作为命令来执行,而不是aws使用一堆参数来执行。

您确实不应该将命令存储在变量中。使用数组代替:

s3ls=(aws s3 ls --human-readable --summarize 's3://ssyssplunk/' --recursive)
#...
totalSize=$("${s3ls[@]}" | ...)

相关内容