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
2
  • part_arr isn't array, it's a single string. Are you missing () around your command substitution? Commented Aug 22, 2018 at 15:15
  • Thanks @BenjaminW. I needed a the following: part_arr=($(lsblk -b | awk '/.*[\/\[].*/{ print $4, $7 }')). You can provide the solution below. Commented Aug 22, 2018 at 15:25

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.