×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Array with "long file name".

Array with "long file name".

Array with "long file name".

(OP)
Hi Guys,

I was trying to use the output of "ls" command to fill an array with. This is the sample code I'm using:

CODE

#!/bin/bash
#
cd /Volumes
PART=( `ls` )

echo; echo "Directory listing:"
echo "------------------"; ls

echo; echo "The array using ls:"
echo "----------------------"
ix=0
  while [ ${ix} -lt ${#PART[@]} ]; do
  echo "($[${ix}+1])  ${PART[${ix}]}"
   let ix=ix+1
done
echo

You know what I mean – the windows type file names with space(s) in it. Say e.g. If the file name is "xyz mn", the array gets two elements - "xyz" and "mn" instead of one. I tried using a '\' for each space and also tried "double quote" but didn't work. This is what I get if I run the script:


Directory listing:
------------------
Documents       OSX             Programs        Projects HD

The array using ls:
----------------------
(1)  Documents
(2)  OSX
(3)  Programs
(4)  Projects
(5)  HD


"ls" showing the correct output at the top but in stead of 4, the array is being made of 5 elements i.e. treating "Projects" and "HD" separately. Can any one please help solving this? I thank all of you in advance. cheers!!!


RE: Array with "long file name".

(OP)

stefanwagner, thanks for replying. But sadly, it doesn't work either. Now the output becomes rather funny.  

Directory listing:
------------------
"Documents"       "OSX"             "Programs"        "Projects HD"

The array using ls:
----------------------
(1)  "Documents"
(2)  "OSX"
(3)  "Programs"
(4)  "Projects
(5)  HD"

Another thing is that -Q option is not available in everywhere, e.g. BSD/OS X to name one.  I don't think it's that hard to make it work. I do believe that anyone of you must me knowing thw wrong thing I'm doing here. Cheers!!!

RE: Array with "long file name".

(OP)

Quote:

Do you really need arrays?
Unfortunately yes. This is something I've to end up with in the end:

CODE

#!/bin/bash
#
cd /Volumes
# We just need the directories *NOT* the files.
PART=( `ls -d */ | sed 's/\///g'` );

echo; echo "Select one from the list:"
echo "----------------------"
ix=0
  while [ ${ix} -lt ${#PART[@]} ]; do
  echo "($[${ix}+1])  ${PART[${ix}]}"
  let ix=ix+1
done

echo "----------------------------"
echo -n "Select ( `jot -s " / " ${#PART[@]}` ): "
read -n 1 NEW_LOC; echo ""; echo ""
echo
and as you see, it will give you 5 options, in stead of 4 (in this case) to choose one from, which is totally wrong. There are several other things also work with a blank/white space in the file name but I'm stuck putting then on the list properly.

RE: Array with "long file name".

You could temporarily replace the embedded spaces with some other character that you know will not be used in filenames. For example, the following will change spaces to ~ characters:

PART=$(ls -1 | sed 's/ /~/g')

Of course, you will have to change the ~s back to spaces before processing each individual filenames.

Alternatively, you could just use a different scripting language, like Perl.

RE: Array with "long file name".

(OP)

Quote:

You could temporarily replace the embedded spaces with some other character that you know will not be used in filenames.
That idea seems to be working. Though, I didn't achieve the thing I wanted to but I guess, now I know what to do. Many thanks!!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close