0

Part of my bash script is to access a series of folders:

 #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

However when I received errors:

/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

I've tried to just cd into that folder on terminal and it absolutely worked. How come it just wont work in a shell script?

  • 1
    Here's an idea. You should try it on your terminal. These 2 will work: `cd /Volumes/Israel\ Hernandez`, `cd "/Volumes/Israel Hernandez"`. But this one will not work: `cd "/Volumes/Israel\ Hernandez"`. The backslash inside double quotes in the assignment of the directories into array `locations` is the culprit. – alvits Nov 03 '16 at 01:38

3 Answers3

1

You don't need the backslash escapes before the spaces since the spaces are already inside a double-quoted string. Your quoting is correct — rejoice! Remove the backslashes inside your "" strings and you should be set.

cxw
  • 16,685
  • 2
  • 45
  • 81
0

The error is clearly:"No Such file or directory!" You can executive the command "cd /Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314a/" on the terminal.
if the error is "No such file or directory!",you should carefully check the path. Don't doubt the script parser itself!Just doubt your code!

李德康
  • 15
  • 6
  • “line 16: cd: /Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314a/" – 李德康 Nov 03 '16 at 01:44
  • “line 16: cd: /Volumes/Israel\ Hernandez/Quantitative\ Data/Microglia\ data/3\ month/Mutant/314a/” carefully check the error of this path! Copy the erroried path on your terminal. – 李德康 Nov 03 '16 at 01:47
0

before you cd command, you should process the escape space in you $i. here is the code, hope it helps.

#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
LF00
  • 27,015
  • 29
  • 156
  • 295
  • That looks like it will work (haven't tried it myself). I do want to mention you should [be very careful with `eval` to avoid introducing security holes or other problems.](http://stackoverflow.com/q/17529220/2877364) – cxw Nov 03 '16 at 02:02