bash script to get all mounted devices

Hi, to all

I'm writing script with zenity to benchmark selected disk with tools like; hdparm, seeker (to found in here > How fast is your disk? | LinuxInsight)

With this piece of code i try to get all mounted devices to variables to use it with selection menu, but i stuck and don't know how send listed devices to variables. Of course script must be flexible and react with hot plugged disks.

#!/bin/bash

gksudo 'fdisk -l' | sed -n '/^[/]/p' | awk '{print $1}';

Output of script example:

/dev/sda1
/dev/sda2
/dev/sda5

Thanks in advance for any help or suggestions.

Sorry for my English (not native language) and code (first script).

Something like:

#!/bin/bash
for device in `gksudo 'fdisk -l' | sed -n '/^[/]/p' | awk '{print $1}'`
do
  echo $device
done

Not exactly, your code assign whole $1 to $device, that don't change any thing. What i try to get is divide $1 column to separated lines and assign each line to variable which be the separated partition to chose from menu.

Thanks anyway for suggestion

A bit different then..

#!/bin/bash
count=0
for device in `gksudo 'fdisk -l' | sed -n '/^[/]/p' | awk '{print $1}'`
do
  count=$((count+1))
  dev[$count]=$device
done

echo ${dev[7]}
echo $count

The last two echo's are just to show output.

Thanks, that is good point of start for me.

You can avoid sudo with this.

for device in $(sed -n '/sd[ab][1-9]/ s,.* ,/dev/,p' /proc/partitions)
do
...

Notice some things:

  • The file /proc/partitions probably has the info you want.
  • The syntax $(...) is equivalent to backticks, but easier to read.
  • The sed expression s,.* ,/dev/, uses commas in place of slashes which works just fine.