Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Dear Mr / Guru,
ive qnap and set to turn itself on every day on 8am for 1 hour. .
so my linux fedora 14 (laughlin) will auto copy 1 file to my qnap on 8:05am,
ok. so i put in crontab, 1 file batch to be run every 0805am
my file in crontab is :
Code:
#!/bin/bash
# to access qnap
mount -t cifs //192.168.0.249/folder/s /mnt/nas -o user=abc,pass=abc,nocase
# to copy my 1 file to qnap
cp /home/folder/abd.BBB /mnt/nas/ABC-`date +%d%b%Y-%H%M%S`.BBB
ok, the question id like to ask is :
This command :
Code:
cp /home/folder/abd.BBB /mnt/nas/ABC-`date +%d%b%Y-%H%M%S`.BBB
only run if mount is success, otherwise , no run.
how to add "IF" command to Mount -t blablabla, ?
Thx
Last edited by pengusaha; 04-09-2013 at 11:21 PM.
Reason: create code tag
else
mount -t cifs //192.168.0.249/folder/s /mnt/nas -o user=abc,pass=abc,nocase
if [[ $? -eq 0 ]]
then
cp /home/folder/abd.BBB /mnt/nas/ABC-`date +%d%b%Y-%H%M%S`.BBB
else
echo "mount on /mnt/nas failed"
exit 1
fi
fi
else
mount -t cifs //192.168.0.249/folder/s /mnt/nas -o user=abc,pass=abc,nocase
if [[ $? -eq 0 ]]
then
cp /home/folder/abd.BBB /mnt/nas/ABC-`date +%d%b%Y-%H%M%S`.BBB
else
echo "mount on /mnt/nas failed"
exit 1
fi
fi
This is what I would do, but keep in mind that there is no unmount in this script (or the one the OP provided). This means that the first time it runs it will probably work fine, but any subsequent run will likely result in an error on the mount command since it's already been mounted.
I would alter the approach to first check if the drive has already been mounted. If it has, continue with the copy. If it hasn't, then try to mount it. If successful, then continue with the copy, otherwise spit out an error and exit.
As for checking if the drive has been mounted - you can write some code to parse the output of mount or df, but what I typically do is place a small non-intrusive file on the mounted drive, that will never be deleted or renamed. Something like an empty text file called "verify" or "present". Then all you have to do is check for the existence of this file at the mount point. If it exists, you know the drive is mounted, if it doesn't then it's not. Of course this isn't "intruder proof", since all somebody has to do is delete this file or create the file at the mount point to fool the checker, but for a system that you have full control over, it's a very easy and painless way to check for a mounted drive.
Last edited by suicidaleggroll; 04-10-2013 at 07:26 PM.
Habitual already checked if its mounted (post #3), I was just safety checking in the re-mount case
Sorry, I was thinking your post stood alone, rather than an "addendum" to post #3. I guess I missed the "else" and "fi" when I first read it. My mistake.
will run command2 if command1 returns an exit status of zero, else it will run command3. This is same as:
Code:
command1
if [ "$?" = "0" ]; then
command2
else
command3
fi
These two are not exactly equivalent. There's a big gotcha involved with the first pattern. If command2 evaluates as false (exit code >0), then command3 will run as well.
In other words, the && and || conditions are evaluated independently, not as part of a single expression. So only use "..&&..||.." if the second command can never fail, such as when echoing feedback messages.
On a second note, when using advanced shells like bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.
You could also make use of mountpoint if you have the tool to check if something is mounted in a specific mountpoint like:
Code:
if mountpoint -q /mnt/nas; then
...
fi
It's more specific that way.
Also, things would be less redundant through this:
Code:
#!/bin/bash
if ! mountpoint -q /mnt/nas; then
mount -t cifs //192.168.0.249/folder/s /mnt/nas -o user=abc,pass=abc,nocase || {
echo "Unable to mount //192.168.0.249/folder/s to /mnt/nas."
exit 1
}
fi
FILENAME=ABC-`date +%d%b%Y-%H%M%S`.BBB
cp /home/folder/abd.BBB "/mnt/nas/$FILENAME" || {
echo "Failed to copy /home/folder/abd.BBB to /mnt/nas as $FILENAME."
exit 1
}
... do other stuffs like mailing here.
exit 0
Last edited by konsolebox; 04-16-2013 at 09:39 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.