Archive for the ‘Shell Scripts’ Category

Send Mail with Attachment from the Commandline

November 23, 2011 Leave a comment

Under Linux, you can send some file “test.pdf” attached to an email by typing:

uuencode test.pdf test.pdf | mailx -s "Title for my email"

So what’s going on there? uuencode takes two input parameters: the file to read and the filename how it should appear at the mail. The file is converted to an ASCII-string and piped to mailx. The email is created with the given title (after the -subject flag) and then send to the given email address. The text-encoded file as piped from uuencode is attached to the body. Any modern email client is able to recognize this attachement.

The following script can be used to send a file including a subject to some fixed email. Some help is printed if it is called with the wrong number of parameters.

if [ $# -ne 2 ]; then
        echo "sendfile <filename> \"title\""
        exit 1
uuencode $1 $1 | mailx -s "$2"

Just save these lines in some file (e.g., sendfile) and make it executable (e.g., with chmod 755 sendfile).


Mounting an .iso file

October 30, 2011 Leave a comment

In Linux it is easy to access the content of some .iso-file directly without having to burn it to a CD/DVD. Just mount the .iso-file to some directory of your choice and you can browse it like any directory:

sudo mount <iso-file> <existing directory> -t iso9660 -o loop

The following script also prints some help if called with the wrong number of parameters and creates the directory if not yet existing:


# print help if called with wrong parameter count
if [ $# -ne 2 ]; then
        echo "USAGE:   isomount <isofile> <location>"
        echo "INFO:    Mounts the specified .iso-file to the given"
        echo "         directory location (if it is not existing then"
        echo "         it is created with user-rights)"
        echo "EXAMPLE: isomount MyFile.iso /tmp/fooooo"
        exit 1

# create directory if not existing and exit on error
if [ ! -d "$2" ]; then
        mkdir $2
        if [ $? -ne 0 ]; then
                exit 1

# mount the iso-file
sudo mount $1 $2 -t iso9660 -o loop

Just save these lines in some file (e.g., isomount) and make it executable (e.g., with chmod 755 isomount).

Categories: Computer, Shell Scripts Tags: ,