通过自定义列表循环安装多个木桶

时间:2018-11-11 12:57:18

标签: shell terminal sh

我正在尝试创建一个脚本,以使Mac可以与我需要的所有工具/应用一起使用。

我创建了我需要的酒桶列表:

CASKS=(
       transmission
       vlc
       )

我正试图遍历CASKS列表(是列表吗?)以1装1的方式安装木桶,看看实际安装的是什么:

echo "Installing casks"
for i in "${CASKS[@]}"; do
  echo "Installing "${CASKS[@]}
  brew cask install ${CASKS[@]}
done

该脚本无法正常运行。在终端上,我可以阅读:

Installing transmission vlc

我想看

Installing transmission...
(installation process going on...)

Installing VLC...
(installation process going on...)

基本上,对于CASKS列表中的所有元素,整个过程都在反复进行,而Homebrew告诉我已经安装了酒桶。

我该如何解决?

2 个答案:

答案 0 :(得分:0)

这样的循环方式:

SELECT 
    t.`ticket_id`, 
    t.`subject`, 
    t.`user_id`, 
    t.`created_date`, 
    t.`last_updated`
    ( 
      -- coalesce handles case when no rows
      SELECT COALESCE(SUM(`ticket_id` > 0), 1)
      FROM `tblReadBy` 
      WHERE `ticket_id` = t.`ticket_id` AND 
            `user_id` = 'XXX' AND 
            `last_read_time` < t.`last_updated` 
    ) AS `has_unread_msg`
FROM `tblTickets` AS t
WHERE t.`creator_id` = 'XXX'
ORDER BY t.`last_updated` DESC    

您可以通过简单的方法来做到这一点,只需在脚本中完成即可:

## declare an array variable
## can be on the same line or on multi-lines
declare -a arr=("zsh" "zsh-completions"
                "ffmpeg --with-zimg --with-xz --with-theora --with-tools --with-rubberband --with-fontconfig --with-frei0r --with-game-music-emu --with-libass --with-libcaca --with-libssh --with-libvorbis --with-fdk-aac --with-ffplay --with-freetype --with-libass --with-libquvi --with-opencore-amr --with-openh264 --with-openjpeg --with-theora --with-snappy --with-two-lame --with-wavpack --with-webp --with-xz --with-libvorbis --with-libvpx --with-opus --with-x265"
                "asciidoc"
)

## now loop through the above array with use of the loop-variable `i`
for i in "${arr[@]}"
do
   echo "Installing $i..."
   brew install $i
done

每次添加内容时只需添加新的brew install zsh brew install zsh-completions brew install ffmpeg --with-zimg --with-xz --with-theora --with-tools --with-rubberband --with-fontconfig --with-frei0r --with-game-music-emu --with-libass --with-libcaca --with-libssh --with-libvorbis --with-fdk-aac --with-ffplay --with-freetype --with-libass --with-libquvi --with-opencore-amr --with-openh264 --with-openjpeg --with-theora --with-snappy --with-two-lame --with-wavpack --with-webp --with-xz --with-libvorbis --with-libvpx --with-opus --with-x265 brew install asciidoc 行,以便您可以在新计算机上重复该行...

我本人有两个这样的清单。一种用于brew install ...,另一种用于brew installs,它对我有好处:-)

答案 1 :(得分:0)

您需要实际使用循环变量(i)而不是使用${CASKS[@]}引用整个数组:

echo "Installing casks"
for i in "${CASKS[@]}"; do
  echo "Installing $i"
  brew cask install "$i"
done

还要注意,引用变量扩展名(例如"$i")通常是个好主意。 (如果您的CASKS数组元素包含多个单词,这些单词应作为brew cask install的单独参数使用,则此建议不适用;在这种情况下,您将使用brew cask install $i不带引号。)