bash 脚本中的 mkdir 错误
以下是我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改:
与
大多数 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:
to
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 themkdir
command, where it is calleddeploydir
(all lowercase). Sincedeploydir
(all lowercase) is a considered distinct variable fromdeployDir
(mixed-case) anddeplydir
(all lowercase) has never had a value assigned to it, the value ofdeploydir
(all lowercase) is empty string ("").Without the quotes (
mkdir $deploydir
), the line effectively becomesmkdir
(just the command without the required operand), thus the errormkdir: missing operand
.With the quotes (
mkdir "$deploydir"
), the line effectively becomesmkdir ""
(the command to make a directory with the illegal directory name of empty string), thus the errormkdir: cannot create directory
'.Using the form with quotes (
mkdir "$deployDir"
) is recommended in case the target directory name includes spaces.更改:
至
Change:
to
由于显而易见的原因,Windows 上的文件名中不能包含冒号。
You can't have colons in file names on Windows, for obvious reasons.