2016-11-03 83 views
0

部分我的bash脚本的是访问一系列的文件夹:当我收到错误bash脚本CD不起作用

#lsit of folders 
locations=("/Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314a" 
    "/Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314b" 
    "/Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314c") 

for i in "${locations[@]}" 
do (
    #change to directory 

    cd "$i" 
    #convert tiff to png 

但是:

/Users/luna/Documents/Ethan/scripts/microglia.sh: line 16: cd: /Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314a/: No such file or directory 

我一直在努力,只是CD进入该终端上的文件夹,它绝对有效。它怎么会不会在shell脚本中工作?

+1

这是一个想法。你应该在你的终端上试试它。这两个将工作:'CD /卷/以色列\埃尔南德斯','CD“/卷/以色列埃尔南德斯”'。但是这个不起作用:'cd'/ Volumes/Israel \ Hernandez''。将目录分配到数组位置中的双引号内的反斜杠是罪魁祸首。 – alvits

回答

1

由于空格已经在双引号字符串中,因此您不需要在空格之前进行反斜杠转义。你的报价是正确的—欢呼!删除""字符串中的反斜杠,您应该设置。

0

错误很明显:“没有这样的文件或目录!” 您可以在终端上执行命令“cd/Volumes/Israel \ Hernandez/Quantitative \ Data/Microglia \ data/3 \ month/Mutant/314a /”。
如果错误是“没有这样的文件或目录!”,你应该仔细检查路径。 不要怀疑脚本解析器本身!只是怀疑你的代码!

+0

“line 16:cd:/ Volumes/Israel \ Hernandez/Quantitative \ Data/Microglia \ data/3 \ month/Mutant/314a /” –

+0

“line 16:cd:/ Volumes/Israel \ Hernandez/Quantitative \ Data/Microglia \ data/3 \ month/Mutant/314a /“仔细检查此路径的错误!复制终端上的错误路径 –

0

在您执行cd命令之前,您应该在您$ i中处理转义空间。 这里是代码,希望它有帮助。

#lsit of folders 
locations=("/Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314a" 
    "/Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314b" 
    "/Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314c") 

for i in "${locations[@]}" 
do (

    #reverse esxape chars such space in you code 
    i="echo \$\'"$(echo $i|sed -e 's|\\|\\\\|g')"\'" 
    i=$(eval "echo $(eval $i)") 
    i=$(eval "echo $(eval $i)") 

    #change to directory 
    cd "$i" 
    #convert tiff to png 
+0

看起来它会起作用(我自己没有尝试过)。应[非常小心'eval'以避免引入安全漏洞或其他问题。](http://stackoverflow.com/q/17529220/2877364) – cxw