Bash 目录名crashing.sh中的空格-use";usebackq";?
我正在尝试运行以下脚本,但是当它尝试运行mkdir$dir1/$str2时,会创建: /d/Dropbox/PhD/实验性 并返回错误: 创建目录“/d/Dropbox/PhD/Experimental”:文件存在 创建目录“Design/APS/Processed_和_Graphed/InvariantQ”:没有这样的文件或目录 我尝试过用双引号或“实验设计”中空格前的“\”来编码目录名,但这两种方法似乎都不起作用。。。这似乎可以通过使用“usebackq”在批处理文件中实现——在GitBash for windows中有没有办法做到这一点?如果是,在我的代码中,它将应用于哪里Bash 目录名crashing.sh中的空格-use";usebackq";?,bash,unix,directory,space,git-bash,Bash,Unix,Directory,Space,Git Bash,我正在尝试运行以下脚本,但是当它尝试运行mkdir$dir1/$str2时,会创建: /d/Dropbox/PhD/实验性 并返回错误: 创建目录“/d/Dropbox/PhD/Experimental”:文件存在 创建目录“Design/APS/Processed_和_Graphed/InvariantQ”:没有这样的文件或目录 我尝试过用双引号或“实验设计”中空格前的“\”来编码目录名,但这两种方法似乎都不起作用。。。这似乎可以通过使用“usebackq”在批处理文件中实现——在GitBas
另外,是否有人知道为什么在这里使用“[[”测试语句有效,而单个“[”测试无效?引用您的变量,以防止在扩展上分词
dir1='/d/Dropbox/PhD/Experimental Design/APS/Processed_and_Graphed/InvariantQ'
echo $dir1
for f in A*.xlsx
do
str2=${f%?????}
if [[ ! -d $dir1/$str2 ]]; then
mkdir $dir1/$str2
else
echo "Directory" $dir1/$str2 "already exists, directory not created"
fi
if [[ ! -f $dir1/$str2/$f ]]; then
mv -v $f $dir1/$str2
else
echo "File" $dir1/$str2/$f "already exists, file not copied"
fi
done
它与
[[
一起工作,因为这是shell语法,而不是普通命令。它专门识别变量,不会对变量进行功拆分。这与它允许您使用类似的运算符的原因相同,请参见Awesome-感谢BroSlow!:D
dir1='/d/Dropbox/PhD/Experimental Design/APS/Processed_and_Graphed/InvariantQ'
echo "$dir1"
for f in A*.xlsx
do
str2=${f%?????}
if [[ ! -d $dir1/$str2 ]]; then
mkdir "$dir1/$str2"
else
echo "Directory $dir1/$str2 already exists, directory not created"
fi
if [[ ! -f $dir1/$str2/$f ]]; then
mv -v "$f" "$dir1/$str2"
else
echo "File $dir1/$str2/$f already exists, file not copied"
fi
done