bash script to get all mounted devices


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script to get all mounted devices
# 1  
Old 08-05-2009
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.

Code:
#!/bin/bash

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

Output of script example:

Code:
/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).
# 2  
Old 08-05-2009
Something like:

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

# 3  
Old 08-05-2009
Quote:
Originally Posted by peterro
Something like:

Code:
#!/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
# 4  
Old 08-05-2009
A bit different then..

Code:
#!/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.
# 5  
Old 08-07-2009
Quote:
Originally Posted by peterro
A bit different then..

Code:
#!/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.
# 6  
Old 08-08-2009
You can avoid sudo with this.
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check if file systems are mounted

Hi I have the following piece of code, running on a solaris 10 O.S., that is not working for NFS file systems: for vol in `grep -E 'vxfs|ufs|nfs' /etc/vfstab | egrep -v '^#' | awk '{ print $3 }'` do if df -k $vol | grep $vol > /dev/null then outputOK "Filesystem: $vol mounted" else... (1 Reply)
Discussion started by: fretagi
1 Replies

2. How to Post in the The UNIX and Linux Forums

NFS mounted and unmounted shell script

Hi, I making a script to check nfs mount and unmount options. After various findings, i didn't get any solution for that. Can you please help me in making the script. 1) I have used, if grep -qs '/var/JETSHARE' /proc/mounts; then echo "It's mounted." else echo "It's not mounted. ... (2 Replies)
Discussion started by: Santosh101
2 Replies

3. Shell Programming and Scripting

sample code to display mounted devices

Hi all I need to write shell script to list out the mounted devices for a particular user. As i am new to shell script please help me. Here the problem is sometime unmounted devices also will be displayed in fstab. How to rectify that? Can anybody help me? Regards Ilamathi (0 Replies)
Discussion started by: ilamathi
0 Replies

4. Shell Programming and Scripting

How to check if a partition is mounted or not with bash?

How to check if a partition is mounted or not with bash? And when is $? variable one? Please give example. (10 Replies)
Discussion started by: cola
10 Replies

5. Shell Programming and Scripting

1 usb stick -> 2 mounted devices

Hello, i am using a solaris thinclient that tries to connecting to a terminalserver. (RDP) Everything works fine, but the usb redirection. If i put in a usb stick i always get 2 usb-drives mounted. If i look in /tmp/SUNWut/mnt/<name of the host> i see 2 devices. One with the name of the... (2 Replies)
Discussion started by: anarcy
2 Replies

6. Shell Programming and Scripting

bash script to check if mounted, and mount if not

I'd like to make a wrapper bash script that will make sure that an nfs mount is mounted before launching a program that depends on the mount being active. Basically: 1) Check to see if the mount is active 2) If it's not active, try to mount it 3) If it won't mount because the nfs server is... (3 Replies)
Discussion started by: graysky
3 Replies

7. Shell Programming and Scripting

How to display partition name on mounted on using this script

Hi All Guys.... root> df -k|grep /u0 /dev/vx/dsk/oradg/u02vol 12582912 8717924 3744252 70% /u02 /dev/vx/dsk/oradg/u01vol 8796160 5563610 3131556 64% /u01 /dev/vx/dsk/oradg/u04vol 10035200 1247888 8519534 13% /u04 /dev/vx/dsk/oradg/u03vol 12582912 2524060 9744542 21% ... (3 Replies)
Discussion started by: adzuanamir
3 Replies

8. UNIX for Dummies Questions & Answers

passthrough devices vs. named devices

I am having trouble understanding the difference between a passthrough device and a named device and when you would use one or the other to access equipment. As an example, we have a tape library and giving the command "camcontrol devlist" gives the following output: akx# camcontrol... (1 Reply)
Discussion started by: thumper
1 Replies

9. Shell Programming and Scripting

disableing my USB devices using a script

hi guys I would like to disable my USB devices using a shell script(Linux machine) i.e(hoteplug,kernelpcmciacs,pcmciacs) Kindly suggest me at the earliest Thnks in advance regards ash (0 Replies)
Discussion started by: whizkidash
0 Replies
Login or Register to Ask a Question