從中央Linux機器(RHEL 6.3)中,我試圖將一組zip文件推送到一系列其他Linux主機,然後在每個目標主機上解壓這些文件。我的中央主機是hpdb1。嵌套for循環的問題
#Push zip files to other hosts
for i in {2..8}; do
scp -r /software/stage/*.zip [email protected]$i:/software/stage
done
#Unzip files to /software/stage
for i in {2..8}; do
ssh hpdb$i "for f in /software/stage/*.zip; do unzip /software/stage/"$f" done";
done
第一個for循環推送文件工作正常。但是,在運行嵌套的for循環時,我得到了以下錯誤:
[[email protected] ~]# for i in {2..8}; do ssh hpdb$i "for f in /software/stage/*.zip; do unzip /software/stage/"$f"; done"; done
unzip: cannot find or open /software/stage/, /software/stage/.zip or /software/stage/.ZIP.
unzip: cannot find or open /software/stage/, /software/stage/.zip or /software/stage/.ZIP.
貌似$f
變量是沒有得到解釋。有任何想法嗎?
更新時間:
此代碼適用。
for i in {2..7}; do
ssh hpdb$i 'for f in /software/stage/*.zip; do unzip "$f" -d /software/stage; done';
done
我只是打算寫,這個是原來的問題的註釋。我認爲這是答案,試試吧! –
羅布和DopeGhoti,你們大多是正確的,但讓我在正義的道路上... 單引號允許找到文件,但是當它運行時,我得到以下錯誤:unzip:無法找到或打開/software/stage/software/stage/p13390677_112040_Linux-x86-64_1of7.zip 因此,我將語法更改爲hpdb $ i'for f in /software/stage/*.zip;解壓縮「$ f」;完成「;完成 這工作,但它解壓到/根 一切進一步修改它進一步使用-d標誌解壓縮工作就像一個冠軍! hpdb $ i'for f in /software/stage/*.zip;解壓縮「$ f」-d/software/stage;完成「;完成 – user3311061