bash 检查用户挂载是否失败
我正在编写一个脚本来通过 sftp 传输一些文件。 我想通过使用 sshfs 挂载目录来进行本地传输,因为这样可以更轻松地创建所需的目录结构。 我遇到的问题是我不确定如何处理没有网络连接的情况。 基本上我需要一种方法来判断 sshfs 命令是否失败。 如果无法安装远程目录,有什么想法如何使脚本退出?
I'm writing a script to transfer some files over sftp. I wanted do the transfer as a local transfer by mounting the directory with sshfs because it makes creating the required directory structure much easier. The problem I'm having is I'm unsure how to deal with the situation of not having a network connection. Basically I need a way to tell whether or not the sshfs command failed. Any ideas how to cause the script to bail if the remote directory can't be mounted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需测试
sshfs
是否返回 0(成功):上面的方法有效,因为在 bash 中逻辑或
||
执行 短路评估。 允许您打印错误消息的更好的解决方案如下:编辑:
确实。 详细说明一下:大多数应用程序成功时返回 0,失败时返回另一个值。 shell 知道这一点,因此将返回值 0 解释为 true,将任何其他值解释为 false。 因此,逻辑或和否定测试(使用感叹号)。
Just test whether
sshfs
returns 0 (success):The above works because in bash the logical-or
||
performs short-circuit evaluation. A nicer solution which allows you to print an error message is the following:Edit:
Indeed. To elaborate a bit more: most applications return 0 on success, and another value on failure. The shell knows this, and thus interprets a return value of 0 as true and any other value as false. Hence the logical-or and the negative test (using the exclamation mark).
我试图检查目录是否不是 sshfs 挂载的挂载点。 使用上面的示例失败:
错误:
-bash: !( mountpoint -q /my/dir ): No such file or directory
我用以下内容修改了代码并取得了成功:
I was trying to check if a directory was not a mountpoint for an
sshfs
mount. Using the example from above failed:The error:
-bash: !( mountpoint -q /my/dir ): No such file or directory
I amended my code with the following and had success:
在 oracle linux 8 上对此进行了测试,
如果 !( mountpoint -q /my/dir ); 则 有效 然后
echo "/my/dir 不是挂载点"
别的
echo "/my/dir 是一个挂载点"
fi
此代码适用于 Centos 7
if (! mountpoint -q /my/dir ); 然后
echo "/my/dir 不是挂载点"
别的
echo "/my/dir 是一个挂载点"
菲
Tested this on oracle linux 8 and works
if !( mountpoint -q /my/dir ); then
echo "/my/dir is not a mountpoint"
else
echo "/my/dir is a mountpoint"
fi
This code worked on Centos 7
if (! mountpoint -q /my/dir ); then
echo "/my/dir is not a mountpoint"
else
echo "/my/dir is a mountpoint"
fi