如何在執(zhí)行一個(gè)命令或程序之前就了解它會(huì)做什么
有沒(méi)有想過(guò)在執(zhí)行一個(gè) Unix 命令前就知道它干些什么呢?并不是每個(gè)人都會(huì)知道一個(gè)特定的命令或者程序?qū)?huì)做什么。當(dāng)然,你可以用 Explainshell 來(lái)查看它。你可以在 Explainshell 網(wǎng)站中粘貼你的命令,然后它可以讓你了解命令的每個(gè)部分做了什么。但是,這是沒(méi)有必要的?,F(xiàn)在,我們從終端就可以輕易地在執(zhí)行一個(gè)命令或者程序前就知道它會(huì)做什么。 maybe
,一個(gè)簡(jiǎn)單的工具,它允許你運(yùn)行一條命令并可以查看此命令對(duì)你的文件做了什么,而實(shí)際上這條命令卻并未執(zhí)行!在查看 maybe
的輸出列表后,你可以決定是否真的想要運(yùn)行這條命令。
maybe
是如何工作的
根據(jù)開(kāi)發(fā)者的介紹:
maybe
利用python-ptrace
庫(kù)在ptrace
控制下運(yùn)行了一個(gè)進(jìn)程。當(dāng)它截取到一個(gè)即將更改文件系統(tǒng)的系統(tǒng)調(diào)用時(shí),它會(huì)記錄該調(diào)用,然后修改 CPU 寄存器,將這個(gè)調(diào)用重定向到一個(gè)無(wú)效的系統(tǒng)調(diào)用 ID(效果上將其變成一個(gè)無(wú)效操作(no-op)),并將這個(gè)無(wú)效操作(no-op)的返回值設(shè)置為有效操作的返回值。結(jié)果,這個(gè)進(jìn)程認(rèn)為,它所做的一切都發(fā)生了,實(shí)際上什么都沒(méi)有改變。
警告:在生產(chǎn)環(huán)境或者任何你所關(guān)心的系統(tǒng)里面使用這個(gè)工具時(shí)都應(yīng)該小心。它仍然可能造成嚴(yán)重的損失,因?yàn)樗荒茏柚股贁?shù)系統(tǒng)調(diào)用。
安裝 maybe
確保你已經(jīng)在你的 Linux 系統(tǒng)中已經(jīng)安裝了 pip
。如果沒(méi)有,可以根據(jù)您使用的發(fā)行版,按照如下指示進(jìn)行安裝。
在 Arch Linux 及其衍生產(chǎn)品(如 Antergos、Manjaro Linux)上,使用以下命令安裝 pip
:
sudo pacman -S python-pip
在 RHEL,CentOS 上:
sudo yum install epel-release
sudo yum install python-pip
在 Fedora 上:
sudo dnf install epel-release
sudo dnf install python-pip
在 Debian,Ubuntu,Linux Mint 上:
sudo apt-get install python-pip
在 SUSE、 openSUSE 上:
sudo zypper install python-pip
安裝 pip
后,運(yùn)行以下命令安裝 maybe
:
sudo pip install maybe
了解一個(gè)命令或程序在執(zhí)行前會(huì)做什么
用法是非常簡(jiǎn)單的!只要在要執(zhí)行的命令前加上 maybe
即可。
讓我給你看一個(gè)例子:
$ maybe rm -r ostechnix/
如你所看到的,我從我的系統(tǒng)中刪除一個(gè)名為 ostechnix
的文件夾。下面是示例輸出:
maybe has prevented rm -r ostechnix/ from performing 5 file system operations:
delete /home/sk/inboxer-0.4.0-x86_64.AppImage
delete /home/sk/Docker.pdf
delete /home/sk/Idhayathai Oru Nodi.mp3
delete /home/sk/dThmLbB334_1398236878432.jpg
delete /home/sk/ostechnix
Do you want to rerun rm -r ostechnix/ and permit these operations? [y/N] y
maybe
執(zhí)行了 5 個(gè)文件系統(tǒng)操作,并向我顯示該命令(rm -r ostechnix/
)究竟會(huì)做什么。現(xiàn)在我可以決定是否應(yīng)該執(zhí)行這個(gè)操作。是不是很酷呢?確實(shí)很酷!
這是另一個(gè)例子。我要為 Gmail 安裝 Inboxer 桌面客戶端。這是我得到的輸出:
$ maybe ./inboxer-0.4.0-x86_64.AppImage
fuse: bad mount point `/tmp/.mount_inboxemDzuGV': No such file or directory
squashfuse 0.1.100 (c) 2012 Dave Vasilevsky
Usage: /home/sk/Downloads/inboxer-0.4.0-x86_64.AppImage [options] ARCHIVE MOUNTPOINT
FUSE options:
-d -o debug enable debug output (implies -f)
-f foreground operation
-s disable multi-threaded operation
open dir error: No such file or directory
maybe has prevented ./inboxer-0.4.0-x86_64.AppImage from performing 1 file system operations:
create directory /tmp/.mount_inboxemDzuGV
Do you want to rerun ./inboxer-0.4.0-x86_64.AppImage and permit these operations? [y/N]
如果它沒(méi)有檢測(cè)到任何文件系統(tǒng)操作,那么它會(huì)只顯示如下所示的結(jié)果。
例如,我運(yùn)行下面這條命令來(lái)更新我的 Arch Linux。
$ maybe sudo pacman -Syu
sudo: effective uid is not 0, is /usr/bin/sudo on a file system with the 'nosuid' option set or an NFS file system without root privileges?
maybe has not detected any file system operations from sudo pacman -Syu.
看到?jīng)]?它沒(méi)有檢測(cè)到任何文件系統(tǒng)操作,所以沒(méi)有任何警告。這非常棒,而且正是我所預(yù)料到的結(jié)果。從現(xiàn)在開(kāi)始,我甚至可以在執(zhí)行之前知道一個(gè)命令或一個(gè)程序?qū)?zhí)行什么操作。我希望這對(duì)你也會(huì)有幫助。
Cheers!
資源: