ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I'm trying to automate a backup process via shell scripts.
I have a NAS box that I use across two machines so I would like to first check if my NAS is mounted via shell.
And how would I use it in an IF condition? I know how to use IF's in terms of structure in a shell script but what would the logical condition itself be?
But that would only see if the folder /mnt/nas has something in it, right?
If another user happens to copy something to that folder without checking if it was mounted (while NAS is not mounted, hypothetical case), then that condition would result in a false positive, would it not?
As I said before:
"Well, /etc/mtab tells you what is actually mounted, as opposed to /etc/fstab, which tells you what is mounted a boot time."
Try cat'ing that file then dismount something and cat it again.....
As I said before:
"Well, /etc/mtab tells you what is actually mounted, as opposed to /etc/fstab, which tells you what is mounted a boot time."
Try cat'ing that file then dismount something and cat it again.....
as i mentioned in my second post, i'm not sure how the IF condition should be written. I need to know what to check for on the IF on the return of the cat of mtab.
That's because grep returns zero (false) if it matches, 1 if no matches, 2 (?) if an error, so reverse your logic.
It'd be clearer if you did the grep on it's own line, then just test the status ($?) in the if [[ ]] eg
Code:
grep -flags string file
if [[ $? -eq 0 ]]
then
grep matched, do s'thing
else
no match, do other thing
fi
That's because grep returns zero (false) if it matches, 1 if no matches, 2 (?) if an error, so reverse your logic.
It'd be clearer if you did the grep on it's own line, then just test the status ($?) in the if [[ ]] eg
Code:
grep -flags string file
if [[ $? -eq 0 ]]
then
grep matched, do s'thing
else
no match, do other thing
fi
Thanks.
I had the opportunity to try this out today again (I was assigned to do something else the last few days).
I ran it on another machine, testing if the SATA drive was mounted. When I tested for a mounted SATA drive, it worked fine but when I tested for a non existent SATA drive, the else portion of the condition did not work. I'm assuming it did not work because I didn't see the echo'd message.
Code:
grep -qi \/dev\/sda4 /etc/mtab
if [[ $? -eq 0 ]]
then
echo 'NAS is mounted. Continuing...';
else
echo 'NAS is not mounted. Can not continue. Exiting...';
fi
I had the opportunity to try this out today again (I was assigned to do something else the last few days).
I ran it on another machine, testing if the SATA drive was mounted. When I tested for a mounted SATA drive, it worked fine but when I tested for a non existent SATA drive, the else portion of the condition did not work. I'm assuming it did not work because I didn't see the echo'd message.
Code:
grep -qi \/dev\/sda4 /etc/mtab
if [[ $? -eq 0 ]]
then
echo 'NAS is mounted. Continuing...';
else
echo 'NAS is not mounted. Can not continue. Exiting...';
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.