有条件地执行.bashrc命令

时间:2017-08-05 19:23:17

标签: linux bash google-drive-api

我想将我的Google云端硬盘附加到我的本地计算机(linux),以便我可以通过终端访问它。

经过一些谷歌搜索,我看到我可以安装google-drive-ocamlfuse来做到这一点。

当我启动机器时,我必须输入:

google-drive-ocamlfuse ~/google-drive

安装Google云端硬盘。

为了避免每次都这样做,我将行添加到我的.bashrc中。哪个工作正常。但随后我打开的每个后续终端都试图运行该行,我收到消息:

fuse: mountpoint is not empty
fuse: if you are sure this is safe, use the 'nonempty' mount option

我认为我收到此消息是因为它正在尝试挂载已挂载的内容。有没有什么办法可以让我的.bashrc中的这一行只在第一次在会话中打开时才执行。或者其他一些方法我可以停止警告?

没什么大不了的,但学习一些东西会很好。

1 个答案:

答案 0 :(得分:5)

尝试将此命令放在启动文件中:

mountpoint ~/google-drive || google-drive-ocamlfuse ~/google-drive

mountpoint测试其参数是否为挂载点。如果不是,那么mountpoint返回false并触发||执行第二个命令。

mountpoint是util-linux实用程序集合的一部分。

您可以将此命令放在一个shell启动文件中,但最好放在其中一个系统启动文件中。根据您的分发,该文件可能是/etc/rc.local

条件执行的替代形式

如果您愿意,可以使用if-then语句执行条件执行:

if ! mountpoint ~/google-drive
then
  google-drive-ocamlfuse ~/google-drive
fi

此处,!否定了mountpoint的退出代码,因此只有在mountpoint返回false时才会执行google-drive-ocamlfuse。

相关问题