Archived : Techno Babble

This is the archived version of my b2evolution code blog.
If you require any help regarding b2evolution then
visit it's support forums

You can find my current blogs here
Code : {@link : WafflesOn}
Personal : {@link : InnerVisions}

Bourne to Bash

Posted on 13th Apr 2008 in : Techno Babble

Not all shells are on a beach

I decided to have a play with Bash this weekend as I'd been asked if I could come up with a backup process that was simple enough for the average joe blogs to configure to suit their needs. The script needed to be able to do daily/weekly/monthly backups of selected databases and files/folders and store them on a different server, as a nice touch it also emails the user to let them know that the backup had been done and which databases/files/folders had been included. As well as doing the backups it also keeps the latest 3 in a grandfather/father/son rotation which gives the user 9 potential backups which they can recover from.

As I haven't had a shedload of experience in using bash I spent a fair amount of time hunting round the web looking for clues as to how to do some bits and bats so I thought I'd share some snippets as some of them took a fair amount of searching for/solving and you never know, it may just help someone else in the future ..... probably me :p

Bash:

#!/usr/bin/env bash
#
# Read a named file into a variable
#
 
# read "file_you_want" into $variable_name
contents=$(<foo.txt)
 
echo "${contents}"
 
#
# Read a variable file into a variable
#
 
# set $variable_name = "file_you_want"
my_file='/path/foo.txt'
 
# read file $variable_name into $variable_name
contents=$(<"${my_file}")
 
echo "${contents}"

Bash:

#!/usr/bin/env bash
#
# Convert windows line endings to linux line endings
#
 
# set $variable_name = "file_you_want_to_convert"
filename='/path/foo.txt'
 
# remove all \r and save the output to $variable_name.linux
tr -d '\r' < ${filename} > ${filename}.linux
 
# remove original file
rm ${filename} -f
 
# rename $variable_name.linux to $variable_name
mv ${filename}.linux ${filename}

Bash:

#!/usr/bin/env bash
#
# check if a process is already running
#
 
# name of the process to check for
# add [] around the first letter to stop the "grep process" from showing
check_process='[f]oo'
 
if ps aux | grep -q "${check_process}"
  then
    echo 'running'
  else
    echo 'not running'
fi

Bash:

#!/usr/bin/env bash
#
# switch between 2 directories
#
 
# switch to directory 1
cd /path/foo/
 
# show current directory
pwd
 
# switch to directory 2 and "remember" previous directory
pushd /path/bar/
 
# show current directory
pwd
 
# switch back to directory 1
popd
 
# show current directory
pwd
 
# alternative method
 
# switch to directory 1
cd /path/foo/
 
# show current directory
pwd
 
# switch to directory 2 and "remember" previous directory
pushd /path/bar/
 
# show current directory
pwd
 
# switch back to directory 1
pushd
 
# show current directory
pwd
 
# switch back to directory 2
pushd
 
# show current directory
pwd

Bash:

#!/usr/bin/env bash
#
# send an email using echo
#
 
echo 'your email content' | mail -s 'your email subject' email_1@domain.com email_2@domain.com
 
#
# send an email using a file as content
#
 
mail -s 'your email subject' email_1@domain.com email_2@domain.com < email_content.txt
 
#
# send an email using a variable file for content
#
 
# set $variable_name = "file_you_want_to_use_as_content"
email_body='/path/foo.txt'
 
mail -s 'your email subject' email_1@domain.com email_2@domain.com < ${email_body}
 
#
# send an email using a file and replace "%placeholders%" with variables
#
 
# set $variable_name = "file_you_want_to_use_as_content"
email_body='/path/foo.txt'
 
# set $variable_name = "replacement_value"
foo='bar'
 
# replace the placeholders and email the results
# replaces %placeholder% with $foo
sed -e "s!%placeholder%!${foo}!;" ${email_body}" | mail -s 'your email subject' email_1@domain.com email_2@domain.com

Bash:

#!/usr/bin/env bash
#
# find the directory your script lives in
#
 
foo= `dirname $0`
echo $foo

¥

Tags: bash, snippets
Page archived : 11th Nov 2009
 

X