Only fill this in if you're a spammer huh? Your name :
Your email :
Your message :
Apr
13th
2008

Bourne to Bash

Posted 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

Code:

#!/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}"

Code:

#!/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}

Code:

#!/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

Code:

#!/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

Code:

#!/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

Code:

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

¥

Tags: bash, snippets

Trackback URL (right click and copy shortcut/link location)

Trackback url for this post
Note : This trackback url is time limited so use it within 30 minutes or the SpamHound will snarl at you.

http://waffleson.co.uk/z/?k=37032172633103238918f9b2f59f69081ea685b1d33940c85

Gary users avatar

Posts : 323

Joined : 04/04/05

Reply to comment 14641 by Gary

Replied on : 04/13/08 @ 06:46 pm #1

I am sure you understand it, it just flew over my head.
Good luck with future bashes ;)

Gz

Blog-a-doodle-do :D  http://garysblog.co.uk

 
 

Replied on : 04/13/08 @ 09:58 pm #2

Korny, dude! (You must've bashed in a previous life, cause methinks yer "Bourne Again" to BASH) :p

Time to go pick some c-shells off the beach.

Later.
 
 

Leave a comment

Your email address will not be displayed on this site.
Your URL will be displayed.
About you

Just fill in the bits you want, none of it is required



Your email address will only be used to send you adverts for viagra and rolex watches.



(Line breaks become <br />)
Leave a comment
Code:
=> :!: :?: :idea: :) :D :p B) ;) :> :roll: :oops: :| :-/ :( >:( :'( |-| :>> ;D :)) 88| :lalala: :crazy: >:XX :o