
做時docker exec -it [container_id] /bin/bash
:
我運行了以下命令:
假設資料夾test/data
存在。
for file in $(aws s3 ls s3://foo-bucket | awk '{print $NF}'); do
aws s3 cp foo-bucket/${file} test/data && tar -xzf test/data/${file} -C test/data/
done
為此,我獲取相關路徑下的所有文件並提取。但是,以這種方式透過 shell 執行完全相同的操作時:
/bin/bash -ac "for file in $(aws s3 ls s3://foo-bucket | awk '{print $NF}') ; do aws s3 cp foo-bucket/${file} test/data && tar -xzf test/data/${file} -C test/data/ ; done"
我收到奇怪的錯誤,例如:
/bin/bash: -c: line 1: syntax error near unexpected token foo1.tar.gz'
/bin/bash: -c: line 1: foo1.tar.gz ; do aws s3 cp s3://foo-bucket/foo1.tar.gz test/data' && tar -xzf 'test/data/foo1.tar.gz' -C 'test/data/' ; done'
知道為什麼會發生嗎?
答案1
它們並不完全相同!看看 awk,你有一個
awk '{print $NF}'
在另一個你有
bash -c " .... awk '{print $NF}' ..."
在第一個中,$
受單引號保護,因此 awk 的程式是{print $NF}
,但在建構字串時,第二個中的單引號沒有任何意義。假設您沒有名為NF
Defined 的 shell 變量,後者給出
bash -c " ... awk '{print }' ..."
所以 awk 會列印指令的所有輸出aws
,而 shell 會給出語法錯誤。
$( ... ) 也可能在錯誤的時間被評估。
一般來說,總是使用單引號而不是雙引號,除非您想要評估的變數。