Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array with "long file name".

Status
Not open for further replies.

santanudas

Technical User
Mar 18, 2002
121
GB
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!!!


 

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!!!
 
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.

 
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:
[tt]
PART=$(ls -1 | sed 's/ /~/g')
[/tt]
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.
 
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!!!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top