
我有這部分 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[@]}" | ...)