Bash

From Blue-IT.org Wiki

Revision as of 17:26, 9 January 2014 by Apos (talk | contribs) (mailx - former known as nail)

Environment

Put this in front of any script to avoid problems when running the script in other languages! The en_US locale should be avaiabel on all systems:

export LC_MESSAGES="en_US.UTF-8"
export LC_TYPE="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8


Sending mails

mailx - former known as nail

Tested in

  • Ubuntu 12.04
  • Ubuntu 14.04

Inspired by

sudo apt-get install ca-certificates heirloom-mailx msmtp
vim ~/.msmtprc
# config options: http://msmtp.sourceforge.net/doc/msmtp.html#A-user-configuration-file
defaults
logfile /tmp/msmtp.log

# isp account
account gmx
auth on
host mail.gmx.de
port 587
user username@gmx.de
from username@gmx.de
password PASSHERE
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
 
# set default account to
account default: gmx
vim ~/.mailrc
# set smtp for nail
# ref: http://ubuntuforums.org/showpost.php?p=4531994&postcount=6
# docs: http://msmtp.sourceforge.net/doc/msmtp.html#Configuration-files

# isp account (default)
# $ nail -s "subject line" -a /path/file recipient@email.com < /path/body.txt
set from="username@gmx.de"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmx"

# gmx account
# $ nail -A gmail -s "subject line" -a /path/file recipient@email.com < /path/body.txt
account gmx {
set from="username@gmx.de (Your name) send from command line"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmx"
}

Send mail

mailx -A gmx -s "gmx test" username@gmx.de < /tmp/test_email
echo "mail text" | mailx -A gmx -s "gmx test" username@gmx.de

Sending dmesg message (or other) via pipe

dmesg | mailx -A gmx -s "$HOSTNAME dmesg" username@gmx.de

Explanation:

-A xxx    : the name of the isp account in ~/.msmtprc AND .mailrc
-s "Text" : the subject

Singleton

The singleton pattern is a very handy one. You can realise it in bash like this:

if ps x | grep -v grep | grep -v $$ | grep $0 | grep -v subl | grep -v vi
then
        echo "$0 already running. Exiting"
        exit 1
else

################################################
# PUT YOUR CODE HERE
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

./run_me

# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
################################################ 
 
fi

Grep

Grep a steam with tail, head

Sometimes you need not to filter a standard out only once, but with tail

tail -f -n 300 /var/log/syslog | stdbuf -o0 grep PATTERN

Graphical Tools

Sometimes you need to get out of yor script, give a message to your users - visually.

notify-send

See Ubuntu_Desktop#Notify_OSD

#!/bin/bash
 
notify_start() {
    aplay Critical_Error.wav
   
    notify-send   "Backup is is mounted." \
                -i /usr/share/icons/gnome/48x48/actions/document-open-recent.png \
                "Read and write support for you is working."
}

notify_end() {

    notify-send   "Backup Drive is not ready!" \
                -i /usr/share/icons/gnome/48x48/actions/stock-delete.png \
                "Resolve the problem and save your work"
}


while(true); do

	if touch /backup/testfile
	then
		rm /backup/testfile
		notify_start
	else
		notify_end
	fi

sleep 10

done


Monitoring

Sometimes you like to run a monitor in the background. Here an example for checking if a drive had an drive error.

#!/bin/bash

if ps x | grep -v grep | grep -v $$ | grep $0 | grep -v subl | grep -v vi
then
        echo "$0 already running. Exiting"
        exit 1
else

################################################
# PUT YOUR CODE HERE
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

 notify_start() {

     aplay /local/share/sounds/KDE_Critical_Error.wav
    
     notify-send   "Backup is is mounted." \
                 -i /usr/share/icons/gnome/48x48/actions/document-open-recent.png \
                 "Read and write support for you is working."
 }
 
 notify_end() {
 
     aplay	/local/share/sounds/KDE_Critical_Error.wav

     notify-send   "Backup Drive is not ready!" \
                 -i /usr/share/icons/gnome/48x48/actions/stock-delete.png \
                 "Resolve the problem and save your work"
 }

while(true); do

	message_head="SSD WRITE TEST:"
	message_time="date +%F_%Hh:%Ms:%Nms"
	error="ERROR:"
	
	logger "${message_head} $(${message_time}) starting sync"

	if sync
	then
		logger "${message_head} $(${message_time}) sync finished successfully."
	else
		logger "${message_head} $(${message_time}) ${error} while do sync."
	fi

	sleep 2

 	
	if touch ~/testfile
 	then
		logger "${message_head} $(${message_time}) touched testfile in home successfully."

 		if rm ~/testfile 
		then 
			logger "${message_head} $(${message_time}) removed testfile in home successfully."
		else
			logger "${message_head} $(${message_time}) ${error} while removing tesfile in home."
		fi

 	else
		logger "${message_head} $(${message_time}) ${error} while touching tesfile in home."
 		
		notify_end
 	fi
 
sleep 28
 

done

# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
################################################ 
 
fi