我想处理文本文件中包含的Homebrew公式列表。如果出现安装错误(例如,已经安装,公式名称错误),我希望它写入错误,但继续进行处理。 Github project

到目前为止,我有:

...
# process list of formulas that have been installed
for i in $(cat $FILE) ; do

    echo "Installing $i ..."

    # attempt to install formula; if error write error, process next formula

    brew install $i

done
...


我该怎么做呢?

最佳答案

有帮助吗?

...
# process list of formulas that have been installed
for i in $(< "$FILE") ; do

    echo "Installing $i ..."

    # attempt to install formula; if error write error, process next formula

    brew install "$i" || continue

done
...


请注意,如果公式中包含空格,则for循环将拆分该行。最好写:

...
# process list of formulas that have been installed
while read i ;  do

    # Jump blank lines
    test -z "$i" && continue

    echo "Installing $i ..."

    # attempt to install formula; if error write error, process next formula

    brew install "$i" || continue

done < "$FILE"
...

关于bash - Bash在循环中检测到错误,然后继续处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26679500/

10-11 03:41