LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-15-2019, 09:54 AM   #1
subby80
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Rep: Reputation: Disabled
Need help to create a script


Hi,
I need to create a small script which i want to run every night to check if a file is created in a certain directory. If a file is created i need an email message to warn me about it.

I can find the file like this

# find folder -mtime 0 -name "test*" | mail -s 'test' mailadress

But this also sends a message without body. I only want a mail if a file exists.

Thanks
 
Old 10-15-2019, 10:00 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by subby80 View Post
Hi,
I need to create a small script which i want to run every night to check if a file is created in a certain directory. If a file is created i need an email message to warn me about it. I can find the file like this

# find folder -mtime 0 -name "test*" | mail -s 'test' mailadress

But this also sends a message without body. I only want a mail if a file exists.
Ok...so now that we know what you want, why don't you post what you have DONE to accomplish this?? Read the "Question Guidelines"...we're happy to help you, but we WILL NOT write your scripts for you. There are abundant tutorials available, along with samples/examples, to get you started, and you can find them with a brief Google search.

Post what you've written/done/tried (past the single-line above), and we'll be glad to help.
 
Old 10-15-2019, 10:30 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Please check the link in my signature for My Bash Blog. It discusses how to create and debug bash scripts and discusses the very most important detail, which is if you can type it in a command line, you can add it to a script and run it that way.
 
1 members found this post helpful.
Old 10-15-2019, 10:45 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
you're going to need to look more in depth to how conditionals work and relationship to variable checking for the ' if it is empty or not?'

Code:
[[ -n $(find folder -mtime 0 -name "test*") ]] && send  mail
https://www.cyberciti.biz/faq/unix-l...able-is-empty/

https://www.tldp.org/LDP/abs/html/comparison-ops.html
 
1 members found this post helpful.
Old 10-15-2019, 11:59 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Have you thought about using an inotify-based program to watch for file creation, like inotifywatch, entr, or watchman?
 
1 members found this post helpful.
Old 10-17-2019, 03:09 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
If it is only one particular file in a particular location, you do not need find.
Code:
test -e /path/to/file && mail -s ....
 
1 members found this post helpful.
Old 10-18-2019, 12:15 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Store the find output in a variable. Then test the variable; if not empty then send mail with contents from the variable.
Code:
#!/bin/sh
out=$(find folder -mtime 0 -name "test*")
if [ -n "$out" ]
then
  echo "$out" | mail -s 'test'
fi

Last edited by MadeInGermany; 10-18-2019 at 12:31 AM. Reason: Fixed typo
 
Old 10-18-2019, 01:52 AM   #8
subby80
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: Disabled
Hi,


I did it al little bit different but it works:
Code:
t3=$(find folder -mtime 0 -name "*.txt")

if test -z "$t1"
then
      echo "\$t1 is empty"
else
       find find -mtime 0 -name "*.ERG" | mail -s 'error' Mailaddress
fi

Last edited by subby80; 10-21-2019 at 01:38 AM.
 
Old 10-18-2019, 06:51 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
seriously, subby80 didn't you notice that your code blocks did not work?

Use the edit button and use the square brackets [ ]

Any method will work

test condition then execute command(s) related to results of that test. causes and effects.

test are always seeking a true or false value but only act on the truth, else they throw an error, and they don't work.

Last edited by BW-userx; 10-18-2019 at 06:57 AM.
 
Old 10-18-2019, 08:21 AM   #10
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 369
Blog Entries: 1

Rep: Reputation: 49
Quote:
Originally Posted by ondoho View Post
If it is only one particular file in a particular location, you do not need find.
Code:
test -e /path/to/file && mail -s ....
I +1 this.
 
Old 10-18-2019, 08:55 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by ychaouche View Post
I +1 this.
same as
Code:
[ -e /path/to/file ] && do something

[[ -e /path/to/file ]] && do something

if [ -e /path/to/file ] ;
then
   do something
fi

if [[-e /path/to/file ]] ;
then
   do something
fi
File test operators

https://linuxize.com/post/bash-check-if-file-exists/
https://www.shellhacks.com/bash-test-if-file-exists/

Last edited by BW-userx; 10-18-2019 at 09:01 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is it possible to create a bash script to create multiple new bash scripts Batistuta_g_2000 Linux - Newbie 6 02-19-2013 11:42 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
How to create a Create script "easier"? lorio Linux - Newbie 6 10-10-2006 06:05 PM
Need help to create a script barneyt Programming 2 08-18-2006 04:11 PM
need help with a script to automatically create a subdirectory verbatim Linux - Newbie 6 04-26-2005 12:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:08 PM.

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