Help with code to check if file systems are mounted


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with code to check if file systems are mounted
# 1  
Old 08-01-2016
Help with code to check if file systems are mounted

Hi

I need to have a piece of code that check if all file systems are mounted or not.
I have to pieces of information like the output of the bdfcommand, and the file /etc/fstab.
The first is:
Code:
bdf
Filesystem          kbytes    used   avail %used Mounted on
/dev/vg00/lvol3    2097152  266656 1816272   13% /
/dev/vg00/lvol1    1835008  363248 1460336   20% /stand
/dev/vg00/lvol8    8912896 8198888  714008   92% /var
/dev/vg00/lvol7    6553600 3494248 3035568   54% /usr
/dev/vg00/lvol6    10485760 5113048 5331840   49% /tmp
/dev/vg00/lvol5    10485760 4422824 6016816   42% /opt
/dev/vg00/lvol4    2097152 2054552   42600   98% /home
/dev/vg07/lvol1    78643200   85919 73647458    0% /fs2
/dev/vg06/lvol1    78643200 8723105 65651325   12% /fs1
/dev/vg12/lvol1    727056384 396156890 310218394   56% /data9
/dev/vg11/lvol1    727056384 315960073 385402915   45% /data8
/dev/vg10/lvol1    727056384 161251871 530441835   23% /data7
/dev/vg09/lvol1    727056384 465474025 245233585   65% /data6
/dev/vg05/lvol1    524288000 300936349 209392237   59% /data5
/dev/vg04/lvol1    524288000 333342894 179013428   65% /data4
/dev/vg03/lvol1    524288000 311943041 199073488   61% /data3
/dev/vg02/lvol1    524288000 424465130 93583947   82% /data2
/dev/vg01/lvol1    524288000 377040011 138045190   73% /data1
/dev/vg08/lvol2    829489152 519891537 290247874   64% /backup

and
Code:
cat /etc/fstab
# System /etc/fstab file.  Static information about the file systems
# See fstab(4) and sam(1M) for further details on configuring devices.
/dev/vg00/lvol3 / vxfs delaylog 0 1
/dev/vg00/lvol1 /stand vxfs tranflush 0 1
/dev/vg00/lvol4 /home vxfs delaylog 0 2
/dev/vg00/lvol5 /opt vxfs delaylog 0 2
/dev/vg00/lvol6 /tmp vxfs delaylog 0 2
/dev/vg00/lvol7 /usr vxfs delaylog 0 2
/dev/vg00/lvol8 /var vxfs delaylog 0 2
#
#NetApp file systems
/dev/vg01/lvol1 /data1  vxfs    delaylog 0 2
/dev/vg02/lvol1 /data2  vxfs    delaylog 0 2
/dev/vg03/lvol1 /data3  vxfs    delaylog 0 2
/dev/vg04/lvol1 /data4  vxfs    delaylog 0 2
/dev/vg05/lvol1 /data5  vxfs    delaylog 0 2
/dev/vg06/lvol1 /fs1    vxfs    delaylog 0 2
/dev/vg07/lvol1 /fs2    vxfs    delaylog 0 2
/dev/vg08/lvol2 /backup vxfs    delaylog 0 2
/dev/vg09/lvol1 /data6  vxfs    delaylog 0 2
/dev/vg10/lvol1 /data7  vxfs    delaylog 0 2
/dev/vg11/lvol1 /data8  vxfs    delaylog 0 2
/dev/vg12/lvol1 /data9  vxfs    delaylog 0 2

I am trying the following:

Code:
for vol in /etc/fstab | egrep -v | awk '{ print $2 }'
do
if bdf    $vol | grep $vol > /dev/null
then
 outputOK "Filesystem: $vol    mounted"
 else    outputW  "Filesystem: $vol    NOT MOUNTED"
fi

But I am having the following error:

Code:
sh: Syntax error: `|' is not expected.

Could you help
# 2  
Old 08-01-2016
Code:
for vol in $(awk '! /^ *#/ && NF { print $2 }' /etc/fstab)
do
   v=$(bdf "$vol" 2>/dev/null | grep "$vol")
   if [ -n "$v" ]
   then
      echo "OK Filesystem : $vol    mounted"
   else
      echo "ERR Filesystem: $vol    NOT MOUNTED"
   fi
done

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 08-01-2016
The stuff in for vol in stuff is a list of strings. And, the 2nd string in:
Code:
/etc/fstab | egrep -v | awk '{ print $2 }'

is | which is a shell keyword that would need to be escaped if you wanted it to be treated as a string instead of a keyword. But, I would guess you wanted something more like;
Code:
for vol in $(grep -v expression /etc/fstab | awk '{ print $2 }')

where expression (which you omitted) probably should be something like '^#'.

In addition to that, your do is missing a matching done. And, we have no idea what the outputOK and outputW commands are that you are calling in your script. Both rdrtx1 and I assumed that both of those commands are just printing the strings given to them. The script rdrtx1 suggested invokes awk once and invokes both bdf and grep once for each filesystem listed in /etc/fstab. Both rdrtx1's suggestion and your code can falsely report that a filesystem is mounted if one filesystem name is a subset of another filesystem name (for example, if /data1 and /data10 are two filesystems listed in /etc/fstab and /data10 is mounted but /data1 is not, your code will incorrectly report that /data1 is mounted).

The following alternative only invokes awk once and bdf once, and will not report false positives:
Code:
#!/bin/ksh
FSTAB=/etc/fstab
bdf | awk '
FNR == NR {
	# Gather mounted filesystems.
	if(NR > 1)
		fs[$NF]
	next
}
! /^#/ {# For each filesysem in FSTAB, determine if it is mounted or not...
	if($2 in fs)
		print "outputOK Filesystem: " $2 "\tmounted"
	else	print "outputW Filessytem: " $2 "\tNOT MOUNTED"
}' - "$FSTAB"

Although written and tested with a Korn shell, this will work with any shell that accepts Bourne shell syntax.

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

With the sample data you provided, it produces the output:
Code:
outputOK Filesystem: /	mounted
outputOK Filesystem: /stand	mounted
outputOK Filesystem: /home	mounted
outputOK Filesystem: /opt	mounted
outputOK Filesystem: /tmp	mounted
outputOK Filesystem: /usr	mounted
outputOK Filesystem: /var	mounted
outputOK Filesystem: /data1	mounted
outputOK Filesystem: /data2	mounted
outputOK Filesystem: /data3	mounted
outputOK Filesystem: /data4	mounted
outputOK Filesystem: /data5	mounted
outputOK Filesystem: /fs1	mounted
outputOK Filesystem: /fs2	mounted
outputOK Filesystem: /backup	mounted
outputOK Filesystem: /data6	mounted
outputOK Filesystem: /data7	mounted
outputOK Filesystem: /data8	mounted
outputOK Filesystem: /data9	mounted

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-02-2016
Thanks a lot, I am still in the learning curve of shell scripting
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. Windows & DOS: Issues & Discussions

How to check if the folders in mounted on which partition?

Hi there, I am able to check which parition from Storage > Disk Management How is it possible to check if the folder is mounted on which partition. (1 Reply)
Discussion started by: alvinoo
1 Replies

3. Solaris

Check mounted filesystems

Hi, Please help me to tell How to check mounted filesystems for any inconsistency. Can I run fsck -m /dev/rdsk/cntndnsn for this? Thanks, (3 Replies)
Discussion started by: Manmohan Mishra
3 Replies

4. Shell Programming and Scripting

Check if NAS filesystem is mounted

Anyone know the best way to check and see if a NAS filesystem is mounted on a linux box. I have no idea where to start :wall:. (2 Replies)
Discussion started by: d3mon_spawn
2 Replies

5. 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

6. Solaris

Check systems login

Hi All, In Solaris 10, how can I check back who is login to the systems by telnet, ssh and ftp in success or failed. I already check on /var/adm/messages but no details for all this. Hope your can help. Thanks. (1 Reply)
Discussion started by: mailbox80
1 Replies

7. 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

8. Shell Programming and Scripting

check if file exists in a mounted windows shared folder

hi, I posted a thread before on that subject, but with a wrong focus... here's my problem: I want to check if a file exists in a windows shared folder mounted using: sudo mount -t cifs -o username=xxx,password=xxx,uid=xxx,gid=xxx //192.168.0.92/public /media/92_shared I tried if ... (2 Replies)
Discussion started by: jul
2 Replies

9. UNIX for Dummies Questions & Answers

Solaris10:How to check where /usr are really mounted

How can I check which partition /usr are mounted on ? Usually this is mounted on root (/). If I want to move /usr to another partition, how do I do this ? BR Ludwig (1 Reply)
Discussion started by: ludwig
1 Replies

10. Filesystems, Disks and Memory

how to assign same mount point for file systems mounted on physical disks

We have 6 hard disks attached to the hardware. Of this 2 hard disks are of 9 GB each. Now I want combine both the same in such a way that i see a combined entry in the output of df -k . The steps I follow are 1. Create partition on hard disks (Using format partition) 2. Run newfs -v for... (6 Replies)
Discussion started by: Hitesh Shah
6 Replies
Login or Register to Ask a Question