002_网上脚本执行请注意,慎重

本文记录了一次使用Ansible时遇到的dyld库加载错误,并分享了解决方案。作者误用了一个脚本导致/usr/local下的软链接被删除,进而影响了多个命令的正常使用。文章详细介绍了手动恢复软链接的过程,包括如何使用Homebrew重新链接软件包。

一、进坑过程如下

执行ansible遇到这种错误:
(1)
dyld: Library not loaded: @executable_path/../.Python
  Referenced from: /your/home/.virtualenvs/virtualenv_name/bin/python
  Reason: image not found
[1]    95221 trace trap  python

(2)于是从Google查解决方案。看到是之前同事写的,就没细看就执行了,执行关键内容如下
#https://gist.github.com/tevino/1a557a0c200d61d4e4fb

read -p "‼️  Press Enter if you are not sure (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "♻️  Removing old symbolic links......"
    find "$ENV_PATH" -type l -delete -print      #######important########
    echo "?  Creating new symbolic links......"
    $SYSTEM_VIRTUALENV "$ENV_PATH"
    echo "?  Done!"
fi

(3)执行完就哭了,很多命令都找不到了,原因是删除了/usr/local及/usr/local/bin下的所有软连接

(4)粗糙的执行恢复:
cd /usr/local/bin
for i in `ls ../Cellar/*/*/bin`;do
    softlinksrc=`sudo find /usr/local/Cellar  -name $i|sed  's#/usr/local#..#g'`
    ln -s $softlinksrc .
done
(5)或执行以下恢复
Updating Homebrew...
Warning: nginx 1.13.12 is already installed, it's just not linked
You can use `brew link nginx` to link this version.
即:
brew link custom_software_name
➜  nginx brew link --overwrite --dry-run wget
Would remove:
/usr/local/bin/wget -> /usr/local/Cellar/wget
../Cellar/wget/1.19.1_1/bin/wget
(6)
for cs in `ls /usr/local/Cellar`;do
    brew link --overwrite --dry-run $cs
    brew link $cs
done

拿wget举例:

这种warning单独处理下吧.已经提示很明显了

二、总结

坑的连接如下:
#https://gist.github.com/tevino/1a557a0c200d61d4e4fb

其中有个叫laymonk的哥们明确说了这有坑了,所以以后Google和github出的内容一定要看完,谨记!!!

laymonk commented on Oct 7, 2016 • 
I stumbled upon this, and just had to comment.

I imagine you reckon you are being helpful, but this a most dangerous thing to ask people to run. Running it could easily delete all links in /usr/local, which is the standard place homebrew installs pip (so, your $ENV_PATH will be /usr/local)

The simple fix involves just cd'ing into the virtualenv, and deleting all the stale links to the upgraded python by running:
find . -type l -delete

.. and then re-installing your virtualenv:
mkvirtualenv venv-name or virtualenv venv-name

An unsuspecting user may not understand the implication of answering yes, and have all symlinks blown away from /usr/local .. I think you should take this down, or amend it. No offence intended.

三、遗留问题处理

(1)diagnosis

➜  ~ brew doctor    #根据brew doctor的建议进行响应的操作,notice warning section.
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  python
  ruby
  vim

Warning: Homebrew's sbin was not found in your PATH but you have installed
formulae that put executables in /usr/local/sbin.
Consider setting the PATH for example like so
  echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zshrc

Warning: Broken symlinks were found. Remove them with `brew prune`:
  /usr/local/bin/bin
  /usr/local/bin/erb
  /usr/local/bin/gem
  /usr/local/bin/irb
  /usr/local/bin/rake
  /usr/local/bin/rdoc
  /usr/local/bin/ri
  /usr/local/bin/ruby
  /usr/local/bin/sqlite3

(2)multi-version question

➜  ~ brew ls --versions | grep vim
vim 8.0.1250 8.0.1600
➜  ~ brew uninstall --force vim
➜  ~ brew install vim    #解决

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值