【问题标题】:rmdir command working on linux and windowsrmdir 命令在 linux 和 windows 上工作
【发布时间】:2021-06-14 19:17:17
【问题描述】:

我有一个可以在 windows 和 linux 上运行的部署脚本。

如果目录存在,我需要删除它。 windows上的cmd命令是:

if exist BackendFrontendShare rmdir somedir /s /q

有什么方法可以编写同时支持这两种平台的命令吗?

我试过了

rm -rf somedir || if exist somedir rmdir somedir /s /q

适用于windows,但不适用于linux

【问题讨论】:

  • 我认为你不能像这样混合使用 Bash 和 cmd。
  • 如果你在windows上安装了git bash,可能你可以使用相同的脚本。

标签: bash cmd multiplatform rmdir


【解决方案1】:

PowerShell 在 Linux、Mac 和 Windows 上运行。相同的代码适用于所有平台。 https://github.com/PowerShell/PowerShell

$thedir = 'somedir'
if (Test-Path -Path $thedir) { Remove-Item -Recurse -Path $thedir -Force }

使用-WhatIf 开关来了解如果项目被删除会做什么。

Remove-Item -Recurse -Path 'somedir' -Force -WhatIf

【讨论】:

    【解决方案2】:

    您可以使用多种编程语言,但 Python 是一个不错的选择或 powershell 内核,因为您可以在 Windows 和 Linux 上安装和运行,并且允许您的命令独立于操作系统。

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 2012-02-05
      • 2016-06-09
      • 1970-01-01
      • 2016-09-09
      • 2019-10-19
      • 1970-01-01
      • 2013-03-25
      相关资源
      最近更新 更多