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 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 Thx |
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 Code:
command1 Code:
command1 && command2 || command3 Code:
command1 Code:
info bash Hope it helps. |
Quote:
but it goes something like this: Code:
#!/bin/bash |
I'd add a check to see if the mount succeeded
Code:
else |
Quote:
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. |
Habitual already checked if its mounted (post #3), I was just safety checking in the re-mount case :)
|
Quote:
|
Quote:
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 Finally, you don't actually even need the explicit test at all. You can evaluate command1 directly: Code:
if command1 ; then |
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. |
Thx for the reply.
Code:
#!/bin/bash then how to add a "mail to me" if the copied success, if the mount fail, Thx |
mailx example http://stackoverflow.com/questions/2...-mailx-command
|
(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 Also, things would be less redundant through this: Code:
#!/bin/bash |
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. |