Bash脚本cd不起作用

时间:2016-11-03 01:29:03

标签: bash shell

我的bash脚本的一部分是访问一系列文件夹:

<fieldset class="range__field">
  <input class="range" type="range" min="0" max="10">
  <div class="timeline-wrapper">
    <svg role="presentation" width="100%" height="10" xmlns="http://www.w3.org/2000/svg">
      <rect class="range__tick" x="0%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="10%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="20%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="30%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="40%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="50%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="60%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="70%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="80%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="90%" y="3" width="1" height="10"></rect>
      <rect class="range__tick" x="100%" y="3" width="1" height="10"></rect>
    </svg>
    <svg role="presentation" width="100%" height="14" xmlns="http://www.w3.org/2000/svg">
      <text class="range__point" x="0%" y="14" text-anchor="start">0</text>
      <text class="range__point" x="10%" y="14" text-anchor="middle">1</text>
      <text class="range__point" x="20%" y="14" text-anchor="middle">2</text>
      <text class="range__point" x="30%" y="14" text-anchor="middle">3</text>
      <text class="range__point" x="40%" y="14" text-anchor="middle">4</text>
      <text class="range__point" x="50%" y="14" text-anchor="middle">5</text>
      <text class="range__point" x="60%" y="14" text-anchor="middle">6</text>
      <text class="range__point" x="70%" y="14" text-anchor="middle">7</text>
      <text class="range__point" x="80%" y="14" text-anchor="middle">8</text>
      <text class="range__point" x="90%" y="14" text-anchor="middle">9</text>
      <text class="range__point" x="100%" y="14" text-anchor="end">10</text>
    </svg>
  </div>
</fieldset>

但是当我收到错误时:

 #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

我试图在终端上刻录到该文件夹​​,它绝对有效。为什么它不会在shell脚本中工作?

3 个答案:

答案 0 :(得分:1)

您不需要在空格之前使用反斜杠转义符,因为空格已经在双引号字符串中。你的报价是正确的 - 高兴!删除""字符串中的反斜杠,您应该设置。

答案 1 :(得分:0)

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

答案 2 :(得分: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