LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2015, 04:23 PM   #1
noahshines
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
Unhappy how to check the conditions for mount point if already mounted or unmounted?help!!


#!/bin/bash
i="/rtlogs";

mount $i 2>&1

if [ $? -eq 0 ] ; then

echo "Mounted successfully"
else
echo "Unmounted successfully"
fi
------------------------------------------------------
#!/bin/bash
i="/rtlogs"
umount $i 2>&1
if [ $? -eq 0 ] ; then
echo " $i Unmounted successfully"
else
echo "$i DID NOT UNMOUNT SUCCESSFULLY"
lsof $i
fuser $i
fi
---------------------------------------------------------------

because the script runs fine ONCE. if I re-run the script more than once...i need it tell the user that "Already Mounted " OR "Already Unmounted"

Last edited by noahshines; 10-16-2015 at 04:27 PM.
 
Old 10-16-2015, 04:43 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2.1.2 on Lenovo Thinkpad W520
Posts: 9,755

Rep: Reputation: Disabled
Before mounting or unmounting, grep the output of "cat /proc/mount" or "df -h" to see if your device or mountpoint is already mounted or in use.
 
1 members found this post helpful.
Old 10-16-2015, 04:59 PM   #3
Perceptor
Member
 
Registered: Jul 2007
Location: the Future
Distribution: Slackware
Posts: 128

Rep: Reputation: 64
You have posted two scripts.
I am not sure if this is what you want, but this is how I'd do it:

Code:
#!/bin/bash
if [[ -n `mount | grep rtlogs` ]]; then
echo "mounted"
        if [[ $1 == "act" ]]; then
                echo insert_your_umount_command_and_remove_this_echo || echo "Failed to unmount!!!"
        fi
exit 0

else
echo "not mounted"
        if [[ $1 == "act" ]]; then
                echo insert_your_mount_command_and_remove_this_echo || echo "Failed to mount!!!"
        fi

fi
If called without parameters, this script will only show you the mount status. If called with the act parameter, it would be able to (u)mount as well.

Last edited by Perceptor; 10-19-2015 at 05:17 PM. Reason: correction
 
Old 10-19-2015, 09:52 AM   #4
noahshines
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
@Perceptor Could you please explain what an "act" parameter is?
 
Old 10-19-2015, 02:33 PM   #5
Perceptor
Member
 
Registered: Jul 2007
Location: the Future
Distribution: Slackware
Posts: 128

Rep: Reputation: 64
It refers to calling the script like this:
Code:
/home/user/script act
as opposed to without arguments.
$1 refers to the first "option" of the script. In this case we're checking for a particular string (the string act is completely arbitrary).
 
Old 10-19-2015, 02:50 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,023

Rep: Reputation: 3880Reputation: 3880Reputation: 3880Reputation: 3880Reputation: 3880Reputation: 3880Reputation: 3880Reputation: 3880Reputation: 3880Reputation: 3880Reputation: 3880
FYI because the output of `mount | grep rtlogs` is a string that basically consists of "/dev/sda1 on / type ext4 (rw)" the if -e conditional does not work as expected.

As Didier Spaier posted you need to check the status prior to running the mount or unmount command. There are many ways to accomplish the task and your distribution might include the mountpoint command or maybe possible to install it from the repository.

Code:
#!/bin/bash    
if mountpoint -q "/rtlogs"; then
    echo "/rtlogs is mounted"
else
    echo "/rtlogs is not a mounted"
fi
Also as suggested to check if the rtlogs is mounted using /proc/mount you can swap the if statement above for
Code:
if grep -qs "/rtlogs" /proc/mounts; then
 
2 members found this post helpful.
Old 10-19-2015, 03:13 PM   #7
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2.1.2 on Lenovo Thinkpad W520
Posts: 9,755

Rep: Reputation: Disabled
Thanks michaelk I didn't know the mountpoint command. I confirm that it is shipped in Slackware in the util-syslinux package.

PS I realize now that I don't know what distribution the OP is running... Still "which mountpoint" or "whereis mountpoint" should tell if it's installed and where.

Last edited by Didier Spaier; 10-19-2015 at 03:17 PM. Reason: PS added.
 
Old 10-19-2015, 09:04 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,833

Rep: Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559
Thanks from me also: haven't seen 'mountpoint' before.
Just for anyone else, I can confirm it exists in Centos6 (& therefore RHEL6 I assume)
 
  


Reply

Tags
bash scripting, mount point


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a way to check mount point is mounted and is writeable matthes138 Programming 2 01-03-2012 01:56 AM
[SOLVED] proper term for an unmounted piece of hardware's sort of mount point thing? Cultist Linux - Hardware 5 10-11-2011 07:57 AM
How to prevent access to unmounted mount point? stanleycia Linux - Newbie 2 03-31-2010 02:32 AM
Mount Problem: evice already mounted or mount point busy paldebojyoti Linux - Distributions 5 08-27-2009 03:46 AM
inactive mount point can't be unmounted fractal_chaos Linux - General 4 07-01-2004 01:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration