santanudas
Technical User
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:
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!!!
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!!!