Difference between revisions of "Bash"

From Blue-IT.org Wiki

(Environment)
Line 35: Line 35:
 
* Source: http://stackoverflow.com/questions/7161821/how-to-grep-a-continuous-stream
 
* Source: http://stackoverflow.com/questions/7161821/how-to-grep-a-continuous-stream
  
 +
 +
== 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
  
  
 
[[Category:Network]]
 
[[Category:Network]]
 
[[Category:Bash]]
 
[[Category:Bash]]

Revision as of 08:38, 8 January 2014

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


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 and 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