Shell Checking user privilege of multi line Variable

1

I am trying to write a script that, at some point, needs to check the user privileges of multiple folders. The paths are stored in a variable, each path in a new line.

So far I have this:

output=$PATH
output=$(echo "$output" | tr ':' '\n' | sort )                                                                                                                                                         
arr=($(echo "$output"))                                                                                                                                                                                                           
for i in "$arr"                                                                                                                                                                                                                   
do                                                                                                                                                                                                                                
    echo "$i"                                                                                                                                                                                                                     
    if ! [[ -x $i ]]; then                                                                                                                                                                                                        
        echo "You are missing execute Permission on
        echo "$i"                                                                                                                                                                                  
    fi                                                                                                                                                                                                                            
done 

The output contains several paths, in my case the content of my $PATH variable.

With the first line I try to convert the multi line variable into an array and then iterate over all array elements.

When I run the code it only works for the first entry, the echo "$i" only displays /bin before exiting the loop.

So I guess I did not convert the multi line variable to an array.

NIoSaT

Posted 2016-10-16T21:31:07.273

Reputation: 113

Ok I edited my initial post – NIoSaT – 2016-10-16T22:02:43.140

Answers

1

Replace:

    for i in "$arr"

with:

    for i in "${arr[@]}"

$arr refers to only the first element of an array. {$arr[@]} will expand to all elements, each as a separate word.

Also, if you have bash 4.0 or newer, the following can create arr in one step:

mapfile -t arr <<<"${PATH//:/$'\n'}"

John1024

Posted 2016-10-16T21:31:07.273

Reputation: 13 893

1I tried something with mapfile already but it seems mapfile is not build in on OS X 10.12 So I think other solution is more "portable" – NIoSaT – 2016-10-16T22:22:51.800

1@NIoSaT Sorry about that. mapfile does require bash 4.0 (circa 2009) or better. Mac OSX uses bash 3.2 which dates to 2006. – John1024 – 2016-10-16T22:31:31.283

yeah OS X has some outdated stuff... – NIoSaT – 2016-10-16T22:47:43.180