Results 1 to 12 of 12

Thread: [Linux] Scripting Help Required

  1. #1
    Senior Member Workaholic's Avatar
    Join Date
    Oct 2004
    Location
    Manchester
    Posts
    1,500
    Thanks
    187
    Thanked
    14 times in 12 posts

    Question [Linux] Scripting Help Required

    Basically I work in Retail as a worker on a part time contract (and a full time one soon) but always forget when I'm in and for how long, due to changing rotas everyday.

    Basically work uses Fedora Core 4 (I think it's 4 and not 5) and I found a tool that could send home (and to other staff's) email address a rota file as long as it was stored in a particular file. The problem is that every week the newest rota gets saved to a newer file and so my script will need to be changed every week or so. At the moment the tool that I'm going to use is "mutt" and I was wondering if you guys could help me find:

    * Any tool that would locate the last modifed files in a particular location within a certain time (i.e. the last week?)
    * Any tool that would schedule the email sending automatically say Saturday/Sunday mornings.

    The files are located in this sytyle of filestore:
    \home\user\Desktop\Rotas\yearNumber\Month.xls (xls is the format as the files are in a spreadsheet file created by OpenOffice.)

    Thanks in advance
    Woohoo now Assistant Manager!


  2. #2
    Senior Member Workaholic's Avatar
    Join Date
    Oct 2004
    Location
    Manchester
    Posts
    1,500
    Thanks
    187
    Thanked
    14 times in 12 posts
    BTW could a mod add a poll to this so that I could see how good this idea would be to other members working on different rotas each day and week.

    Thanks
    Woohoo now Assistant Manager!


  3. #3
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    cron is the peroidic scheudular, ie a peroid been every saturday.

    you create a "cron tab" which tells cron about your task, and ask it to run a script,

    then you concatinate the path like so "date %B" will give you the month in a nice happy format so you have

    filepath="\home\user...."
    month=$(date +%B)
    month=$month."xls"

    isn't that horrible!
    throw new ArgumentException (String, String, Exception)

  4. #4
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    Quote Originally Posted by TheAnimus View Post
    cron is the peroidic scheudular, ie a peroid been every saturday.

    you create a "cron tab" which tells cron about your task, and ask it to run a script,

    then you concatinate the path like so "date %B" will give you the month in a nice happy format so you have

    filepath="\home\user...."
    month=$(date +%B)
    month=$month."xls"

    isn't that horrible!
    And largely pointless.

    Write the following script:

    Code:
    #!/bin/bash
    /bin/echo "Latest quota is attached." | /usr/bin/mutt -s"Latest quota" -a$(/bin/date +/home/user/Desktop/Rotas/%Y/%B.xls) someem@iladdress
    Save it as something like /home/user/sendrota, and make it executable (chmod a+x /home/user/sendrota)

    Then, type "crontab -e" and add the following line:

    Code:
    0 8 * * 7 /home/user/sendrota
    Which will execute the sendrota script at 8am every Sunday.
    Last edited by directhex; 27-09-2006 at 10:07 PM.

  5. #5
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    Actually, now I think of it, I'm barking up the wrong tree. The above will only send the current month's file. Try:

    Code:
    #!/bin/bash
    for i in $(find /home/user/Desktop/Rotas/ -type f -mtime -7)
    do 
    j="$j -a$i"
    done
    /bin/echo "Latest quotas attached." | /usr/bin/mutt -s"Latest quota" $j someemail@ddress
    which will send all files in the rotas folder modified within the past 168 hours
    Last edited by directhex; 27-09-2006 at 10:10 PM.

  6. #6
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    damn! i got served!

    just spent the last 5 min in the hope of finding some typo in hex's script, but it should work fine
    throw new ArgumentException (String, String, Exception)

  7. #7
    Senior Member Workaholic's Avatar
    Join Date
    Oct 2004
    Location
    Manchester
    Posts
    1,500
    Thanks
    187
    Thanked
    14 times in 12 posts
    Quote Originally Posted by directhex View Post
    Actually, now I think of it, I'm barking up the wrong tree. The above will only send the current month's file. Try:

    Code:
    #!/bin/bash
    for i in $(find /home/user/Desktop/Rotas/ -type f -mtime -7)
    do 
    j="$j -a$i"
    done
    /bin/echo "Latest quotas attached." | /usr/bin/mutt -s"Latest quota" $j someemail@ddress
    which will send all files in the rotas folder modified within the past 168 hours
    Do I still need to use crontab with the above code?

    Quote Originally Posted by directhex View Post
    And largely pointless.

    Write the following script:

    Code:
    #!/bin/bash
    /bin/echo "Latest quota is attached." | /usr/bin/mutt -s"Latest quota" -a$(/bin/date +/home/user/Desktop/Rotas/%Y/%B.xls) someem@iladdress
    Save it as something like /home/user/sendrota, and make it executable (chmod a+x /home/user/sendrota)

    Then, type "crontab -e" and add the following line:

    Code:
    0 8 * * 7 /home/user/sendrota
    Which will execute the sendrota script at 8am every Sunday.
    Woohoo now Assistant Manager!


  8. #8
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    Quote Originally Posted by Workaholic View Post
    Do I still need to use crontab with the above code?
    yes. cron handles scheduled tasks. it's just a slightly different task that you schedule. the same crontab line should be correct.

  9. #9
    Senior Member Workaholic's Avatar
    Join Date
    Oct 2004
    Location
    Manchester
    Posts
    1,500
    Thanks
    187
    Thanked
    14 times in 12 posts
    Any chance you can explain how the last script works?

    My bash scripting skills are crap.
    Woohoo now Assistant Manager!


  10. #10
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    bash replaces things inside $() with the output of the command. in the above instance, it replaces it with the output of "find /home/user/Desktop/Rotas/ -type f -mtime -7", which results in a list of all files (not folders) modified within the past 7 days, inside the path /home/user/Desktop/Rotas/

    this results in, after substitution, something like:
    Code:
    for i in /home/user/Desktop/Rotas/file1 /home/user/Desktop/Rotas/file2 /home/user/Desktop/Rotas/file3
    do 
    j="$j -a$i"
    done
    /bin/echo "Latest quotas attached." | /usr/bin/mutt -s"Latest quota" $j someemail@ddress
    the for loop means that the bit between do and done is repeated, with $i being set to each of the values in the list, in turn. the loop is trivial, simply adding bits to a string, and storing it in $j (which is empty to start with):
    " -a/home/user/Desktop/Rotas/file1"
    " -a/home/user/Desktop/Rotas/file1 -a/home/user/Desktop/Rotas/file2"
    " -a/home/user/Desktop/Rotas/file1 -a/home/user/Desktop/Rotas/file2 -a/home/user/Desktop/Rotas/file3"

    so now we have a string, $j, which contains as many " -a/some/full/path/to/file" snippets in a line as appropriate. "-a/file/path" is a mutt flag for "attachment". repeat multiple times to do it multiple times. lo and behold, the last line calls "mutt -s"Message subject" $j recipient@ddress" - remember, $j is replaced with all your attachments. The only final bit is that mutt requires the message body to be fed in through standard input - the two ways to do this are a pipe "command_which_generates_text | mutt recipient@ddress" or "mutt recipient@ddress < some_file_name_containing_input_text". with neither of these, mutt asks you to type your message interactively, which obviously we don't want

  11. #11
    Senior Member Workaholic's Avatar
    Join Date
    Oct 2004
    Location
    Manchester
    Posts
    1,500
    Thanks
    187
    Thanked
    14 times in 12 posts
    Quote Originally Posted by directhex View Post
    Code:
    #!/bin/bash
    for i in $(find /home/user/Desktop/Rotas/ -type f -mtime -7)
    Thanks so far, could you explain what the top line does and what i simplifies? Does it mean for every file in location x with a last modified timestamp of 7 days?

    Cheers
    Woohoo now Assistant Manager!


  12. #12
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    Quote Originally Posted by Workaholic View Post
    Thanks so far, could you explain what the top line does and what i simplifies? Does it mean for every file in location x with a last modified timestamp of 7 days?

    Cheers
    scripts in *nix, if marked executable, need to say with which interpreter they should be loaded - /bin/bash, /bin/ksh, /usr/bin/perl, etc. that's what the first "shebang" line indicates.

    and you're pretty much correct on the second point - every file (not folder) in x location with a last modified timestamp of 7 days or less

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. HTPC advice required
    By squishty.wibble in forum PC Hardware and Components
    Replies: 6
    Last Post: 02-10-2006, 11:25 AM
  2. Required speed for video?
    By sidcos in forum Networking and Broadband
    Replies: 2
    Last Post: 29-05-2006, 08:17 PM
  3. Kitchen extractors fans, are they required by law?
    By TheAnimus in forum General Discussion
    Replies: 11
    Last Post: 22-05-2006, 08:55 AM
  4. Advice Required
    By just_laze in forum Help! Quick Relief From Tech Headaches
    Replies: 2
    Last Post: 24-01-2006, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •