[英]ZSH for loop array variable issue
它實際上比這簡單得多:
lw=('plugin1' 'plugin2' 'plugin3')
for i in $lw; do
. ~/Library/Rogall/plugins/$i/lw.prg end
done
總之:
foo
,而不是$foo
(shell 會嘗試擴展$foo
並分配給它擴展到的任何內容;通常沒有用)為什么要費心使用數組? 這可以很容易地在便攜式 sh 中完成:
lw='plugin1 plugin2 plugin3'
for i in $lw;
do . ~/Library/Rogall/plugins/$i/lw.prg end
done
請注意,要使其在 zsh 中工作,您需要使用以下命令使 zsh 做正確的事情: set -o shwordsplit
我有一個問題,像這樣的循環
for i in (1 2 3 4); do echo $i; done
一再不能按我的意願工作。 我會得到像zsh: unknown file attribute: 1
這樣的錯誤,或者像這樣的輸出
1 2 3 4
不管我如何重新扭曲它,而不是我想要的
1
2
3
4
為了獲得我想要的行為,我必須從循環定義中刪除in
關鍵字
for i (1 2 3 4); do echo $i; done
1
2
3
4
對於最終在這里與文件名中的 shell 腳本、數組和空格搏斗的其他人......就像我下面的文件列表一樣;
$ ls *.meta
20160324 1850 - ABC - Clarke And Dawe.ts.meta
20160706 1515 - 9Gem - Agatha Christie's Poirot.ts.meta
20181213 0155 - 10 BOLD - Star Trek_ The Next Generation.ts.meta
20210424 1927 - ABCTV HD - The Durrells.ts.meta
20210501 1927 - ABCTV HD - The Durrells.ts.meta
20210818 1930 - SBS ONE HD - Tony Robinson's World By Rail.ts.meta
經過大量搜索和嘗試不同的方法后,這是問題最少的方法,並且在 MacOS 10.15 上運行
eval "list=( $(find . -name "*.ts" -exec echo \"{}\" \;) )"
for name in "${list[@]}"
do
res=`mediainfo --Output=''Video\;\%Width\%:%Height\%'' "$name"`
echo ${name} :: $res
sed -i .bak -e 5s/\;[[:digit:]]*:[[:digit:]]*// -e 5s/$/\;$res/ "$name.meta"
done
忽略我試圖實現的內部結構,關鍵方面是使用 eval 並在 find 中的 echo 中的文件名周圍使用雙引號。 (我在做什么?我正在從 Enigma2 PVR 錄音中提取分辨率並將其寫入相關元數據文件的第 5 行)
感謝所有發布有關 shell 腳本生成的各種問題的知識的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.