问题描述
下面是我在Cygwin的Windows上运行bash脚本的一个片段:
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
这产生的输出,例如:
making dir /cygdrive/c/Temp/deploy/2010-04-30_11:47:58
mkdir: missing operand
Try `mkdir --help' for more information.
不过,如果我键入 / cygdrive / C /温度/部署/ 2010-04-30_11:47:58
在命令行成功,为什么用相同的命令脚本不工作?
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?
谢谢,
唐
推荐答案
修改
mkdir -p $deploydir
到
mkdir -p "$deployDir"
最喜欢的Unix外壳(甚至全部),伯恩(再次)壳牌(SH / bash)的是区分大小写的。该目录VAR被称为 deployDir
(混合大小写)处处除的mkdir
命令,其中它被称为 deploydir
(全部小写)。由于 deploydir
(全部小写)为 deployDir
A认为是不同的变量(混合大小写)和 deplydir
(全部小写)从来没有分配给它的价值,价值 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 ("").
如果没有引号(的mkdir $ deploydir
),该行有效地成为的mkdir
(只是没有按规定命令操作数),因此误差的mkdir:缺少操作数
Without the quotes (mkdir $deploydir
), the line effectively becomes mkdir
(just the command without the required operand), thus the error mkdir: missing operand
.
使用引号(的mkdir$ deploydir
),该行有效地成为的mkdir
(命令使空字符串非法目录名称的目录),因此误差的mkdir:无法创建目录
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
'.
使用带引号(的mkdir$ deployDir
)的形式的情况下,建议在目标目录名称中包含空格。
Using the form with quotes (mkdir "$deployDir"
) is recommended in case the target directory name includes spaces.
这篇关于在bash脚本错误的mkdir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!