LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   creat "if" batch Job (https://www.linuxquestions.org/questions/linux-newbie-8/creat-if-batch-job-4175457578/)

pengusaha 04-09-2013 10:45 PM

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

towheedm 04-09-2013 11:04 PM

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.

Habitual 04-10-2013 09:10 AM

Quote:

Originally Posted by pengusaha (Post 4928614)
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


chrism01 04-10-2013 07:11 PM

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


suicidaleggroll 04-10-2013 07:19 PM

Quote:

Originally Posted by chrism01 (Post 4929437)
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.

chrism01 04-10-2013 08:35 PM

Habitual already checked if its mounted (post #3), I was just safety checking in the re-mount case :)

suicidaleggroll 04-10-2013 08:48 PM

Quote:

Originally Posted by chrism01 (Post 4929468)
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.

David the H. 04-11-2013 04:10 PM

Quote:

Originally Posted by towheedm (Post 4928620)

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


towheedm 04-11-2013 10:32 PM

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.

pengusaha 04-16-2013 05:20 AM

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

chrism01 04-16-2013 07:15 AM

mailx example http://stackoverflow.com/questions/2...-mailx-command

konsolebox 04-16-2013 09:33 AM

(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


pengusaha 10-06-2014 10:13 PM

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%


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