[英]Directory name created with a dot ,using shell script
I am using Cygwin Terminal to run shell to execute shell scripts of my Windows 7 system.我正在使用 Cygwin Terminal 运行 shell 来执行我的 Windows 7 系统的 shell 脚本。 I am creating directory , but it is getting created with a dot in name.我正在创建目录,但它的名称中带有一个点。
test.sh
#!/bin/bash
echo "Hello World"
temp=$(date '+%d%m%Y')
dirName="Test_$temp"
dirPath=/cygdrive/c/MyFolder/"$dirName"
echo "$dirName"
echo "$dirPath"
mkdir -m 777 $dirPath
on executing sh test.sh
its creating folder as Test_26062015
while expectation is Test_26062015
.Why are these 3 special charterers coming , how can I correct it在执行sh test.sh
它的创建文件夹为Test_26062015
而期望为Test_26062015
为什么这 3 个特殊的承租人来了,我该如何纠正它
Double quote the $dirPath
in the last command and add -p
to ignore mkdir
failures when the directory already exists: mkdir -m 777 -p "$dirPath"
.在最后一个命令中双引号$dirPath
并添加-p
以在目录已经存在时忽略mkdir
失败: mkdir -m 777 -p "$dirPath"
。 Besides this, take care when combining variables and strings: dirName="Test_${temp}"
looks better than dirName="Test_$temp"
.除此之外,在组合变量和字符串时要小心: dirName="Test_${temp}"
看起来比dirName="Test_$temp"
更好。
Also, use this for static analysis of your scripts.此外,将其用于脚本的静态分析。
UPDATE: By analyzing the debug output of sh -x
, the issue appeared due to DOS-style line-endings in the OP's script.更新:通过分析sh -x
的调试输出,问题出现是由于 OP 脚本中的 DOS 风格的行尾。 Converting the file to UNIX format solved the problem.将文件转换为 UNIX 格式解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.