Riaan Lehmkuhl's Blog

Subversion, Progamming, Tips & Tricks and whatever else springs to mind.
20Aug

Clone of the Windows shell "start" command for Nautilus in Ubuntu

20 August 2010 11:33 by Riaan Lehmkuhl

If you used the Windows shell a lot, you would likely have used the "start" command to launch an explorer window from the command prompt. The same can be easily done with Nautilus in Ubuntu. Create a shell script with the following content (in my case /opt/bin/browse.sh):

echo "nohup nautilus $1 2> /dev/null > /dev/null &" > /opt/bin/browse.sh
nohup is used so that the new process runs in the background and all output is discarded.

Make the file executable:

chmod ug+x /opt/bin/browse.sh

To make the script accessible you can do one of two things:
Either add an alias to your .bashrc file

echo "alias browse='/opt/bin/browse.sh'" >> ~/.bashrc

or Add a symbolic link to the /usr/bin directory:

sudo ln -s /opt/bin/browse.sh /usr/bin/browse

Usage: browse [target-directory]

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
13Aug

Convert XML elements and attributes to lowerCaSe using awk

13 August 2010 21:50 by Riaan Lehmkuhl

Install xmlstarlet (download from http://xmlstar.sourceforge.net/ or on ubuntu sudo apt-get install xmlstarlet).

Let's look at a simple XML file (menu.xml):

<BREAKFAST_MENU>
<FOOD NAME="Belgian Waffles" PRICE="$5.95">
<DESCRIPTION>two of our famous Belgian Waffles with plenty of real maple syrup</DESCRIPTION>
<CALORIES>650</CALORIES>
</FOOD>
<FOOD NAME="Strawberry Belgian Waffles" PRICE="$7.95">
<DESCRIPTION>light Belgian waffles covered with strawberries and whipped cream</DESCRIPTION>
<CALORIES>900</CALORIES>
</FOOD>
</BREAKFAST_MENU>

Convert the XML to PYX:

xmlstarlet pyx menu.xml > menu.pyx

The new file will now look like this:

(BREAKFAST_MENU
-\n\t
(FOOD
ANAME Belgian Waffles
APRICE $5.95
-\n\t\t
(DESCRIPTION
-two of our famous Belgian Waffles with plenty of real maple syrup
)DESCRIPTION
-\n\t\t
(CALORIES
-650
)CALORIES
-\n\t
)FOOD
-\n\t
(FOOD
ANAME Strawberry Belgian Waffles
APRICE $7.95
-\n\t\t
(DESCRIPTION
-light Belgian waffles covered with strawberries and whipped cream
)DESCRIPTION
-\n\t\t
(CALORIES
-900
)CALORIES
-\n\t
)FOOD
-\n
)BREAKFAST_MENU

Now we need to convert the lines beginning with "(" and ")" to lower case, and the first word of the lines beginning with an "A" (except the first "A").

cat menu.pyx | awk '{if (/^\(/) print tolower($0); else if (/^\)/) print tolower($0); else if (/^A/) print substr($0, 1, 1) tolower(substr($0, 2, index($0, " ")-1)) substr($0, index($0, " ")+1); else print $0; }' > menu.tmp

We now have the converted PYX file and this can be convert back to XML

xmlstarlet p2x menu.tmp > menu.xml

That's it, we have the converted XML file:

<breakfast_menu>
<food name="Belgian Waffles" price="$5.95">
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food name="Strawberry Belgian Waffles" price="$7.95">
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
</breakfast_menu>

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
11Jun

The UNIX date command and Date manipulation

11 June 2008 22:44 by Riaan Lehmkuhl
You need to do something on a daily with a file in a directory named yyyy-mm-dd.txt (year-month-day.txt).
That's easy enough:
#!/bin/ksh
# just get the current date
today="`date +%Y-%m-%d`"
filename="${today}.txt"
echo $filename
Oops, you need the file from yesterday. Not so simple any more as the unix date command just sets or prints the date.
Let's do some of the usual date math (month ends, leap years, etc.).
#!/bin/ksh
# default is one to travel back in time to yesterday
daystogoback="1" 
# to be a little dynamic, we can use an argument to go back n days.
if [ $# -eq "1" ]; then
   daystogoback="$1"
fi
# get the current year month and day
nowy=`date +%Y`
nowm=`date +%m`
nowd=`date +%d`
# get rid of the month's possible leading zero
nowm=`echo $nowm*1|bc`
# if today's day is greater than the number of days to go back
# we really can just take the shortcut and subtract the days to go back
# because we will stay in the current month and nothing fancy is needed
if test $nowd -gt $daystogoback
then
        nowd=`echo $nowd-$daystogoback|bc`
else
# if we are in January, we have to go to the previous year 
# and set the month to December
# else we can just subtract the month
        if test $nowm -eq "1"
        then
                nowy=`echo $nowy-1|bc`
                nowm="12"
        else
                nowm=`echo $nowm-1|bc`
        fi
# now we can calculate the day, but first we have to see
# how many days in the month...
# we default to 31 and do nothing more for the months with 31 days
# we have to check for February and then for a leap year (28/29)
# the rest of the months will have 30 days
        newd="31"
        case "$nowm" in
                "1") ;;
                "3") ;;
                "5") ;;
                "7") ;;
                "8") ;;
                "10") ;;
                "12") ;;
                "2")    if test "0" -eq "`echo $nowy%4|bc`"
                                then
                                        newd="29"
                                else
                                        newd="28"
                                fi ;;
                *) newd="30" ;;
        esac
# subtract the current day from the days to go back 
# and then from the total days in the month
        nowd="`echo $daystogoback-$nowd|bc`"
        nowd="`echo $newd-$nowd|bc`"
fi
# now we just do some formatting for the month and day
if test $nowm -lt "10"
then
        nowm="0$nowm";
fi
if test $nowd -lt "10"
then
        nowd="0$nowd";
fi
# our new date
filedate="$nowy-$nowm-$nowd"
# and the file we need to access...
filename="${filedate}.txt"
echo $filename
Always fun playing with dates.

References:
KSH script BASICS
Wikipedia: Korn shell

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
31Dec

SvnPerms dot Net

31 December 2006 21:30 by Riaan Lehmkuhl
C# port of svnperms.py pre-commit hook script.

I've just posted my first article to The Code project.
Check it out at SvnPerms dot Net.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
29Dec

Remove the thousand separator from CSV files using AWK

29 December 2006 21:26 by Riaan Lehmkuhl
This will make fields like "11,368.35" look like this "11368.35".

awk -F, -v quo='"' '/"[0-9,.]*"/ { startq=0;str="";for (i=1;i<=NF;i++) {if($i ~ quo) { startq=!startq;sub(quo,"",$i)} str=str""$i; if(!startq && (i<NF)) str=str","} print str;next} {print}' OLDFILE > NEWFILE

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
29Dec

Convert Subversion Repositories from BDB to FS

29 December 2006 21:18 by Riaan Lehmkuhl
Having a lot of problems with 'wedged' repositories?
A shell script to convert SVN Repositories from BDB to FS.

Change the SVNBASE, SVNREPOS and SVNTEMP variables to point to where your directories are. #!/bin/bash E_WRONGARGS=65 E_DIRDOESNTEXIST=66 if [ -z "$1" ]; then echo "Usage: `basename $0` name_of_repository" exit $E_WRONGARGS fi echo "Creating $1 repository" # set up environment SVNBASE=/subversion SVNREPOS=$SVNBASE/repos SVNTEMP=$SVNBASE/temp if [ ! -d "$SVNREPOS/$1" ] ; then echo "`basename $0`: Can't open repository '$1': No such file or directory" exit $E_DIRDOESNTEXIST fi echo "dumping DBD repos $1" svnadmin dump $SVNREPOS/$1 > $SVNTEMP/$1.dmp echo "removing DBD repos $1" mv $SVNREPOS/$1 $SVNTEMP/$1 echo "creating FSFS repos $1" mkdir $SVNREPOS/$1 svnadmin create $SVNREPOS/$1 --fs-type fsfs echo "loading $1.dmp to FSFS repos" svnadmin load $SVNREPOS/$1 < $SVNTEMP/$1.dmp chown -R apache:apache $SVNREPOS/$1 chmod -R ug+rw $SVNREPOS/$1 # for SELinux chcon -R -h -t httpd_sys_content_t $SVNREPOS/$1 echo "Done."

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
29Dec

Linking Subversion changesets back to a Issue Tracking url

29 December 2006 21:03 by Riaan Lehmkuhl
Set the bugtraq: properties on a Subversion repository (recursively):


1. Check out your repository
2. cd to the root directory of your checkout
3. Run the following commands (note the dot at the end of each line)
svn propset -R bugtraq:label "IssueID:" .
svn propset -R bugtraq:url "http://your.issuetracker.url/?%BUGID%" .
svn propset -R bugtraq:message "IssueID: %BUGID%" .
svn propset -R bugtraq:number "true" .
svn propset -R bugtraq:warnifnoissue "true" .
svn commit -q -m "Added IssueID properties to the repository"
WINDOWS: You have to run them from the command line because a batch file will replace %BUGID% with nothing (trying to use the variable BUGID)

4. Verify when you are finished that the bugtraq:message is IssueID: %BUGID% and not just IssueID:

For more info, check out this guide which explains all the above.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
29Dec

Getting the pid of svnserve

29 December 2006 20:59 by Riaan Lehmkuhl

 

#!/bin/bash
the_pid=`ps -ef | grep svnserve | grep -v grep | awk '{print $2}'`
if [ -z "$the_pid" ]; then
echo "svnserve not running"
else
echo "svnserve running with pid $the_pid"
fi

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
29Dec

sed to convert CSV to Informix unload format

29 December 2006 15:46 by Riaan Lehmkuhl

After searching for ever, I finally managed to compile a sed script to convert CSV formatted files to Informix unload format (bar-delimited) files.

Hope I help someone with this.

#************************************************#
#                 csv2unl.sh                     #
#                                                #
#          written by Riaan Lehmkuhl             #
#                Dec  29, 2006                   #
#                                                #
#  sed to convert csv to bar-delimited records.  #
#************************************************#
# usage: ./csv2unl.sh < infile > outfile         #
#************************************************#
/./!d
s/\([^\]\)|/\1\\\\|/g
s/\`/\'/g
s/^ *\(.*[^ ]\) *$/|\1|/;
s/" *, */"|/g;
: loop
s/| *\([^",|][^,|]*\) *, */|\1|/g;
t loop
s/  *|/|/g;
s/|  */|/g;
s/^|\(.*\)|$/\1/;
s/"//g
s/$/|/g

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Riaan Lehmkuhl


Me, a disorder of the brain that results in a disruption in a person's thinking, mood, and ability to relate to others.

Recent comments

Comment RSS

Thingies

Calendar And Month List

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Disclaimer & Privacy

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Privacy:
We use third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

Most comments

Cool Quote

I know that you believe that you understood what you think I said, but I am not sure you realize that what you heard is not what I meant. - Robert McCloskey