elranu
elranu

Reputation: 2312

Run a bash command and split the output by line not by space

I want to run the following command and get the output in the variable splited in an array by line not by space:

files=$( hdfs dfs -ls -R $hdfsDir)

So the output I get is the following: echo $files

drwxr-xr-x - pepeuser supergroup 0 2016-05-27 15:03 /user/some/kpi/2015/01/02 -rw-r--r-- 3 pepeuser supergroup 55107934 2016-05-27 15:02 /user/some/kpi/2015/01/02/part-00000902148 -rw-r--r-- 3 pepeuser supergroup 49225279 2016-05-27 15:02 /user/some/kpi/2015/01/02/part-00001902148

When I do a for in $files in stead of getting the full line on each., I get the column in stead of the line. It prints like the following:

drwxr-xr-x
-
pepeuser
supergroup

and what I need on the for to print like this:

drwxr-xr-x - pepeuser supergroup 0 2016-05-27 15:03 /user/some/kpi/2015/01/02 
-rw-r--r-- 3 pepeuser supergroup 55107934 2016-05-27 15:02 /user/some/kpi/2015/01/02/part-00000902148 
-rw-r--r-- 3 pepeuser supergroup 49225279 2016-05-27 15:02 /user/some/kpi/2015/01/02/part-00001902148

Upvotes: 1

Views: 71

Answers (1)

John Kugelman
John Kugelman

Reputation: 361565

If you have bash 4, you can use readarray:

readarray -t files < <(hdfs dfs -ls -R "$hdfsDir")

Otherwise, use read -a to read into an array. IFS=$'\n' sets the field separator to newlines and -d '' tells it to keep reading until it hits a NUL character: effectively, that means it'll read to EOF.

IFS=$'\n' read -d '' -r -a files < <(hdfs dfs -ls -R "$hdfsDir")

You can verify that the array is populated correctly with something like:

printf '[%s]\n' "${files[@]}"

And can loop over the array with:

for file in "${files[@]}"; do
    echo "$file"
done

Upvotes: 3

Related Questions