那个时候我不小心改写了Bash ...

by Jacob Evelyn

雅各布·伊夫林(Jacob Evelyn)

那个时候我不小心改写了Bash ... (That time I accidentally overwrote Bash… in Bash)

“我知道我在做什么。” (“I know what I’m doing.”)

Five years ago, a few weeks into my very first Programming Job™, I was tinkering around in bash trying to get some code to run.

五年前,也就是我第一次编程工作™的几周后,我在进行bash修改,试图运行一些代码。

I was becoming increasingly frustrated. Why wasn’t that file in my $PATH? It should have been, I felt certain of that.

我变得越来越沮丧。 为什么我的$PATH没有该文件? 我本该确定应该的。

But, growing more and more desperate to get something to work, I moved the file to a folder I knew was in the $PATH:

但是,越来越大,越来越迫切希望得到的东西的工作,我搬到文件到我所知道的是在一个文件夹$PATH

> mv ./file.whatever /bin

At least, that’s what I meant to type. In reality what I typed was:

至少,这就是我键入。 实际上,我键入的是:

> mv ./file.whatever /bin/bash

My employer-provided MacBook Pro yelled at me, like any good UNIX system:

我的雇主提供的MacBook Pro对我大叫,就像任何好的UNIX系统一样:

mv: rename ./file.whatever to /bin/bash: Operation not permitted

And like any good programmer, I blindly added a sudo and typed in my password as quickly as I could. Stupid computer, thinking it knows better than me.

像任何优秀的程序员一样,我盲目添加了sudo并尽快输入密码。 愚蠢的计算机,以为它比我更了解。

Having just unknowingly overwritten the very shell I was using, I was shocked to find my code still wouldn’t run.

在不知不觉中覆盖了我正在使用的外壳的情况下,我震惊地发现我的代码仍然无法运行。

“我不知道我在做什么,但是没关系。” (“I don’t know what I’m doing but it’s okay.”)

I opened a new OS X Terminal tab to try a new approach to getting my code running, and instead saw this:

我打开了一个新的OS X终端选项卡,尝试一种新方法来运行我的代码,但看到了以下内容:

> permission denied: ./file.whatever
[Process completed]

Hmm, that’s weird. Not sure why bash isn’t working, but at least it’s doing something with my file!

嗯,这很奇怪。 不知道为什么bash不起作用,但至少它对我的文件有作用!

I switched back to my previously-open bash shell and continued trying a few commands. They still worked, of course, because the bash program was already in memory at the time I overwrote its executable.

我切换回以前打开的bash shell,并继续尝试一些命令。 当然,它们仍然有效,因为在我重写可执行文件时, bash程序已经在内存中。

So some Terminal tabs aren’t working, but others are. Probably ghosts in the machines.

因此,某些终端选项卡不起作用,而其他选项卡则起作用。 机器中可能有鬼影。

To sort out this weird-but-definitely-probably-not-a-big-deal bash behavior I decided to do the tried-and-true cure-all: quit the app (Terminal) and open it again.

为了解决这种怪异但肯定不是大交易的bash行为,我决定进行尝试并真正解决所有问题:退出应用程序(终端),然后再次打开它。

“天啊! 我做了什么?” (“Oh god! what have I done?”)

Out of nowhere, the reality of what had happened hit me like a snowball to the face. Oh no oh no oh no I just overwrote Bash.

无处不在,发生的事情的现实像雪球一样击中了我。 哦,不,不,我只是重写了Bash。

I no longer cared about getting my code to run. All I wanted to do was go back to the way things were.

我不再关心让我的代码运行。 我要做的就是回到原来的样子。

“这可能是可以解决的……” (“This is probably fixable…”)

I spent a long time Googling things like “deleted bash” and “download new bash OS X” and got nowhere. I was in too much of a panic to think about using other shells — which I vaguely knew about, but didn’t realize were already installed on my machine. (And I certainly didn’t realize these shells were also usable by just changing a setting in the Terminal app. #facepalm)

我花了很长时间谷歌搜索诸如“删除bash”和“下载新bash OS X”之类的东西,却一无所获。 我实在太着急了,无法考虑使用其他外壳了-我隐约知道,但没有意识到已经安装在我的机器上了。 (而且我当然不知道仅通过在终端应用程序中更改设置即可使用这些shell。#facepalm)

Eventually, I sheepishly confessed to some coworkers what I had done and after we all had a good chuckle I got one of them to email me a copy of his bash program so I could manually move it back into the /bin folder in Finder. (Hooray for point-and-click interfaces!)

最终,我令人毛骨悚然地向一些同事坦白了我的所作所为,当所有人都欢呼雀跃之后,我让其中一个人通过电子邮件将他的bash程序的副本发送给我,以便我可以将其手动移回Finder中的/bin文件夹。 (万岁的点击界面!)

Except… Finder wouldn’t let me go to the /bin folder. OS X (that version of it, at least) hid /bin and other system folders it deemed unsafe for meddling users like me to see. Stupid computer, thinking it knows better than me.

除了…Finder不允许我进入/bin文件夹。 OS X(至少是该版本)隐藏了/bin和其他系统文件夹,对于像我这样的用户来说,这样做是不安全的。 愚蠢的计算机,以为它比我更了解。

So I Googled some more, this time for things like “view hidden folders in Finder.” I found a handful of different ways to do that, but every single one of them required me to type some magic command… into bash, which I could no longer open. Kids: if you delete your shell but have an instance of it open, don’t close it!

因此,我这次在Google中搜索了更多内容,例如“在Finder中查看隐藏的文件夹”。 我找到了几种不同的方法来执行此操作,但是每个方法都要求我在bash键入一些魔术命令…,我无法再打开它。 孩子们:如果删除外壳程序但打开了它的实例,请不要关闭它!

“好吧...这也许可以解决...” (“OK… this is maybe fixable…”)

At a loss for what to do next, I found an old internal Q&A system the company had and posted a quick description of my problem, trying to strike that balance in tone between heh what a funny but probably not uncommon situation, right everyone? and please someone anyone help me I’m panicking. The site looked like it wasn’t ever used anymore but I was hoping someone got an email when a question was posted.

不知所措,下一步我发现公司有一个旧的内部问答系统,并快速描述了我的问题,试图在两种有趣但可能不常见的情况之间取得平衡,对吗? 请有人帮助我感到恐慌 。 该网站看起来好像已经不再使用了,但是我希望有人在发布问题时能收到电子邮件。

Lo and behold, my Hail Mary quickly got a response: someone recommended booting from a Linux Live CD (this was back when computers had CD drives), and then from within Linux accessing my OS X file system to add bash back to its rightful place. I understood about a third of the suggestion, but went ahead anyway — what other options did I have? I found a Linux CD, did a bunch of things I didn’t understand to get it to work, and waited impatiently as the machine went through all of the myriad setup steps until — voilà! A desktop appeared!

瞧,我的冰雹玛丽很快得到了回应:有人建议从Linux Live CD引导(这在计算机具有CD驱动器时返回),然后从Linux内部访问我的OS X文件系统以将bash添加回其应有的位置。 。 我理解了大约三分之一的建议,但无论如何都继续进行-我还有什么其他选择? 我找到了一张Linux CD,做了很多我不了解的事情才能使它正常工作,并且不耐烦地等待着机器经过所有无数的设置步骤,直到– 瞧! 桌面出现了!

I Googled around until I found out how to mount the OS X filesystem, and eagerly opened bash (what a good feeling!) to copy that machine’s bash executable back over to OS X… only to encounter an error message: the OS X partition was read-only from Linux. I did find out about a way to make it writable, but that required restarting back in OS X and — you guessed it — running a command in bash.

我四处搜寻,直到我发现如何挂载OS X文件系统,然后急切地打开bash (感觉真好!)将那台机器的bash可执行文件复制回OS X……只是遇到一条错误消息:OS X分区是从Linux只读。 我确实找到了使它可写的方法,但是需要在OS X中重新启动,并且-您猜对了-在bash运行命令。

I tried a few different Linux Live CDs (each of which took about forty minutes of impatient pacing to boot), but each had the same result. Once again: if you delete your shell but have an instance of it open, don’t close it!

我尝试了几种不同的Linux Live CD(每张CD大约需要四十分钟不耐烦的节奏启动),但是每张光盘都具有相同的结果。 再说一次:如果删除外壳程序但打开了它的实例, 请不要关闭它!

“无论如何,可修复甚至意味着什么?” (“What does fixable even mean anyway?”)

Unsure where to go from there, I reached out to coworkers again and eureka! — someone knew of a way to navigate to any folder — even hidden ones — within Finder. All I had to do was restart in OS X again, copy the emailed bash executable to /bin, and everything would be gravy. So I shut down Linux, removed the Live CD, restarted in OS X, and…

不确定从那里去哪里,我再次联系了同事, 尤里卡! —有人知道在Finder中导航到任何文件夹(甚至是隐藏文件夹)的方法。 我要做的就是再次在OS X中重新启动,将通过电子邮件发送的bash可执行文件复制到/bin ,一切都会变得很糟。 所以我关闭Linux,取出Live CD,在OS X中重新启动,然后…

Hmm. I couldn’t log in, because, well, the OS X login process uses a shell under the hood, and guess which shell that is?

嗯 我无法登录,因为OS X的登录过程在幕后使用了一个外壳,然后猜测是哪个外壳?

Was I doomed to spend the rest of my career living off of Linux Live CDs? I pictured myself years in the future, a babbling hermit kept around to scare the new kids: “Don’t delete bash or you’ll end up like crazy old Jake.”

我是否注定要用Linux Live CD来度过余生? 我对自己的未来进行了想象,一个a咕的隐士一直在吓着新来的孩子: “不要删除bash,否则您最终会像疯了的老杰克一样。”

“我从来没有更高兴看到错误消息” (“I’ve never been happier to see an error message”)

I had given up all hope, when another coworker (goodness, these people knew so much!) told me about single-user mode, a special OS X startup mode that helps you resolve login (and other) errors. Single-user mode let me boot a bare-bones, command-line version of OS X through a different shell (/bin/sh, I think). From there, it was just a matter of finding the right incantations to get the bash executable back into /bin and off of a USB drive (where I put it in another painfully slow iteration of the Linux Live CD boot).

当另一个同事(天哪,这些人知道很多!)告诉我有关单用户模式时 ,我已经放弃了所有希望,这是一种特殊的OS X启动模式,可以帮助您解决登录(和其他)错误。 单用户模式让我通过不同的外壳(我认为是/bin/sh )引导OS X的准系统命令行版本。 从那里开始,找到合适的方法将bash可执行文件放回/bin并从USB驱动器中移出(这是我将其放入Linux Live CD引导的另一个痛苦的缓慢迭代中)的问题。

Once that was done, I restarted the Mac and all was finally well again! Well, except that of course my code still didn’t run.

完成之后,我重新启动了Mac,一切终于恢复了! 好吧,除了我的代码当然还没有运行。

翻译自: https://www.freecodecamp.org/news/i-accidentally-overwrote-bash-in-bash-e612da33da4b/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值