bash 脚本中的 mkdir 错误

发布于 2024-08-31 01:20:09 字数 553 浏览 8 评论 0原文

以下是我在 Windows 上的 cygwin 下运行的 bash 脚本的片段:

deployDir=/cygdrive/c/Temp/deploy

timestamp=`date +%Y-%m-%d_%H:%M:%S`
deployDir=${deployDir}/$timestamp

if [ ! -d "$deployDir" ]; then
    echo "making dir $deployDir"
    mkdir -p $deployDir
fi

这会产生如下输出:

making dir /cygdrive/c/Temp/deploy/2010-04-30_11:47:58
mkdir: missing operand
Try `mkdir --help' for more information.

但是,如果我输入 /cygdrive/c/Temp/deploy/2010-04-30_11:47: 58 在命令行上它成功了,为什么相同的命令在脚本中不起作用?

谢谢, 大学教师

The following is a fragment of a bash script that I'm running under cygwin on Windows:

deployDir=/cygdrive/c/Temp/deploy

timestamp=`date +%Y-%m-%d_%H:%M:%S`
deployDir=${deployDir}/$timestamp

if [ ! -d "$deployDir" ]; then
    echo "making dir $deployDir"
    mkdir -p $deployDir
fi

This produces output such as:

making dir /cygdrive/c/Temp/deploy/2010-04-30_11:47:58
mkdir: missing operand
Try `mkdir --help' for more information.

However, if I type /cygdrive/c/Temp/deploy/2010-04-30_11:47:58 on the command-line it succeeds, why does the same command not work in the script?

Thanks,
Don

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

柠北森屋 2024-09-07 01:20:09

更改:

mkdir -p $deploydir

mkdir -p "$deployDir"

大多数 Unix shell(甚至可能是所有 shell)一样,Bourne(再次)Shell (sh/bash) 区分大小写。 dir var 在任何地方都被称为 deployDir(混合大小写),除了 mkdir 命令,它被称为 deploydir(全部小写)。由于 deploydir (全部小写)被认为是与 deployDir (混合大小写)不同的变量,并且 deplydir (全部小写)从未有过值分配给它时,deploydir 的值(全部小写)为空字符串 ("")。

如果没有引号 (mkdir $deploydir),该行实际上会变为 mkdir(只是没有所需操作数的命令),因此会出现错误 mkdir: Missing operand代码>.

使用引号 (mkdir "$deploydir"),该行实际上变为 mkdir "" (使用空字符串的非法目录名创建目录的命令),因此错误mkdir:无法创建目录'。

如果目标目录名称包含空格,建议使用带引号的形式 (mkdir "$deployDir")。

Change:

mkdir -p $deploydir

to

mkdir -p "$deployDir"

Like most Unix shells (maybe even all of them), Bourne (Again) Shell (sh/bash) is case-sensitive. The dir var is called deployDir (mixed-case) everywhere except for the mkdir command, where it is called deploydir (all lowercase). Since deploydir (all lowercase) is a considered distinct variable from deployDir (mixed-case) and deplydir (all lowercase) has never had a value assigned to it, the value of deploydir (all lowercase) is empty string ("").

Without the quotes (mkdir $deploydir), the line effectively becomes mkdir (just the command without the required operand), thus the error mkdir: missing operand.

With the quotes (mkdir "$deploydir"), the line effectively becomes mkdir "" (the command to make a directory with the illegal directory name of empty string), thus the error mkdir: cannot create directory'.

Using the form with quotes (mkdir "$deployDir") is recommended in case the target directory name includes spaces.

染年凉城似染瑾 2024-09-07 01:20:09

更改:

mkdir -p $deploydir

mkdir -p "$deploydir"

Change:

mkdir -p $deploydir

to

mkdir -p "$deploydir"
没︽人懂的悲伤 2024-09-07 01:20:09

由于显而易见的原因,Windows 上的文件名中不能包含冒号。

You can't have colons in file names on Windows, for obvious reasons.

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