0

In a section of a script I am writing, I output a list of paths into a temporary text file for use later with cd. I am using sed to replace spaces in directory names with '\ ' so that I also make the script work with spaces in dir names. My command is given below: cat temp.txt | sed 's/ /\\ /g' This has the desired output of replacing spaces with '\ ' but when I try to cd later it ignores the second word and says that example\ does not exist. Any ideas how to fix this?

My whole script is on pastebin here if you want to look at it: http://pastebin.com/6jmnKDAd

This is from a backup before I added sed

Dom Brown
  • 201
  • 1
  • 2
  • 9
  • 1
    Can we see the following piece of code with cd? – Danstahr Jan 04 '13 at 15:10
  • give sample input and output from temp.txt – hovanessyan Jan 04 '13 at 15:10
  • the input to temp.txt is from a recursive function that gets all the directories and subdirectories in a path. It would output each path on a new line. e.g. ~ [newline] ~/work and so on. I then perform the sed I gave above and store it in a variable called directories. I then use another function with a for loop to iterate over all paths. It looks something like `for a in $directories; do cd $a` – Dom Brown Jan 04 '13 at 15:21
  • I also tried `cd "$a"` but that didn't fix it either – Dom Brown Jan 04 '13 at 15:22
  • 3
    This smells like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is your **real** problem? – glenn jackman Jan 04 '13 at 15:24
  • Use `sed 's/ /\\ /g' temp.txt`, there is no need for cat here. – Micha Wiedenmann Jan 04 '13 at 15:28
  • Basically I'm writing a script to get files matching a regex from all the subdirectories of a specified path. If a 3rd arg is specified, it searches within the files for that word and prints the line it is found on. It works perfectly and only breaks when a directory name has a space in it :S The command is as follows `./script.sh pathHere regexHere optionalSearchHere` – Dom Brown Jan 04 '13 at 15:28
  • Ive added a pastebin link to my code – Dom Brown Jan 04 '13 at 15:45
  • Rather than trying to solve a problem that your code is creating, it's best to just not create the problem. See @GlennJackman's answer. – Ed Morton Jan 04 '13 at 20:41

4 Answers4

3

Try enclosing your variable within " " without escaping blanks.

cd "$mydir"
Lorenzo L
  • 201
  • 1
  • 4
3

To complement Lorenzo's answer, if you have a bunch of newline-separated pathnames in a file:

while IFS= read -r path; do
    cd "$path"
    do stuff
done < temp.txt

It is critical to quote "$path" to prevent the shell's word splitting

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I've modified my code to use this, but `cd` still gives me errors for names with spaces :/ it outputs something along the lines of "/pathHere/space test no such file or directory" though if i manually cd to the given path outside the script it works fine. any ideas? – Dom Brown Jan 05 '13 at 12:33
  • Perhaps your text file has DOS line endings, so the $path variable ends with a carriage return. Try using `dos2unix` on your text file. – glenn jackman Jan 05 '13 at 12:46
  • Ok Glenn, I've found that it works if I specify an absolute path i.e. it's fine with ~/path/..... but relative paths seem to be a no go e.g ./path doesnt work – Dom Brown Jan 05 '13 at 12:51
  • In the context of your script, what is the `pwd`? – glenn jackman Jan 05 '13 at 12:57
  • The pwd was just a random folder I had it in, I fixed it by changing back to the original path after each iteration of the while loop – Dom Brown Jan 05 '13 at 13:02
  • Yes, I had the original search path stored in a variable called `startDir` so at the end of the loop I had `cd "$startDir"` and now it works fine :) – Dom Brown Jan 05 '13 at 14:26
0

You need to do this instead of cat temp.txt | sed 's/ /\\ /g'

perl -F"/" -ane 'foreach (@F){if(/ /){$_="\"".$_."\"";}}print join "/",@F;' temp.txt

also check here

Vijay
  • 65,327
  • 90
  • 227
  • 319
0

Is there a need to use a temporary file or could you store the directories in an array:

dirs=()
dirs+=("/tmp/file A.txt")
dirs+=("/tmp/file B.txt")

for d in "${dirs[@]}"; do
  cd "$d"
done

Resources

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137