0

Here is a simple script, distilled from something actually useful, that works on fedora but not on OS X Lion.

declare -a directory_contents=($(ls .))
test -e ${directory_contents[0]}
echo $?

On linux it returns 0, i.e., test -e passes. On Mac, it returns 1.

Any idea what could be going wrong here?

6
  • The code above returns 0 on my OS X 10.6.8 ... Commented Apr 16, 2012 at 21:45
  • @NickAtoms, yes. It's good old bash 3.2.48(1)-release. Commented Apr 16, 2012 at 22:00
  • Apple still distributes bash 3.2, rather than a bash 4.x. Is that the source of your trouble? Commented Apr 16, 2012 at 22:01
  • Returns 0 for me on OS X 10.7.3 (bash 3.2.48(1)-release) unless the first filename has a space -- I think @Nick Atoms nailed it. Commented Apr 16, 2012 at 22:23
  • pduey, can you show us the list of filenames? What IS the value of ${directory_contents[0]} ? Commented Apr 17, 2012 at 1:24

3 Answers 3

4

If the first file returned by ls has spaces in the name, ${directory_contents[0]} will not expand to the full file name (only up to the first space). Does this condition apply to your OSX test and not your Fedora test?

Adding the following line before the declare statement might fix the problem:

IFS=$'\n'

reference: http://www.linuxquestions.org/questions/programming-9/bash-passing-arrays-with-spaces-611159/

Sign up to request clarification or add additional context in comments.

Comments

2

Nick's answer is correct. But you should also remember that except in highly controlled environments, ls is not something you should use to populate variables with filenames. There are a number of other ways to pick a single filename.

[ghoti@pc ~]$ ls -l foo*
-rw-r--r--  1 ghoti  wheel  0 Apr 16 21:01 foo bar.txt
-rw-r--r--  1 ghoti  wheel  0 Apr 16 21:01 foo.txt
[ghoti@pc ~]$ test1=`for i in foo*txt;do echo $i; break; done`
[ghoti@pc ~]$ echo $test1
foo bar.txt
[ghoti@pc ~]$ test2=`find . -name foo\*.txt -print | head -1`
[ghoti@pc ~]$ echo $test2
./foo.txt
[ghoti@pc ~]$ 

Obviously, not all methods will return files in the same order.

Also be mindful of filenames that begin with a hyphen. :-)

1 Comment

Upvote since your answer gets to the heart of the matter, i.e., don't rely on "ls". Although it's not the actual cause. I'm answering my own question, even though now it seems obvious, so it hopefully helps someone else.
0

What I neglected to clarify in my original question is that my "script" is really a bash function. The cause of my issue is that "ls" is resolving to an alias with "--color=auto", adding unprintable characters to the actual filename.

Always define variables to hold the full path to the executables used in a script, e.g., "LS=/bin/ls". And, per answers above, ls might not be you best choice for a robust production script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.