0

In one of the lines in my shell script, I have following:

for i in `find ${DATA_DIR}/${KEYSPACE} -type d`

I have defined:

DATA_DIR=/var/lib/cassandra/data

KEYSPACE=test_keyspace

When I execute my shell script, it is giving following error:

find: '/var/lib/cassandra/data/test keyspace': No such file or directory

So,it is converting underscore(_) to space( ). There is a directory on my file system /var/lib/cassandra/data/test_keyspace which I want to iterate over

I also tried without using variables, without any luck:

for i in `find ${DATA_DIR}/test_keyspace -type d`
sjsam
  • 19,686
  • 3
  • 43
  • 88
Krishna Chaitanya
  • 165
  • 1
  • 1
  • 9
  • Works without problems for me – Michael O. Jun 23 '17 at 05:11
  • https://stackoverflow.com/questions/18323508/how-to-cd-into-a-directory-with-space-in-the-name Maybe this helps you. The chosen solution specified that you use `$` – Norsk Jun 23 '17 at 05:11
  • `it is converting underscore(_) to space( ).` What is converting your underscores? This assumption is not right – sjsam Jun 23 '17 at 05:12
  • http://mywiki.wooledge.org/DontReadLinesWithFor – Charles Duffy Jun 23 '17 at 05:21
  • @Norsk Based on the chosen solution, I have tried these variants, using quotes: `for i in `find ${DATA_DIR}/"${KEYSPACE}" -type d``. But it didnt work either. I also tried `for i in `find ${DATA_DIR}/"test_keyspace" -type d`` – Krishna Chaitanya Jun 23 '17 at 05:22
  • 2
    See [Using Find](http://mywiki.wooledge.org/UsingFind) for a discussion of correct ways to iterate over paths identified by `find`; `for item in $(...)` is not among them (and, for that matter, is part of [BashPitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29)). – Charles Duffy Jun 23 '17 at 05:23
  • that said, none of that relates to underscores being converted to spaces, which simply is not a thing bash *does*. Clearly there's other code at play not given in your question, and/or output or errors are being misrepresented. If you can provide a [mcve] with code someone else can copy, paste and run to reproduce the problem, that would be very helpful. – Charles Duffy Jun 23 '17 at 05:24
  • ...as an example of what kind of reproducer to aspire to: `mkdir test_directory; keyspace=test_directory; find "$PWD/$keyspace" -type d` -- if that fails in the same way, it's something someone could just copy-and-paste to see the problem themselves. – Charles Duffy Jun 23 '17 at 05:25
  • 1
    (by the way, the use of a lower-case variable name for `$keyspace` is intentional; POSIX specifies that the shell and utilities are to use all-uppercase names for their own variables, and guarantees that applications using lowercase names don't risk modifying shell or OS behavior inadvertently; see paragraph four of http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that setting a shell variable overwrites any like-named environment variable). – Charles Duffy Jun 23 '17 at 05:27
  • `find /tmp/test keyspace` will give me 2 error lines. Are you sure `/var/lib/cassandra/data/test_keyspace` exists and your monitor (or lower edge of your window) is not fading away the `_` ? – Walter A Jun 23 '17 at 22:02

0 Answers0