我正在创建一个BASH脚本,需要安装几个应用程序. ffmpeg和sox
为确保我的脚本运行时它们就位,我首先检查Homebrew的安装:
#!/bin/bash which -s brew if [[ $? != 0 ]] ; then # Install Homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)" fi
然后我检查sox和ffmpeg是否安装:
echo "---- checking for sox ----" which -s sox || /usr/local/bin/brew install sox echo "---- checking for ffmpeg ----" which -s ffmpeg || /usr/local/bin/brew install ffmpeg
我面临的问题是安装Homebrew但是在非标准位置.
我必须使用Homebrew的完整路径,因为此脚本在Playtypus内运行.
回答我自己的问题……
您可以测试brew的输出并相应地处理事物.为了优雅地处理未安装Homebrew的情况,您可以使用brew 2> / dev / null将stderr重定向到/ dev / null.
brew –prefix在这里也很有用,因为它提供了Homebrew安装的应用程序符号链接的路径,而不是它们的实际安装路径.
#!/bin/bash if which brew 2> /dev/null; then brewLocation=`which brew` appLocation=`brew --prefix` echo "Homebrew is installed in $brewLocation" echo "Homebrew apps are run from $appLocation" else echo "Can't find Homebrew" echo "To install it open a Terminal window and type :" echo /usr/bin/ruby -e \"\$\(curl\ \-fsSL\ https\:\/\/raw\.github\.com\/Homebrew\/homebrew\/go\/install\)\" fi
感谢Allendar的指针.