0

I'm trying to make the following script loop over the part_arr array elements and depending on the value the script should execute the commands. In this case I just used a echo statement. It doesn't seem to detect the paths. part_arr is the size and path of each partition starting with a / or [.

part_arr=$(lsblk -b | awk '/.*[\/\[].*/{ print $4, $7 }')

tmp=''

for i in "${part_arr[@]}"; do

    case "$i" in
    "/boot/efi" )
        echo $i     $tmp;;
    "/boot" )
        echo $i     $tmp;;
    "/var")
        echo $1     $tmp;;
    "/tmp")
        echo $1     $tmp;;
    "/home")
        echo $1     $tmp;;
    "[SWAP]")
        echo $1     $tmp;;
    "/")
        echo $1     $tmp;;
    esac

    tmp=i

done
Jacques MALAPRADE
  • 963
  • 4
  • 13
  • 28

1 Answers1

2

Here is an alternative approach that does away with the case statement by using an array that can easily be edited. It might make maintenance slightly easier.

#!/bin/bash
part_arr=($(lsblk -b | awk '/.*[\/\[].*/{ print $4, $7 }'))
interesting=(
    '/'
    '/boot'
    '/boot/efi'
    '/etc/hosts'
    '/home'
    '/media/ebs0'
    '/tmp'
    '/var'
    '[SWAP]'
)
# Each entry is pair: (size, file-name).
for((i=0; i<${#part_arr[@]}; i+=2)) ; do
    fname=${part_arr[(($i+1))]}
    # O(log(N)) search is okay here because N is small.
    for j in ${interesting[@]} ; do
        if [[ "$j" == "$fname" ]] ; then
            fsize=${part_arr[$i]}
            printf '%-12s %s\n' $fname $fsize
            break
        fi
    done
done
Joe Linoff
  • 761
  • 9
  • 13