xcode运行脚本wget: command not found
xcode会扫描安装在/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin这些目录中的命令。
作者的wget是通过homebrew安装的,而homebrew安装的时候是通过 https://gitee.com/cunkai/HomebrewCN/blob/master/Homebrew.sh这个脚本安装,脚本中有一句
if [[ "$UNAME_MACHINE" == "arm64" ]]; then
#M1
HOMEBREW_PREFIX="/opt/homebrew"
HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}"
else
#Inter
HOMEBREW_PREFIX="/usr/local"
HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
fi
会判断是否系统为64位,因为我电脑是M1芯片的,所以homebrew被安装到了/opt中,所以xcode去/usr/local/bin中就找不到我通过homebrew安装的命令了。
因为xcode的环境变量较本地不同,需要将wget放入到其指定的几个目录下。
本文只以wget为例 其他命令以此类推
一、运行环境
1.硬件macbook pro M1
2.系统版本macOS12.1
3.xcode版本13.2.1
4.本地已安装wget
二、问题描述
1.脚本为通过配置在build的同时运行.sh脚本,出现wget:command not found
2.本地开启终端通过 sh .sh命令是可以正常执行脚本的
三、解决方案
方法1:添加环境变量PATH(需要修改脚本文件)
1.打开终端通过命令获取wget安装目录
which wget
输出结果参考:
/opt/homebrew/bin/wget
2.在脚本中添加上面的环境/opt/homebrew/bin/
PATH=$PATH:/opt/homebrew/bin/
方法2:制作软连接(无需修改脚本文件)
1.打开终端通过命令获取wget安装目录
which wget
输出结果参考:
/opt/homebrew/bin/wget
2.通过xcode运行脚本
echo $PATH
输出结果参考:
/Users/xxx/Downloads/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin:/Users/xxx/Downloads/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/bin:/Users/xxx/Downloads/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/libexec:/Users/xxx/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/usr/bin:/Users/xxx/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/usr/local/bin:/Users/xxx/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Users/xxx/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/local/bin:/Users/xxx/Downloads/Xcode.app/Contents/Developer/usr/bin:/Users/xxx/Downloads/Xcode.app/Contents/Developer/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
3.制作wget的软连接到xcode的PATH目录下
注:前面目录为wget所在目录,后面目录为xcode环境变量目录
ln -s /opt/homebrew/bin/wget /usr/local/bin
以上方法全为一时之计
一劳永逸的方法就是卸载homebrew,重新安装到/usr/local/中,这样会自动生成软连接到/usr/local/bin中,但是在作者尝试将Homebrew卸载重装到/usr/local/中之后(重装Homebrew),进行软件安装时会报错
xxx@xiaomibijibenPro-X-15 lxjsh % brew install wget
Error: Cannot install in Homebrew on ARM processor in Intel default prefix (/usr/local)!
Please create a new installation in /opt/homebrew using one of the
"Alternative Installs" from:
https://docs.brew.sh/Installation
You can migrate your previously installed formula list with:
brew bundle dump
解决方法是找到应用程序–>实用工具–>终端app,右键显示简介,将“使用Rosetta”勾选。再进行安装,安装完记得取消勾选。
xxx@xiaomibijibenPro-X-15 lxjsh % brew install wget
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/gettext-0.2
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/libunistrin
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/libidn2-2.3
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/ca-certific
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/openssl%401
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/wget-1.21.2
######################################################################## 100.0%
完美解决!