bash 检查用户挂载是否失败

发布于 2024-07-21 02:21:05 字数 161 浏览 7 评论 0原文

我正在编写一个脚本来通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我要还你自由 2024-07-28 02:21:05

只需测试 sshfs 是否返回 0(成功):

sshfs user@host:dir mountpoint || exit 1

上面的方法有效,因为在 bash 中逻辑或 || 执行 短路评估。 允许您打印错误消息的更好的解决方案如下:

if !( sshfs user@host:dir mountpoint ); then
  echo "Mounting failed!"
  exit 1
fi

编辑:

我想指出的是,这是检查大多数平台上几乎所有表现良好的应用程序是否成功的方法。 – Sparr 1 分钟前

确实。 详细说明一下:大多数应用程序成功时返回 0,失败时返回另一个值。 shell 知道这一点,因此将返回值 0 解释为 true,将任何其他值解释为 false。 因此,逻辑或和否定测试(使用感叹号)。

Just test whether sshfs returns 0 (success):

sshfs user@host:dir mountpoint || exit 1

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:

if !( sshfs user@host:dir mountpoint ); then
  echo "Mounting failed!"
  exit 1
fi

Edit:

I would point out that this is how you check the success of pretty much any well behaved application on most platforms. – Sparr 1 min ago

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).

站稳脚跟 2024-07-28 02:21:05

我试图检查目录是否不是 sshfs 挂载的挂载点。 使用上面的示例失败:

if !( mountpoint -q /my/dir ); then
    echo "/my/dir is not a mountpoint"
else
    echo "/my/dir is a mountpoint"
fi

错误: -bash: !( mountpoint -q /my/dir ): No such file or directory

我用以下内容修改了代码并取得了成功:

if (! mountpoint -q /my/dir ); then
    echo "/my/dir is not a mountpoint"
else
    echo "/my/dir is a mountpoint"
fi

I was trying to check if a directory was not a mountpoint for an sshfs mount. Using the example from above failed:

if !( mountpoint -q /my/dir ); then
    echo "/my/dir is not a mountpoint"
else
    echo "/my/dir is a mountpoint"
fi

The error: -bash: !( mountpoint -q /my/dir ): No such file or directory

I amended my code with the following and had success:

if (! mountpoint -q /my/dir ); then
    echo "/my/dir is not a mountpoint"
else
    echo "/my/dir is a mountpoint"
fi
不羁少年 2024-07-28 02:21:05

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文