Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20
Like Tree9Likes
Ok, I have this super basic script that will email me a file from our server. I created an alias for this so all I have to type is Code: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    185

    creating a random message


    Ok, I have this super basic script that will email me a file from our server. I created an alias for this so all I have to type is
    Code:
    email $file
    and within ten seconds I have the file in my inbox. Here is my email script
    Code:
    #!/bin/ksh
    #set -x
    uuencode $1 $1.txt | mail -s "Here is the file you requested." mactruck@mail.com
    #end
    The code works great but the email is boring. I would like to have it create a random subject when sending the email. I did some researching and I see there is a random command for numbers but is there a way I can create a list of messages it will randomly pick a message and send that?

  2. #2
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    886
    You can create an array containing the subject lines you wish. Next, generate a random number between 0 and the number of lines in the array. Select the entry with the index that matches that number.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,517
    Not hard with a bash script - not sure if ksh will work the same way.

    1. Create the text file, one saying per line.
    2. Count the number of lines.
    3. Compute a random number that is between 0 and the number of lines - and read that line from the file.

    The bash code compute the line is something like this:
    Code:
    numberoflines=`cat filename | wc -l`
    linetoget=$[ ( $RANDOM % $numberoflines ) ]
    You would add the code to read up to the line to get and then use that for the message subject.
    mactruck likes this.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    185
    Hey Rubberman, That only generated a random number and not the message. I will keep playing with it.

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,517
    Quote Originally Posted by mactruck View Post
    Hey Rubberman, That only generated a random number and not the message. I will keep playing with it.
    Hey, I thought you'd like to solve part of the problem! Such as skipping through the file to the line number given! You know the expression about feeding a person a fish and they eat for a day, but teaching them to fish, and they will eat for a life time?
    mactruck likes this.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    185
    That expression is so true.. I'm going to mess around with it at home this weekend and see what I can come up with. I did switch it bash and not ksh.

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,517
    Quote Originally Posted by mactruck View Post
    That expression is so true.. I'm going to mess around with it at home this weekend and see what I can come up with. I did switch it bash and not ksh.
    Fair enough. Let us know how it works out, and we will be happy to kvetch, or to help some more.
    mactruck likes this.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    185
    OK, I played around with this a little bit and for some reason I still can't get it to work. It will email me but for the random message it gives me a number. (the number is random so that's kinda good)

    Code:
    #!/usr/bin/bash
    #
    set -x
    #
    #
    numberoflines=`cat /notp/MIKE/scripts/subject.email | wc -l`
    linetoget=$[ ( $RANDOM % $numberoflines ) ]
    
    
    uuencode $1 $1.txt | mail -s "$linetoget" mactruck@home.com

  9. #9
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,517
    That's because the variable $linetoget is just a number, not the actual line from the file specified. You need one more expression to extract that line from the file /notp/MIKE/scripts/subject.email
    mactruck likes this.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  10. #10
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    185
    I have been pondering it and I am admitting defeat.... Can you help me with this missing expression?

Page 1 of 2 1 2 LastLast

Posting Permissions

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