简体   繁体   English

有条件地执行.bashrc命令

[英]Conditionally execute a .bashrc command

I wanted to attach my Google Drive to my local machine (linux) so that I could access it via my terminal. 我想将Google云端硬盘附加到本地计算机(Linux)上,以便可以通过终端访问它。

After some googling, I saw that I could install google-drive-ocamlfuse to do this. 经过一番谷歌搜索,我看到我可以安装google-drive-ocamlfuse来做到这一点。

When I boot my machine, I have to type: 当我启动机器时,我必须输入:

google-drive-ocamlfuse ~/google-drive

to mount Google Drive. 挂载Google云端硬盘。

To avoid doing this every time, I added the line to my .bashrc. 为了避免每次都这样做,我将行添加到了.bashrc中。 Which works fine. 哪个工作正常。 But then every subsequent terminal I open tries to run the line, and I get the message: 但是随后我打开的每个后续终端都尝试运行该行,并得到以下消息:

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

I presume I get this message because it is trying to mount something that is already mounted. 我想我收到此消息是因为它正在尝试挂载已经挂载的东西。 Is there any way I can make this line in my .bashrc only execute if it is the first time the terminal is opened in a session. 有什么办法可以使我的.bashrc中的这一行仅在终端在会话中首次打开时才执行。 Or some other way I can stop the warning? 还是我可以停止警告的其他方式?

Not a big deal, but it would be nice to learn something. 没什么大不了的,但是学习一些东西会很好。

Try putting this command in a start up file: 尝试将此命令放在启动文件中:

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

mountpoint tests to see if its argument is a mountpoint. mountpoint测试以查看其参数是否为安装点。 If it isn't, then mountpoint returns false and that triggers || 如果不是,则mountpoint返回false并触发|| to execute the second command. 执行第二个命令。

mountpoint is part of the util-linux collection of utilities. mountpoint是util-linux实用程序集合的一部分。

You may put this command in one of your shell start up files but it is probably better placed in one of the system start up files. 您可以将此命令放在一个外壳启动文件中,但最好将其放在一个系统启动文件中。 Depending on your distribution, that file might be /etc/rc.local . 根据您的分发,该文件可能是/etc/rc.local

Alternate form for conditional execution 条件执行的替代形式

If you prefer, conditional execution can be performed using the if-then statements: 如果愿意,可以使用if-then语句执行条件执行:

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

Here, ! 在这里! negates the exit code of mountpoint so that google-drive-ocamlfuse is only executed if mountpoint returns false. 取消mountpoint的退出代码,以便仅在mountpoint返回false时才执行google-drive-ocamlfuse。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM