gpt4 book ai didi

Bash 在循环中检测到错误然后继续处理

转载 作者:行者123 更新时间:2023-12-03 03:09:25 26 4
gpt4 key购买 nike

我想处理文本文件中包含的 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 在循环中检测到错误然后继续处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26679500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com