LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-09-2013, 10:45 PM   #1
pengusaha
LQ Newbie
 
Registered: Dec 2007
Distribution: Redhat FC6
Posts: 11

Rep: Reputation: 0
Smile creat "if" batch Job


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
 
Old 04-09-2013, 11:04 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Please use code tags for your code. It makes it easier to read and preserves formatting.

You can use command list instead of the if...then command.
Code:
command1 && command2
will run command2 only if command1 returns an exit status of zero (success), which is the same as:
Code:
command1
if [ "$?" = "0" ]; then
  command2
fi
And then there is:
Code:
command1 && command2 || command3
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
See
Code:
info bash
for more info.

Hope it helps.

Last edited by towheedm; 04-09-2013 at 11:07 PM.
 
1 members found this post helpful.
Old 04-10-2013, 09:10 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by pengusaha View Post
how to add "IF" command to Mount -t blablabla, ?
http://www.linuxquestions.org/questi...ng-4175453091/

but it goes something like this:
Code:
#!/bin/bash
MOUNTPOINT=$(mount | grep nas)
if [[ -n "$MOUNTPOINT" ]] ; then 
	cp /home/folder/abd.BBB /mnt/nas/ABC-`date +%d%b%Y-%H%M%S`.BBB
else 
	mount -t cifs //192.168.0.249/folder/s /mnt/nas -o user=abc,pass=abc,nocase
	cp /home/folder/abd.BBB /mnt/nas/ABC-`date +%d%b%Y-%H%M%S`.BBB
	exit
fi
exit 0
#EOF

Last edited by Habitual; 04-10-2013 at 09:59 AM. Reason: FIXED " ]] FROM "]]
 
1 members found this post helpful.
Old 04-10-2013, 07:11 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd add a check to see if the mount succeeded
Code:
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
 
Old 04-10-2013, 07:19 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by chrism01 View Post
I'd add a check to see if the mount succeeded
Code:
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.
 
Old 04-10-2013, 08:35 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Habitual already checked if its mounted (post #3), I was just safety checking in the re-mount case
 
Old 04-10-2013, 08:48 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 04-11-2013, 04:10 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by towheedm View Post

And then there is:
Code:
command1 && command2 || command3
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.

http://mywiki.wooledge.org/BashPitfa...d2_.7C.7C_cmd3

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.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

Code:
if (( $? == 0 )); then

#or even:

if (( ! $? )); then

#In arithmetic contexts zero evaluates as a false condition.
Not to mention that in square-bracket tests, "=/==" is a string comparison operator. For numerical comparisons you need to use "-eq".


Finally, you don't actually even need the explicit test at all. You can evaluate command1 directly:

Code:
if command1 ; then
    command2
else
    command3
fi

Last edited by David the H.; 04-11-2013 at 04:14 PM. Reason: minor code
 
1 members found this post helpful.
Old 04-11-2013, 10:32 PM   #9
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Thanks for the pointers David the H.

I never realized command3 will run if commmand2 fails with command list. Did not pick that up from BASH's info pages.

I get the test construct, just an oversight on my part.
 
Old 04-16-2013, 05:20 AM   #10
pengusaha
LQ Newbie
 
Registered: Dec 2007
Distribution: Redhat FC6
Posts: 11

Original Poster
Rep: Reputation: 0
Thx for the reply.

Code:
#!/bin/bash
MOUNTPOINT=$(mount | grep nas)
if [[ -n "$MOUNTPOINT" ]] ; then 
	cp /home/folder/abd.BBB /mnt/nas/ABC-`date +%d%b%Y-%H%M%S`.BBB
else 
	mount -t cifs //192.168.0.249/folder/s /mnt/nas -o user=abc,pass=abc,nocase
	cp /home/folder/abd.BBB /mnt/nas/ABC-`date +%d%b%Y-%H%M%S`.BBB
	exit
fi
exit 0
#EOF
if the mount failed, then the nas is not turn on / elect fail.

then how to add a "mail to me" if the copied success, if the mount fail, Thx
 
Old 04-16-2013, 07:15 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
mailx example http://stackoverflow.com/questions/2...-mailx-command
 
Old 04-16-2013, 09:33 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
(Deleted. Something was terribly misread.)

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.
 
Old 10-06-2014, 10:13 PM   #13
pengusaha
LQ Newbie
 
Registered: Dec 2007
Distribution: Redhat FC6
Posts: 11

Original Poster
Rep: Reputation: 0
run this line on dos

Dear Mr,
due to our Accounting software doesnt support linux anymore,
how do i run this line on dos prompt (win81) Thx

cp /home/folder/abd.BBB /mnt/nas/ABC-`date +%d%b%Y-%H%M%S`.BBB


Thx

[solved] Thx
batch file like this :
set filea=j%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%.gsb
copy a.txt %filea%

Last edited by pengusaha; 10-06-2014 at 10:34 PM.
 
  


Reply



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
Job-hunting? Skip the recruiters and "job shops" sundialsvcs General 4 07-24-2012 05:00 PM
Batch user creating with the "newusers" command question mainuser Linux - Newbie 1 07-26-2011 07:35 PM
Batch renaming without using "for" in bash shell {} Peterius Linux - General 2 04-28-2008 07:39 PM
Shell and batch operations on hidden files but not on ".." & "." danielsbrewer Linux - Software 5 09-12-2007 08:06 AM
How to creat a gnome desktop shortcut to "my_music" folder? hkl8324 Linux - Newbie 1 07-04-2005 12:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:47 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