Mint!!

   
 

Bash Script: Time, Tar, Date & Email

As im trying to learn bash scripting its easier if you have a little idea of a script that can be useful to you. For this example I want to backup a set of files/directories and have the script email me with the date, contents of the tar and the time it took to run the process…not as easy as it looks due to the “time command” outputting to stderr. The script can then be used as a cron job.

#!/bin/bash

set -e
set -u

DATESTAMP=`date ‘+%d_%m_%Y-%H%M’` #Date will appear in day_month_year-hour&min format
EMAIL=”user@uni.edu.au” #Define an email address for mail to use
SUBJECT=”Backup completed for PC-HTPC $DATESTAMP” #Create a subject for the mail including the date defined above
ARCHIVE=”backup_$DATESTAMP.tar.gz” #Create a name for the tarfile including the date defined above
BODY=`(time tar -cvpzf $ARCHIVE test*) 2>&1`#The tar command (including being timed) and redirecting stderr to stdout

echo -e “$ARCHIVE\n\n$BODY” | mail -s “$SUBJECT” “$EMAIL” #Using the variables archive and body and using the mail command to send the email

Using sed to remove lines

So you have the following list of email addresses in the syntax user@uni.edu.au and you want to strip out everything after the @ sign leaving you will a list of usernames. The user names need then to be on one line only.

So for example if the file called “email_list” looked like this:

test1@uni.edu.au
test2@uni.edu.au
test3@uni.edu.au

To get the first part working:

# cat email_list | sed ‘s/@.*//’

Meaning everything after the @ replace with nothing.

You will get a list like this:

test1
test2
test3

….but we want the output all on one line to be able to copy and paste it:

# echo $(cat email | sed ‘s/@.*//’)

Now you get:

test1 test2 test3

Bash Script 1.2

#!/bin/bash

set -e
set -u

# Removed the _SRC_ROOT variable and replaced with input defined by the user

if [ $# -ge 1 ] && [ -d $1 ]; then
_SRC_ROOT=$1
else
echo “No valid directory provided, please use ./src-sanity-check <directory>. Script is exiting”
exit 1
fi

_ACTION=all

if [ $# -ge 2 ]; then
_ACTION=$2
fi

function list_src_dirs {

find $_SRC_ROOT -type d -printf ‘%m\t%u\t%g\t%p\n’ 2>/dev/null | \
sed  ‘/\/__/d; /RCS$/d’

}

function list_src_dirs_perm_err {

list_src_dirs | sed -nr ‘/\t(source|sys)\t/p’ | sed -r ‘/^277[05]/d’ | sort -k 2

}

echo $_SRC_ROOT
echo $_ACTION

When you define a directory or any information along with the script its assigned a value, for example:

./src-sanity-check.sh /src/config/redhat

/src/config/redhat will be defined as $1

Example:

./src-sanity-check.sh /src/config/redhat xxx

xxx will be defined as $2

so:

if [ $# -ge 1 ] && [ -d $1 ]; then
_SRC_ROOT=$1

This means if the $# input is greater or equal to 1 and is a directory then assign the value of $1 to _SRC_ROOT (which can be used later)

else
echo “No valid directory provided, please use ./src-sanity-check <directory>. Script is exiting”
exit 1
fi

So, if a value is not assigned to $1 provide the error stated above and exit the script.

We then define a new variable:

_ACTION=all

and start the next if statement:

if [ $# -ge 2 ]; then
_ACTION=$2
fi

Meaning if the second input $2 is defined then assign this value to _ACTION

Currently at the end of the script the two values are called:

echo $_SRC_ROOT
echo $_ACTION

Obviously this will be changed later.

Bash Script 1.1

SCRIPT 1.1

#!/bin/bash

set -e
set -u

_SRC_ROOT=/src/config/redhat

function list_src_dirs {

find $_SRC_ROOT -type d -printf ‘%m\t%u\t%g\t%p\n’ 2>/dev/null | \
sed  ‘/\/__/d; /RCS$/d’

}

# Adding a second function to filter group and permissions

function list_src_dirs_perm_err {

list_src_dirs | sed -nr ‘/\t(source|sys)\t/p’ | sed -r ‘/^277[05]/d’ | sort -k 2

}

list_src_dirs_perm_err

Now we are using the first function (function list_src_dirs) in the next function list_src_dirs_perm_err, getting further into the output we actually need.

sed -nr ‘/\t(source|sys)\t/p’ (-n output the following filter -r extended sed) this line prints the lines which have source or sys as the group (word preceeded with a tab)

sed -r ‘/^277[05]/d’ – excludes the following numbers that start from the beginning of the line (^) 2770 or 2775, this is looking at the permissions octal output. Worth remembering that sed looks at the whole line as the output.

sort -k 2 – sorts the second key (second column) which in this case is the username, making the output in alphabetical order.

Bash Script 1.0

Ive started to learn bash scripting and have decided to document it on my blog. Its mainly for my reference but I guess it could also be useful for other beginners. Im not doing anything fancy so dont judge 🙂

The idea behind the script is to run on redhat source directories/files to identify which have the wrong user/group permissions and which files are checked out for longer than expected.

Ill put all the explanations in red so they are easy to read, they are currently not part of the script.

SCRIPT 1.0

#!/bin/bash

set -e #If an error occurs, the script will stop)
set -u #reports if there are any undefined variables)

# Define the variable for the search directory

_SRC_ROOT=/src/config

# Define a function to find the correct directories

function list_src_dirs {

find $_SRC_ROOT -type d -printf ‘%m\t%u\t%g\t%p\n’ 2>/dev/null | \
sed  ‘/\/__/d; /RCS$/d’

}

list_src_dirs

Explanation of the find command

type d (find directories only)

printf (prints to the screen)

%mDisplays permissions in octal format
%uDisplays owners username
%gDisplays group name
%pDisplays filename
\ttab between columns

2>/dev/null (sends any error messages to /dev/null, running the script without this we see the permission denied entries to the directories we cannot access)

sed  ‘/\/__/d; /RCS$/d’ (ignores directories starting __ and ending /RCS)

Enclose sed regular expressions in /
/d = delete from the output (/p will print to the screen)

uTorrent Error: Access is Denied (Windows 7)

Ive recently seen an error in uTorrent when downloading a specific file getting the error “Access is denied”. Strangely it was only for one file, all others seem to be working ok. It appears there are some compatibility error with uTorrent and Windows 7.

To resolve the error complete the following:

1: Right click on the utorrent.exe file and select “Properties”
2: Select the   “Compatibility” tab
3: Check “Run this program in compatibility mode for:” and select “Vista” from the drop down menu
4: Select “Run this program as administrator” under “Privilege Level” at the bottom of the box.
5: Restart utorrent and you should be all good!

How to launch Boxee with MCE Remote (Windows)

Found a neat little install for launching Boxee with your MCE remote green button (usually launches WMC). Simply download and install!

Launching Boxee with a Windows Media Center Remote

Thanks Jacob Johnston!

Forgotten User Password in Vista?

Had a call from my mate over the weekend who had forgotten his user password in Windows Vista. Luckily he was still able to get into his account as he has fingerprint entry enabled, however he still couldnt remember his password. Searching around on the web there are a few solutions but I found this one the easiest:

1: Create a new “Administrator” account
2: Switch to this account
3: Go to Start, then in the run box enter “control userpasswords2”
4: A dialogue box will open, select the user account you want to change the password of and change it! You will not need your current password to do this 🙂
5: Switch back to the troublesome account and test your new password!
6: You can delete your temporary admin account now if so desired!

How launch XBMC with MCE Remote “Home” Button on Ubuntu

Having recently installed ubuntu on my HTPC I was keen to get my Microsoft MCE Remote Control working. Initially this is easy to setup with LIRC but its a little trickier to get the buttons mapped the way you want. There is a lot of documentation on the web which doesnt seem to work.

I following the guide here with a few tweaks of my own: http://www.hackourlives.com/setup-windows-media-center-remote-for-mythtv-and-xbmc/

Im only interested in launching XBMC with the “Home” button on the remote, the power button will close XBMC with the default setup.

My setup:

Microsoft MCE Remote with USB IR Receiver
HTPC: Dell Hybrid
Version of ubuntu:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION=”Ubuntu 9.10″

Here are the steps:

1: Install the LIRC package which supports IR remotes for linux

$ sudo apt-get install lirc lirc-modules-source

You will then next to select the correct remote in the GUI which pops up.

2: For basic setup for media centre remote integration with xbmc and mythtv etc install the following:

$ sudo apt-get install mythbuntu-lirc-generator

and run in a terminal:

$ mythbuntu-lircrc-generator

According to the source this creates a .lirc directory and .lircrc file. However in my experience it created a txt file .lirc and no .lircrc. I removed the txt file .lirc and created a directory .lirc and txt file .lircrc in my home account

$ mkdir .lirc
$ touch .lircrc

3: Ensure the remote/receiver is now working, in a terminal window:

$ irw <return>

Now press some buttons on the remote such as “Home” and “Play” and you should see the button codes | names | mceusb

4: Now cd into your .lirc directory and create these two files

$ touch irexec
$ touch irxevent

5: Edit the irexec and enter the following:

# Start XBMC
begin
prog = irexec
button = Home
config = ~/scripts/startXBMC.sh &
end

6: Make another directory in your home account called scripts

$ mkdir scripts

7: cd into this directory and make two files, one called startIRexec.sh & another called startXBMC.sh

8: Edit startXBMC.sh and enter the following:

#!/bin/bash

# Test to see if XBMC is running first
if ps -ef|grep -v grep|grep -i xbmc.bin
# if ps -ef|grep -v grep|grep -i mythfrontend.re
then
# Do nothing
echo “XBMC already Running!”
else
# Startup MythTV
xbmc
fi
exit

9: Edit startIRexec.sh and enter the following:

#!/bin/bash

# Test to see if IRXevent is running first, if so kill it, then restart
if ps -ef|grep -v grep|grep -i irxevent
then
ps aux|grep -i user_name|grep -i irxevent |awk ‘{print $2}’|xargs kill
else
# Do nothing
echo “irxevent already dead!”
fi

# Test to see if IRexec is running first, if so kill it, then restart
if ps -ef|grep -v grep|grep -vi start|grep -i irexec
then
ps aux|grep -i user_name|grep -i irexec |grep -vi start|awk ‘{print $2}’|xargs kill
else
# Do nothing
echo “irexec already dead!”
fi

#test to see if an instance of irxevent is already running
if ps -ef|grep -v grep|grep irxevent
then
# do nothing
echo “irxevent already running”
else
# start irxevent
irxevent ~/.lircrc &
fi

#test to see if an instance of irexec is already running
if ps -ef|grep -v grep|grep irexec
then
# do nothing
echo “irexec already running”
else
# start irxevent
irexec -d ~/.lircrc &
fi

exit

10: Edit the startIRexec.sh file and replace all user_name entries with your user name.

11: Make sure both scripts are executable:

$ chmod +x *.sh

12: Add the following lines to the .lircrc file in your home account

include ~/.lirc/irexec
include ~/.lirc/irxevent

13: Manually execute the startIRexec.sh script

$ ~/scripts/startIRexec.sh

With any luck you should now be able to launch XBMC with the “Home” button on the MCE remote 🙂

To ensure that the irexec script starts when you login:

System > Preferences > Startup Application

The application to launch is /home/username/scripts/startIRexec.sh

Enjoy your new remote!