How to check if a filesystem is mounted with a script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
16
down vote

favorite
6












I am new at scripting ... I can do very basic stuff, but now I need a hand.



I have a local filesystem that only will be mounted when I need to do a backup.



I'm starting with this.



#!/bin/bash
export MOUNT=/myfilesystem

if grep -qs $MOUNT /proc/mounts; then
echo "It's mounted."
else
echo "It's not mounted."; then
mount $MOUNT;
fi


As I said, I'm very basic at scripting. I heard that you can check the status of the mount command by looking at the return codes.



RETURN CODES
mount has the following return codes (the bits can be ORed):
0 success
1 incorrect invocation or permissions
2 system error (out of memory, cannot fork, no more loop devices)
4 internal mount bug
8 user interrupt
16 problems writing or locking /etc/mtab
32 mount failure
64 some mount succeeded


I don't know how to check that. Any guidance?










share|improve this question



















  • 2




    You have a funny dangling ; then in your script.
    – Mat
    May 17 '12 at 16:18










  • why are we exporting the MOUNT var and also remove the ";"
    – Mike Q
    Nov 23 '16 at 21:38














up vote
16
down vote

favorite
6












I am new at scripting ... I can do very basic stuff, but now I need a hand.



I have a local filesystem that only will be mounted when I need to do a backup.



I'm starting with this.



#!/bin/bash
export MOUNT=/myfilesystem

if grep -qs $MOUNT /proc/mounts; then
echo "It's mounted."
else
echo "It's not mounted."; then
mount $MOUNT;
fi


As I said, I'm very basic at scripting. I heard that you can check the status of the mount command by looking at the return codes.



RETURN CODES
mount has the following return codes (the bits can be ORed):
0 success
1 incorrect invocation or permissions
2 system error (out of memory, cannot fork, no more loop devices)
4 internal mount bug
8 user interrupt
16 problems writing or locking /etc/mtab
32 mount failure
64 some mount succeeded


I don't know how to check that. Any guidance?










share|improve this question



















  • 2




    You have a funny dangling ; then in your script.
    – Mat
    May 17 '12 at 16:18










  • why are we exporting the MOUNT var and also remove the ";"
    – Mike Q
    Nov 23 '16 at 21:38












up vote
16
down vote

favorite
6









up vote
16
down vote

favorite
6






6





I am new at scripting ... I can do very basic stuff, but now I need a hand.



I have a local filesystem that only will be mounted when I need to do a backup.



I'm starting with this.



#!/bin/bash
export MOUNT=/myfilesystem

if grep -qs $MOUNT /proc/mounts; then
echo "It's mounted."
else
echo "It's not mounted."; then
mount $MOUNT;
fi


As I said, I'm very basic at scripting. I heard that you can check the status of the mount command by looking at the return codes.



RETURN CODES
mount has the following return codes (the bits can be ORed):
0 success
1 incorrect invocation or permissions
2 system error (out of memory, cannot fork, no more loop devices)
4 internal mount bug
8 user interrupt
16 problems writing or locking /etc/mtab
32 mount failure
64 some mount succeeded


I don't know how to check that. Any guidance?










share|improve this question















I am new at scripting ... I can do very basic stuff, but now I need a hand.



I have a local filesystem that only will be mounted when I need to do a backup.



I'm starting with this.



#!/bin/bash
export MOUNT=/myfilesystem

if grep -qs $MOUNT /proc/mounts; then
echo "It's mounted."
else
echo "It's not mounted."; then
mount $MOUNT;
fi


As I said, I'm very basic at scripting. I heard that you can check the status of the mount command by looking at the return codes.



RETURN CODES
mount has the following return codes (the bits can be ORed):
0 success
1 incorrect invocation or permissions
2 system error (out of memory, cannot fork, no more loop devices)
4 internal mount bug
8 user interrupt
16 problems writing or locking /etc/mtab
32 mount failure
64 some mount succeeded


I don't know how to check that. Any guidance?







bash shell-script mount






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 17 '12 at 16:18









Mat

38.7k8117125




38.7k8117125










asked May 17 '12 at 15:51









maniat1k

83231833




83231833







  • 2




    You have a funny dangling ; then in your script.
    – Mat
    May 17 '12 at 16:18










  • why are we exporting the MOUNT var and also remove the ";"
    – Mike Q
    Nov 23 '16 at 21:38












  • 2




    You have a funny dangling ; then in your script.
    – Mat
    May 17 '12 at 16:18










  • why are we exporting the MOUNT var and also remove the ";"
    – Mike Q
    Nov 23 '16 at 21:38







2




2




You have a funny dangling ; then in your script.
– Mat
May 17 '12 at 16:18




You have a funny dangling ; then in your script.
– Mat
May 17 '12 at 16:18












why are we exporting the MOUNT var and also remove the ";"
– Mike Q
Nov 23 '16 at 21:38




why are we exporting the MOUNT var and also remove the ";"
– Mike Q
Nov 23 '16 at 21:38










5 Answers
5






active

oldest

votes

















up vote
14
down vote



accepted










You can check the status code of mount, and most well written executables, with the shell special parameter ?.



From man bash:



? Expands to the exit status of the most recently executed foreground pipeline.



After you run the mount command, immediately executing echo $? will print the status code from the previous command.



# mount /dev/dvd1 /mnt
mount: no medium found on /dev/sr0
# echo $?
32


Not all executables have well defined status codes. At a minimum, it should exit with a success (0) or failure (1) code, but that's not always the case.



To expand on (and correct) your example script, I added a nested if construct for clarity. It's not the only way to test the status code and perform an action, but it's the easiest to read when learning.



#!/bin/bash
mount="/myfilesystem"

if grep -qs "$mount" /proc/mounts; then
echo "It's mounted."
else
echo "It's not mounted."
mount "$mount"
if [ $? -eq 0 ]; then
echo "Mount success!"
else
echo "Something went wrong with the mount..."
fi
fi


For more information on "Exit and Exit Status", you can refer to the Advanced Bash-Scripting Guide.






share|improve this answer


















  • 1




    There's no need to export $MOUNT. This script will also break if the filesystem has any character that would undergo wordsplitting (spaces, etc.). You should always quote your expansions.
    – Chris Down
    May 17 '12 at 18:49










  • @ChrisDown You're right. I only deleted the obvious dangling ';then'. In the future, feel free to edit my answer to make it more correct.
    – George M
    May 17 '12 at 19:30










  • Er... this will still break. You have still not quoted the expansions. On your advice, I'll do it now.
    – Chris Down
    May 17 '12 at 20:14










  • I also wouldn't recommend linking to the infamous "Advanced" Bash scripting guide, it's full of errors and will teach people to write bugs, not scripts. BashGuide is a far better alternative.
    – Chris Down
    May 17 '12 at 20:15






  • 2




    @ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by.
    – George M
    May 17 '12 at 21:25

















up vote
29
down vote













Many Linux distros have the mountpoint command. It can explicitly used to check if a directory is a mountpoint. Simple as this:



#!/bin/bash 
if mountpoint -q "$1"; then
echo "$1 is a mountpoint"
else
echo "$1 is not a mountpoint"
fi





share|improve this answer






















  • This approach will fail if you try to check an encfs, since you will stumble into a permission denied error, when run by a non-root user.
    – Denys S.
    Dec 1 '15 at 8:40










  • My solution works with encfs.
    – Theodore R. Smith
    Mar 22 at 16:13

















up vote
2
down vote













The easiest way which doesn't require root is:



if $(df | grep -q /mnt/ramdisk); then

fi


or to see if it isn't mounted:



if ! $(df | grep -q /mnt/ramdisk); then

fi





share|improve this answer




















  • the mount command doesn't need root access, df is focused on disc space not on mount points...
    – Philippe Gachoud
    Nov 28 at 11:04

















up vote
1
down vote













One more way:



if [ "$(findmnt $mount_point)" ] ; then
#Do something for positive result (exit 0)
else
#Do something for negative result (exit 1)
fi





share|improve this answer






















  • Remove those "$()", otherwise you're not testing for exit status, you're testing the output of findmnt (which happens to output something in the true-case and nothing in the false-case, but it's not obviously safe to rely on this behaviour).
    – YoungFrog
    Nov 2 at 22:36

















up vote
0
down vote













Short statements



Check if mounted:



mount|grep -q "/mnt/data" && echo "/mnt/data is mounted; I can follow my job!"


Check if not mounted:



mount|grep -q "/mnt/data" || echo "/mnt/data is not mounted I could probably mount it!"





share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f38870%2fhow-to-check-if-a-filesystem-is-mounted-with-a-script%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    14
    down vote



    accepted










    You can check the status code of mount, and most well written executables, with the shell special parameter ?.



    From man bash:



    ? Expands to the exit status of the most recently executed foreground pipeline.



    After you run the mount command, immediately executing echo $? will print the status code from the previous command.



    # mount /dev/dvd1 /mnt
    mount: no medium found on /dev/sr0
    # echo $?
    32


    Not all executables have well defined status codes. At a minimum, it should exit with a success (0) or failure (1) code, but that's not always the case.



    To expand on (and correct) your example script, I added a nested if construct for clarity. It's not the only way to test the status code and perform an action, but it's the easiest to read when learning.



    #!/bin/bash
    mount="/myfilesystem"

    if grep -qs "$mount" /proc/mounts; then
    echo "It's mounted."
    else
    echo "It's not mounted."
    mount "$mount"
    if [ $? -eq 0 ]; then
    echo "Mount success!"
    else
    echo "Something went wrong with the mount..."
    fi
    fi


    For more information on "Exit and Exit Status", you can refer to the Advanced Bash-Scripting Guide.






    share|improve this answer


















    • 1




      There's no need to export $MOUNT. This script will also break if the filesystem has any character that would undergo wordsplitting (spaces, etc.). You should always quote your expansions.
      – Chris Down
      May 17 '12 at 18:49










    • @ChrisDown You're right. I only deleted the obvious dangling ';then'. In the future, feel free to edit my answer to make it more correct.
      – George M
      May 17 '12 at 19:30










    • Er... this will still break. You have still not quoted the expansions. On your advice, I'll do it now.
      – Chris Down
      May 17 '12 at 20:14










    • I also wouldn't recommend linking to the infamous "Advanced" Bash scripting guide, it's full of errors and will teach people to write bugs, not scripts. BashGuide is a far better alternative.
      – Chris Down
      May 17 '12 at 20:15






    • 2




      @ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by.
      – George M
      May 17 '12 at 21:25














    up vote
    14
    down vote



    accepted










    You can check the status code of mount, and most well written executables, with the shell special parameter ?.



    From man bash:



    ? Expands to the exit status of the most recently executed foreground pipeline.



    After you run the mount command, immediately executing echo $? will print the status code from the previous command.



    # mount /dev/dvd1 /mnt
    mount: no medium found on /dev/sr0
    # echo $?
    32


    Not all executables have well defined status codes. At a minimum, it should exit with a success (0) or failure (1) code, but that's not always the case.



    To expand on (and correct) your example script, I added a nested if construct for clarity. It's not the only way to test the status code and perform an action, but it's the easiest to read when learning.



    #!/bin/bash
    mount="/myfilesystem"

    if grep -qs "$mount" /proc/mounts; then
    echo "It's mounted."
    else
    echo "It's not mounted."
    mount "$mount"
    if [ $? -eq 0 ]; then
    echo "Mount success!"
    else
    echo "Something went wrong with the mount..."
    fi
    fi


    For more information on "Exit and Exit Status", you can refer to the Advanced Bash-Scripting Guide.






    share|improve this answer


















    • 1




      There's no need to export $MOUNT. This script will also break if the filesystem has any character that would undergo wordsplitting (spaces, etc.). You should always quote your expansions.
      – Chris Down
      May 17 '12 at 18:49










    • @ChrisDown You're right. I only deleted the obvious dangling ';then'. In the future, feel free to edit my answer to make it more correct.
      – George M
      May 17 '12 at 19:30










    • Er... this will still break. You have still not quoted the expansions. On your advice, I'll do it now.
      – Chris Down
      May 17 '12 at 20:14










    • I also wouldn't recommend linking to the infamous "Advanced" Bash scripting guide, it's full of errors and will teach people to write bugs, not scripts. BashGuide is a far better alternative.
      – Chris Down
      May 17 '12 at 20:15






    • 2




      @ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by.
      – George M
      May 17 '12 at 21:25












    up vote
    14
    down vote



    accepted







    up vote
    14
    down vote



    accepted






    You can check the status code of mount, and most well written executables, with the shell special parameter ?.



    From man bash:



    ? Expands to the exit status of the most recently executed foreground pipeline.



    After you run the mount command, immediately executing echo $? will print the status code from the previous command.



    # mount /dev/dvd1 /mnt
    mount: no medium found on /dev/sr0
    # echo $?
    32


    Not all executables have well defined status codes. At a minimum, it should exit with a success (0) or failure (1) code, but that's not always the case.



    To expand on (and correct) your example script, I added a nested if construct for clarity. It's not the only way to test the status code and perform an action, but it's the easiest to read when learning.



    #!/bin/bash
    mount="/myfilesystem"

    if grep -qs "$mount" /proc/mounts; then
    echo "It's mounted."
    else
    echo "It's not mounted."
    mount "$mount"
    if [ $? -eq 0 ]; then
    echo "Mount success!"
    else
    echo "Something went wrong with the mount..."
    fi
    fi


    For more information on "Exit and Exit Status", you can refer to the Advanced Bash-Scripting Guide.






    share|improve this answer














    You can check the status code of mount, and most well written executables, with the shell special parameter ?.



    From man bash:



    ? Expands to the exit status of the most recently executed foreground pipeline.



    After you run the mount command, immediately executing echo $? will print the status code from the previous command.



    # mount /dev/dvd1 /mnt
    mount: no medium found on /dev/sr0
    # echo $?
    32


    Not all executables have well defined status codes. At a minimum, it should exit with a success (0) or failure (1) code, but that's not always the case.



    To expand on (and correct) your example script, I added a nested if construct for clarity. It's not the only way to test the status code and perform an action, but it's the easiest to read when learning.



    #!/bin/bash
    mount="/myfilesystem"

    if grep -qs "$mount" /proc/mounts; then
    echo "It's mounted."
    else
    echo "It's not mounted."
    mount "$mount"
    if [ $? -eq 0 ]; then
    echo "Mount success!"
    else
    echo "Something went wrong with the mount..."
    fi
    fi


    For more information on "Exit and Exit Status", you can refer to the Advanced Bash-Scripting Guide.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 17 '12 at 20:15









    Chris Down

    78.4k13187200




    78.4k13187200










    answered May 17 '12 at 16:15









    George M

    8,99623247




    8,99623247







    • 1




      There's no need to export $MOUNT. This script will also break if the filesystem has any character that would undergo wordsplitting (spaces, etc.). You should always quote your expansions.
      – Chris Down
      May 17 '12 at 18:49










    • @ChrisDown You're right. I only deleted the obvious dangling ';then'. In the future, feel free to edit my answer to make it more correct.
      – George M
      May 17 '12 at 19:30










    • Er... this will still break. You have still not quoted the expansions. On your advice, I'll do it now.
      – Chris Down
      May 17 '12 at 20:14










    • I also wouldn't recommend linking to the infamous "Advanced" Bash scripting guide, it's full of errors and will teach people to write bugs, not scripts. BashGuide is a far better alternative.
      – Chris Down
      May 17 '12 at 20:15






    • 2




      @ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by.
      – George M
      May 17 '12 at 21:25












    • 1




      There's no need to export $MOUNT. This script will also break if the filesystem has any character that would undergo wordsplitting (spaces, etc.). You should always quote your expansions.
      – Chris Down
      May 17 '12 at 18:49










    • @ChrisDown You're right. I only deleted the obvious dangling ';then'. In the future, feel free to edit my answer to make it more correct.
      – George M
      May 17 '12 at 19:30










    • Er... this will still break. You have still not quoted the expansions. On your advice, I'll do it now.
      – Chris Down
      May 17 '12 at 20:14










    • I also wouldn't recommend linking to the infamous "Advanced" Bash scripting guide, it's full of errors and will teach people to write bugs, not scripts. BashGuide is a far better alternative.
      – Chris Down
      May 17 '12 at 20:15






    • 2




      @ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by.
      – George M
      May 17 '12 at 21:25







    1




    1




    There's no need to export $MOUNT. This script will also break if the filesystem has any character that would undergo wordsplitting (spaces, etc.). You should always quote your expansions.
    – Chris Down
    May 17 '12 at 18:49




    There's no need to export $MOUNT. This script will also break if the filesystem has any character that would undergo wordsplitting (spaces, etc.). You should always quote your expansions.
    – Chris Down
    May 17 '12 at 18:49












    @ChrisDown You're right. I only deleted the obvious dangling ';then'. In the future, feel free to edit my answer to make it more correct.
    – George M
    May 17 '12 at 19:30




    @ChrisDown You're right. I only deleted the obvious dangling ';then'. In the future, feel free to edit my answer to make it more correct.
    – George M
    May 17 '12 at 19:30












    Er... this will still break. You have still not quoted the expansions. On your advice, I'll do it now.
    – Chris Down
    May 17 '12 at 20:14




    Er... this will still break. You have still not quoted the expansions. On your advice, I'll do it now.
    – Chris Down
    May 17 '12 at 20:14












    I also wouldn't recommend linking to the infamous "Advanced" Bash scripting guide, it's full of errors and will teach people to write bugs, not scripts. BashGuide is a far better alternative.
    – Chris Down
    May 17 '12 at 20:15




    I also wouldn't recommend linking to the infamous "Advanced" Bash scripting guide, it's full of errors and will teach people to write bugs, not scripts. BashGuide is a far better alternative.
    – Chris Down
    May 17 '12 at 20:15




    2




    2




    @ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by.
    – George M
    May 17 '12 at 21:25




    @ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by.
    – George M
    May 17 '12 at 21:25












    up vote
    29
    down vote













    Many Linux distros have the mountpoint command. It can explicitly used to check if a directory is a mountpoint. Simple as this:



    #!/bin/bash 
    if mountpoint -q "$1"; then
    echo "$1 is a mountpoint"
    else
    echo "$1 is not a mountpoint"
    fi





    share|improve this answer






















    • This approach will fail if you try to check an encfs, since you will stumble into a permission denied error, when run by a non-root user.
      – Denys S.
      Dec 1 '15 at 8:40










    • My solution works with encfs.
      – Theodore R. Smith
      Mar 22 at 16:13














    up vote
    29
    down vote













    Many Linux distros have the mountpoint command. It can explicitly used to check if a directory is a mountpoint. Simple as this:



    #!/bin/bash 
    if mountpoint -q "$1"; then
    echo "$1 is a mountpoint"
    else
    echo "$1 is not a mountpoint"
    fi





    share|improve this answer






















    • This approach will fail if you try to check an encfs, since you will stumble into a permission denied error, when run by a non-root user.
      – Denys S.
      Dec 1 '15 at 8:40










    • My solution works with encfs.
      – Theodore R. Smith
      Mar 22 at 16:13












    up vote
    29
    down vote










    up vote
    29
    down vote









    Many Linux distros have the mountpoint command. It can explicitly used to check if a directory is a mountpoint. Simple as this:



    #!/bin/bash 
    if mountpoint -q "$1"; then
    echo "$1 is a mountpoint"
    else
    echo "$1 is not a mountpoint"
    fi





    share|improve this answer














    Many Linux distros have the mountpoint command. It can explicitly used to check if a directory is a mountpoint. Simple as this:



    #!/bin/bash 
    if mountpoint -q "$1"; then
    echo "$1 is a mountpoint"
    else
    echo "$1 is not a mountpoint"
    fi






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 21 '12 at 18:02









    George M

    8,99623247




    8,99623247










    answered May 21 '12 at 17:50









    grosshat

    391124




    391124











    • This approach will fail if you try to check an encfs, since you will stumble into a permission denied error, when run by a non-root user.
      – Denys S.
      Dec 1 '15 at 8:40










    • My solution works with encfs.
      – Theodore R. Smith
      Mar 22 at 16:13
















    • This approach will fail if you try to check an encfs, since you will stumble into a permission denied error, when run by a non-root user.
      – Denys S.
      Dec 1 '15 at 8:40










    • My solution works with encfs.
      – Theodore R. Smith
      Mar 22 at 16:13















    This approach will fail if you try to check an encfs, since you will stumble into a permission denied error, when run by a non-root user.
    – Denys S.
    Dec 1 '15 at 8:40




    This approach will fail if you try to check an encfs, since you will stumble into a permission denied error, when run by a non-root user.
    – Denys S.
    Dec 1 '15 at 8:40












    My solution works with encfs.
    – Theodore R. Smith
    Mar 22 at 16:13




    My solution works with encfs.
    – Theodore R. Smith
    Mar 22 at 16:13










    up vote
    2
    down vote













    The easiest way which doesn't require root is:



    if $(df | grep -q /mnt/ramdisk); then

    fi


    or to see if it isn't mounted:



    if ! $(df | grep -q /mnt/ramdisk); then

    fi





    share|improve this answer




















    • the mount command doesn't need root access, df is focused on disc space not on mount points...
      – Philippe Gachoud
      Nov 28 at 11:04














    up vote
    2
    down vote













    The easiest way which doesn't require root is:



    if $(df | grep -q /mnt/ramdisk); then

    fi


    or to see if it isn't mounted:



    if ! $(df | grep -q /mnt/ramdisk); then

    fi





    share|improve this answer




















    • the mount command doesn't need root access, df is focused on disc space not on mount points...
      – Philippe Gachoud
      Nov 28 at 11:04












    up vote
    2
    down vote










    up vote
    2
    down vote









    The easiest way which doesn't require root is:



    if $(df | grep -q /mnt/ramdisk); then

    fi


    or to see if it isn't mounted:



    if ! $(df | grep -q /mnt/ramdisk); then

    fi





    share|improve this answer












    The easiest way which doesn't require root is:



    if $(df | grep -q /mnt/ramdisk); then

    fi


    or to see if it isn't mounted:



    if ! $(df | grep -q /mnt/ramdisk); then

    fi






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 9 at 15:08









    Theodore R. Smith

    1781110




    1781110











    • the mount command doesn't need root access, df is focused on disc space not on mount points...
      – Philippe Gachoud
      Nov 28 at 11:04
















    • the mount command doesn't need root access, df is focused on disc space not on mount points...
      – Philippe Gachoud
      Nov 28 at 11:04















    the mount command doesn't need root access, df is focused on disc space not on mount points...
    – Philippe Gachoud
    Nov 28 at 11:04




    the mount command doesn't need root access, df is focused on disc space not on mount points...
    – Philippe Gachoud
    Nov 28 at 11:04










    up vote
    1
    down vote













    One more way:



    if [ "$(findmnt $mount_point)" ] ; then
    #Do something for positive result (exit 0)
    else
    #Do something for negative result (exit 1)
    fi





    share|improve this answer






















    • Remove those "$()", otherwise you're not testing for exit status, you're testing the output of findmnt (which happens to output something in the true-case and nothing in the false-case, but it's not obviously safe to rely on this behaviour).
      – YoungFrog
      Nov 2 at 22:36














    up vote
    1
    down vote













    One more way:



    if [ "$(findmnt $mount_point)" ] ; then
    #Do something for positive result (exit 0)
    else
    #Do something for negative result (exit 1)
    fi





    share|improve this answer






















    • Remove those "$()", otherwise you're not testing for exit status, you're testing the output of findmnt (which happens to output something in the true-case and nothing in the false-case, but it's not obviously safe to rely on this behaviour).
      – YoungFrog
      Nov 2 at 22:36












    up vote
    1
    down vote










    up vote
    1
    down vote









    One more way:



    if [ "$(findmnt $mount_point)" ] ; then
    #Do something for positive result (exit 0)
    else
    #Do something for negative result (exit 1)
    fi





    share|improve this answer














    One more way:



    if [ "$(findmnt $mount_point)" ] ; then
    #Do something for positive result (exit 0)
    else
    #Do something for negative result (exit 1)
    fi






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 18 at 11:07









    sungtm

    31




    31










    answered May 18 at 10:12









    Sun

    111




    111











    • Remove those "$()", otherwise you're not testing for exit status, you're testing the output of findmnt (which happens to output something in the true-case and nothing in the false-case, but it's not obviously safe to rely on this behaviour).
      – YoungFrog
      Nov 2 at 22:36
















    • Remove those "$()", otherwise you're not testing for exit status, you're testing the output of findmnt (which happens to output something in the true-case and nothing in the false-case, but it's not obviously safe to rely on this behaviour).
      – YoungFrog
      Nov 2 at 22:36















    Remove those "$()", otherwise you're not testing for exit status, you're testing the output of findmnt (which happens to output something in the true-case and nothing in the false-case, but it's not obviously safe to rely on this behaviour).
    – YoungFrog
    Nov 2 at 22:36




    Remove those "$()", otherwise you're not testing for exit status, you're testing the output of findmnt (which happens to output something in the true-case and nothing in the false-case, but it's not obviously safe to rely on this behaviour).
    – YoungFrog
    Nov 2 at 22:36










    up vote
    0
    down vote













    Short statements



    Check if mounted:



    mount|grep -q "/mnt/data" && echo "/mnt/data is mounted; I can follow my job!"


    Check if not mounted:



    mount|grep -q "/mnt/data" || echo "/mnt/data is not mounted I could probably mount it!"





    share|improve this answer
























      up vote
      0
      down vote













      Short statements



      Check if mounted:



      mount|grep -q "/mnt/data" && echo "/mnt/data is mounted; I can follow my job!"


      Check if not mounted:



      mount|grep -q "/mnt/data" || echo "/mnt/data is not mounted I could probably mount it!"





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Short statements



        Check if mounted:



        mount|grep -q "/mnt/data" && echo "/mnt/data is mounted; I can follow my job!"


        Check if not mounted:



        mount|grep -q "/mnt/data" || echo "/mnt/data is not mounted I could probably mount it!"





        share|improve this answer












        Short statements



        Check if mounted:



        mount|grep -q "/mnt/data" && echo "/mnt/data is mounted; I can follow my job!"


        Check if not mounted:



        mount|grep -q "/mnt/data" || echo "/mnt/data is not mounted I could probably mount it!"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 at 11:02









        Philippe Gachoud

        423310




        423310



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f38870%2fhow-to-check-if-a-filesystem-is-mounted-with-a-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)