Populating a BASH array with a list of files including spaces-in-the-name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Populating a BASH array with a list of files including spaces-in-the-name
# 1  
Old 04-18-2014
Populating a BASH array with a list of files including spaces-in-the-name

For the record, I already tried telling mgmt and the users to disallow spaces in filenames for this script, but it isn't happening for a number of ID10T-error-based reasons.

I have simple list of 3 files in a directory that are named like this:
Code:
bash-3.2$ ls -1 file*
file1
file1 part2
file1 part3

I tried this cmd to populate the array:
Code:
fileNameArray=( `ls -1 file*` )

However, I get the following results with 5 elements when I try this for-loop,
Code:
for x in ${fileNameArray[@]}
> do
> echo $x
> done
file1
file1
part2
file1
part3


So is there a way I can populate the array so the for-loop gives me 3 elements like this:
Code:
file1
file1 part2
file1 part3


Last edited by Don Cragun; 04-18-2014 at 05:06 PM.. Reason: Change INDENT tags to CODE tags.
# 2  
Old 04-18-2014
Try manipulating the IFS variable:-
Code:
# After the shebang line add something like...
save_ifs="$IFS"
# Just a newline.
IFS="
"
#
# All of your code in here...
#
# 
IFS="$save_ifs"
# exit # IF required with or without a code number...

EDIT:
Not saved IFS here for this demo...
Code:
Last login: Fri Apr 18 21:43:23 on ttys000
AMIGA:barrywalker~> echo "Barry Walker." > /tmp/"file 1" 
AMIGA:barrywalker~> echo "Amateur Radio. " > /tmp/"file 2" 
AMIGA:barrywalker~> echo "G0LCU. " > /tmp/"file 3" 
AMIGA:barrywalker~> IFS="
> "
AMIGA:barrywalker~> fileNameArray=( $(ls /tmp/file*) )
AMIGA:barrywalker~> for x in ${fileNameArray[@]}; do echo "$x"; done
/tmp/file 1
/tmp/file 2
/tmp/file 3
AMIGA:barrywalker~> _


Last edited by wisecracker; 04-18-2014 at 05:48 PM.. Reason: Longhand dem0...
# 3  
Old 04-18-2014
You could also try:
Code:
fileNameArray=( file* )
for x in "${fileNameArray[@]}"
do	printf "%s\n" "$x"
done

PS Note that this will work even in the (absolutely horrendous) case where a filename contains a <newline> character.

Last edited by Don Cragun; 04-18-2014 at 05:24 PM.. Reason: Add PS.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

AWK print last field including SPACES

I have simple test.sh script, see below: bill_code=`echo $record | awk -F"|" '{print $1}'` Fullname=`echo $record | awk -F"|" '{print $3}'` email=`echo $record | awk -F\ '{print $4}'` The last field contains spaces: see csv below: A0222|Y|DELACRUZ|-cc dell@yahoo.com-cc support@yahoo.com ... (9 Replies)
Discussion started by: quay
9 Replies

2. UNIX for Dummies Questions & Answers

List of files with spaces

Hi, Anyone can help me on how to list the file with spaces? Like I want to "ls" only the 2008 files. 2008 _overview102.jpg 2008 _overview103.jpg 2008 _overview106.jpg 2008 _overview677.jpg 2008 _overview680.jpg 2008 _overview110.jpg 2008 _overview682.jpg 2009 _overview4373.jpg 2009... (1 Reply)
Discussion started by: fspalero
1 Replies

3. Shell Programming and Scripting

awk - Pre-populating an array from system command output

So, here's a scenario that requires the same logic as what I'm working on: Suppose that you have a directory containing files named after users. For awk's purposes, the filename is a single field-- something parse-friendly, like john_smith. Now, let's say that I'd like to populate an array in... (2 Replies)
Discussion started by: treesloth
2 Replies

4. Shell Programming and Scripting

bash replace spaces in list.txt with \

I'm trying to run a Linux virus scan on a list of files/folders I have ported to list.txt in a format: some file with spaces some other file but I need to feed my scanning script in the format: some\ file\ with\ spaces/ some\ other\ file/ so I would like to read in list.txt and output... (6 Replies)
Discussion started by: unclecameron
6 Replies

5. Shell Programming and Scripting

Populating an Array

Guys, I need to iterate populate an array while going over files in directory. Can someone please tell me syntax I tried this but it isn't working ==> for F in `ls -p "${directory1}" | grep -v "\/"` do cd "${directory2}" cmp "${directory2}"/"${F}" "${directory1}"/"${F}" ... (2 Replies)
Discussion started by: Veenak15
2 Replies

6. Shell Programming and Scripting

Populating array raised an error

Hi, The following test case populate an array named: array3. Since array1 and array2 are equal in length and values array3 will remain empty. #!/usr/bin/ksh test() { set -A array1 "A" set -A array2 "A" NUM_1=`echo ${#array1}` print "num elenemt in NUM_1 is ${NUM_1}" i=1 for ELE2 in... (1 Reply)
Discussion started by: yoavbe
1 Replies

7. Shell Programming and Scripting

populating array using awk

Hi. I have a file with the following structer: DB DISK LOCATION SIZE ============================================ PROD DATA_01 /dev/dm-23 10 PROD DATA_02 /dev/dm-24 10 PROD DATA_03 /dev/dm-25 10 DEV DATA_04 /dev/dm-26 10 DEV DATA_05 ... (1 Reply)
Discussion started by: yoavbe
1 Replies

8. UNIX for Dummies Questions & Answers

Reading a line including spaces

Hi All, I have a script that reads a file and echo it back to std out. Test.txt 1aaaaaaaaaaa . The script is ReadLine.sh #!/bin/ksh cat $1 | while read file do echo $file done I invoke the script as ReadLine.sh Test.txt The output that I get is (1 Reply)
Discussion started by: aksarben
1 Replies

9. Shell Programming and Scripting

including spaces in awk output

I need to tweek my awk output: #cat filename ab cd ef:ghi:jk lm:nop qrs #cat filename | awk '{ for(i=3;i<NF+1;i++) printf $i}' ef:ghi:jklm:nopqrs I would like the ouput to include the original spaces from columns 3 on: ef:ghi:jk lm:nop qrs any suggestions? (4 Replies)
Discussion started by: prkfriryce
4 Replies

10. Shell Programming and Scripting

Read files including spaces

I am accessing two files. I am using read command to read from the files. For the first file, I need read the fields delimited by spaces, and for the other file, I need to read the whole line as a single field including the spaces. When I used read command for the second file, the spaces... (4 Replies)
Discussion started by: kumariak
4 Replies
Login or Register to Ask a Question