Results 1 to 5 of 5

Thread: [Shell Scrip] Email admin with attachment

  1. #1
    Join Date
    Jul 2009
    Location
    ireland
    Beans
    6
    Distro
    Ubuntu 10.10 Maverick Meerkat

    [Shell Scrip] Email admin with attachment

    Hi all,

    I hope I am in the right topic. Is there anyone available to take a look at my script and tell me if something is wrong or if something needs to be improved?

    Thanks in advance for your answer. It is my first script. Here is the subject:

    1. Write a shell script called /tmp/shellscript.sh
    2. Have the shell script gather the current memory configuration and CPU information
    3. Write that data to a file called /tmp/memory
    4. In the script, write out to the /tmp/users file with current-logged-in users information (you could the w command to gather this information)
    5. When running the script, pass a single parameter to it which will determine how many many itterations of top to run. With that information passed to the script, run top in batch mode with the number of iterations passed to it on the command line. For example, when you type shellscript.sh 2, it will run top 2 times. Output the top data to a file called /tmp/top
    6. Create a tar file of the /tmp/memory, /tmp/users, and /tmp/top files, and name the file with today's date, as in this format: tarfile-15092013.tgz. The date of the file must be notated as a VARIABLE in the script
    7. The last entry in the script should have the tar file emailed to the local root user with the subject line "tarfile attached". Run the script once to generate the file.

    Code:
    #!/bin/sh
    
    VARIABLE=`date +%d%m%Y`
    
    clear
    cat /proc/cpuinfo /proc/meminfo > /tmp/memory
    w > /tmp/users
    top - n $1 -b > /tmp/top
    tar -czPf tarfile -$VARIABLE.tgz /tmp/memory /tmp/users /tmp/top
    
    echo "File generated by script" | mutt -s "tarfile attached" -a "tarfile.$VARIABLE.tgz" root

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: [Shell Scrip] Email admin with attachment

    Thread moved to Programming Talk.

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: [Shell Scrip] Email admin with attachment

    You didn't mention what shell are you using. /bin/sh usually is a (sym)link to a POSIX compliant shell. In Ubuntu is a symlink to dash and in some other distros is a symlink to bash. If you invoke bash with the name sh, then it will run POSIX mode. See more here: http://wiki.bash-hackers.org/scripti...cs#the_shebang

    The back-quote is used in old-style command substitutions. The POSIX syntax is preferred: $()
    See: BashFaq 082 (link in my signature).

    Environment variables and internal shell variables, by convention, are fully capitalized. All other variable names should be lowercase ( or at least contain one lowercase letter ). This convention avoids accidentally overriding environmental and internal variables.

    Use more quotes , protect your strings and parameter expansions (check your tar command) from word splitting. Word splitting will eat your babies if you don't quote things properly. See: http://mywiki.wooledge.org/WordSplitting and http://mywiki.wooledge.org/Quotes

    Add (more) comments to your script. Comments aren't processed by the shell, but they are processed by human brains. This is obviously a homework assignment, so your teacher will be impressed.


    See also: http://mywiki.wooledge.org/BashGuide/Practices
    Last edited by sisco311; September 17th, 2013 at 12:29 AM.

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: [Shell Scrip] Email admin with attachment

    • Looks a lot like homework...
    • Unless it's a hoax because I wouldn't keep a script I'm working on in /tmp where it runs the risk of being cleaned up automatically at next reboot.
    • Starting a script with "#!/bin/sh" means it wont be executed by the regular shell (bash). This can create plenty of subtle problems.

  5. #5
    Join Date
    Jul 2009
    Location
    ireland
    Beans
    6
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: [Shell Scrip] Email admin with attachment

    ok thanks all. A friend of mine gave me his linux lab manual, but as there's no solution on it, I am trying to do my best to go through all questions )).
    Sisco311, I will take a deep look at the links you gave me, and I will be back if I have further questions.

Bookmarks

Posting Permissions

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