Bash Scripting with Example

Bash is a popular command-line interpreter for Linux computers including Mac OS X. Bash can execute a vast majority of Bourne shell scripts, mainly benefitting the administration and programming tasks. Many of the features were copied from sh, csh, and ksh. Bash is also like a programming language so you can write programs using bash usually to automate tasks. You can start writing scripts right away, wrap up multiline operations in one file, implement flow control, and interact with users to get input.

Here I would like to explain how bash scripting helps in mailing an attached file using SMTP.

a) Mailing particular file in a directory

#!/bin/bash
emailsend=fromemail@gmail.com
emailrec=toemail@gmail.com
password=password
name=toemail@gmail.com
/usr/local/bin/sendEmail -v -f “$emailsend” -s smtp.gmail.com -xu “$name” -xp
“$password” -t “$emailrec” -o tls=yes -u Message Bash script -m HIII MAIL FROM
BASH EXECUTE -a /home/Jinesh/Desktop/sendMailBash.sh

Explaining the script

1)sendEmail – Is an SMTP client that should be installed in Linux

2)emailsend – represents the sender’s email id

3)emailrec – represents the receiver’s email id

4)smtp.gmail.com – mail server

Options
-u : This option helps in providing a subject to mail
-m : helps in providing the text body for email
-a : helps in attaching a file in your local drive to email (multiple files can be attached)

########################## Required Packages ###############################

# yum install perl perl-CPAN perl-Net-SSLeay perl-IO-Socket-SSL (centos)

# “sudo apt-get install libio-socket-ssl-perl” and “libnet-ssleay-perl” (ubuntu)

###########################install sendEmail ############################

# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
# tar -zxvf sendEmail-v1.56.tar.gz
# sudo cp -a sendEmail-v1.XX/sendEmail /usr/local/bin
# sudo chmod +x /usr/local/bin/sendEmail
# sendEmail

b) Mailing multiple files in a directory

#!/bin/bash

for i in /home/Jinesh/Desktop/*.txt; do
files+=$i” ”
done
emailsend=fromemail@gmail.com
emailrec=toemail@gmail.com
password=password
name=toemail@gmail.com
/usr/local/bin/sendEmail -v -f “$emailsend” -s smtp.gmail.com -xu “$name” -xp
“$password” -t “$emailrec” -o tls=yes -u Message Bash script -m HIII MAIL FROM
BASH EXECUTE -a $files

Executing from Java

These bash scripts can be executed from java : –

String[] cmd = new String[]{“/bin/sh”, “path/to/script.sh”};
Process pr = Runtime.getRuntime().exec(cmd);

Schedule execution

Bash script execution can be scheduled at a particular time

command : – at 1748 < /home/Jinesh/Desktop/sample.sh (will execute sample.sh at 17:78). Here we used “at” command to schedule a bash script execution

We can also schedule bash script execution using crontab

This will periodically execute the script.

crontab -u root -e

This will open scheduled tasks in vi editor. We can modify or add task here.

Example: 10 11,16 18 sep thu /home/Jinesh/Desktop/senMailBash.sh >> /home/Jinesh/Desktop/backup.log 2>&1

10 – 10th Minute (Top of the hour)
11,16 – 11 AM and 4 PM
18 – date
sep – month
thu – day of the week

This will execute and mail an attachment on 18th September at 11:10, 16:10 and the output will be printed to a file say “backup.log”

crontab -u root -l

List all scheduled tasks

using *

* – Every Minute
* – Every hour
* – Every day
* – Every month
* – Every day of the week

tail -500f /var/log/cron : – view logs

Implementation

1) If one needs to get a server log every day/week/month/year to a mail address, where the server is placed in another location we can use this.

2) By implementing this, the user can send an email for a particular query result to any mail address (we can periodically schedule this).

Have questions? Contact the technology experts at InApp to learn more.