Tag Archives: Linux

bash_example

Bash Example Script

These days I use bash more and more often for simple tasks on my Linux system, as well as for a University course I am taking.

Have a look at the script:

#!/bin/bash
# Skript: bash_example.sh
# Credit to: http://www.pro-linux.de/artikel/2/111/
#1,ein-shellskript-template.html
#
# I have added some usefull functions that I am using in many
# of my scripts
# Find this script here: http://phynformatik.de
# Janosch Maier
#
# This work is licensed under the
# Creative Commons Attribution-Share Alike License
#
# You are free
# to Share — to copy, distribute and transmit the work
# to Remix — to adapt the work
# to make commercial use of the work
#
# Under the following conditions:
# Attribution — You must attribute the work in the manner
# specified by the author or licensor (but not in any way
# that suggests that they endorse you or your use of the work).
# Share Alike — If you alter, transform, or build upon this
# work, you may distribute the resulting work only under the
# same or similar license to this one.
version="0.1"
lastChange="14.12.2011"

#### CONFIG - DO NOT MAKE CHANGES HERE ####
# PATH has to be a trusted value
# Try to make this script save in unsave environments
# "." cannot be in PATH
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin
export PATH

# Environment variable IFS should always be
# "" or undefined
# Same as with PATH
if [ -z "$IFS" ]
then
        IFS="
"
        export IFS
fi

#### YOU CAN START MAKING CHANGES FROM HERE ON ####
# Define global variables for later use
# Name of the script is its filename (without possible suffix .sh)
SCRIPTNAME=$(basename $0 .sh)
# Define Exit values
EXIT_SUCCESS=0
EXIT_FAILURE=1
EXIT_ERROR=2
EXIT_BUG=10
# Logfile will be put under current working directory
LOGFILE=`readlink -f "$SCRIPTNAME.log"`

# Error values
ERROR="ERROR"
WARNING="WARNING"
INFO="INFO"
NOTICE="NOTICE"

# Default values for options
VERBOSE=false
DEBUGLEVEL=1

#### FUNCTIONS FOR LATER USE ####
# Print usage message
function usage {
	echo "Usage: $SCRIPTNAME [-h] [-v] [-d debuglevel] file ...

  -h		show this help
  -v		be verbose
  -d		set debug level (1-9)
	[[ $# -eq 1 ]] && exit $1 || exit $EXIT_FAILURE
}

# Log stuff to the defined logfile
# Call "log $ERROR|$WARNING|$INFO|$NOTICE message"
function log {
	case $1 in
		$ERROR)
			if [ $DEBUGLEVEL -ge 1 ]
			then
				shift
				echo "$(date '+%F %T') $ERROR: $@" \
                                >> $LOGFILE
			fi
			;;
		$WARNING)
			if [ $DEBUGLEVEL -ge 3 ]
			then
				shift
				echo "$(date '+%F %T') $WARNING: $@" \
                                >> $LOGFILE
			fi
			;;
		$INFO)
			if [ $DEBUGLEVEL -ge 5 ]
			then
				shift
				echo "$(date '+%F %T') $INFO: $@" \
                                >> $LOGFILE
			fi
			;;
		$NOTICE)
			if [ $DEBUGLEVEL -ge 7 ]
			then
				shift
				echo "$(date '+%F %T') $NOTICE: $@" \
                                >> $LOGFILE
			fi
			;;
		*)
			echo "$(date '+%F %T') BUG: $@" \
                        >> $LOGFILE
			;;
	esac
}

# Echo a string if verbose mode is set
# Log as an Info message in any case
function echo_verbose {
	if [ "$VERBOSE" == "true" ]
	then
		echo $@
	fi
	log $INFO "$@"
}

# Echo a string and put it to Error log
function echo_error {
        log $ERROR "$@"
        echo "$ERROR: $@" >&2
}

#### GETTING ALL OPTIONS #####
# Option -h gives some help
# Option -v makes the script verbose
# Option -o has one option argument therefore o:
while getopts ':o:d:vh' OPTION ; do
	case $OPTION in
		v) VERBOSE=true
		;;
 		h) usage $EXIT_SUCCESS
 		;;
		d) DEBUGLEVEL="$OPTARG"
		;;
 		\?) echo "Unknown Option \"-$OPTARG\"." >&2
 		usage $EXIT_ERROR
 		;;
		:) echo "Option \"-$OPTARG\" needs an argument." >&2
		usage $EXIT_ERROR
		;;
		*) echo "There should never happen..." >&2
		usage $EXIT_BUG
		;;
	esac
done

#### DO CHECKS ON THE ARGUMENTS ####
# $OPTIND is number of options gotten by getopts + 1
# Therefore shift arguments gotte by OPTIND - 1
shift $(( OPTIND - 1 ))

# Check if the number of arguments gotten is correct
if (( $# < 1 || $# > 2)) ; then
	echo "One or two arguments needed." >&2
	log ERR "1 or 2 arguments needed. $# arguments gotten"
	usage $EXIT_ERROR
fi

#### DO SOME STUFF HERE ####
# Do something for all other arguments
for ARG ; do
	echo_verbose "Doing stuff for an argument"
	echo $ARG
done

# The script succeded doing its work
exit $EXIT_SUCCESS

 

I have tried to comment the script as good as possible. Explanation for some parts of the script can be found at the page the base for this script originates from. Feel free to ask questions, if something is not understandable.

 

Download the script: bash_example.sh.

Banshee 1.8

Right now, my package manager pacman (I am using Arch Linux) updated the music player Banshee. The last stable version 1.6.1 was updated to version 1.8.0.

The first things I recognized are:

  • Some new menu entries on the left, as Amazon MP3-Shop and Miro Guide
  • In the “tools” menu there is a new entry “correct meta-data”, a tool that easily removes typos in artists, albums or genres automatically
  • The symbol in the gnome panel has its transparent background back
  • When you listen to an audiobook and stop it, the place where you are is bookmarked automatically. Next time you can easily start at the same position.

Some nice improvements. Well done!

Projecthosting on Bettercodes

A few days ago, I got an invitation from a registered user, to register on Bettercodes.org, a project hosting platform. I am really suspicious about such mails, if they are not eaten by my spam filter automatically. But my curiosity was too big.

Bettercodes is a platform, on which software projects can be hosted. The page is really new and so far there are about 4000 developers registered. It is based on a WordPress blog using the plugin Buddypress, to create a social network. Who registers his project there, can upload the sourcecode in a SVN or GIT repository. It is possible to create milestones and tasks. I could not (yet) find any bug tracking features. It is possible to ask questions and create groups. There is a chat available, and it somehow feels like Facebook for software developers.

I asked the Community Manager of Bettercodes some questions via E-Mail. Here are the answers. Originally, I asked in German but I translatet my questions here. The answers I got, were in English.

How can you describe bettercodes.org in three sentences?

bettercodes.org has the vision to become the best place for developers in the Internet. bettercodes.org is a specialized social network for the wants and needs of developers combined with a collaboration platform to run and manage development proejcts. The bettercodes.org is an Open Source project where every developer can participate and contribute in the further developments of the platform. bettercodes.org is free-of-charge and non-commercial.

The project is currently in a beta phase. When will it be completely opened?

bettercodes.org is now open for all interested developer in an open beta. We will still be in beta since we need to develop a lot of features and functions and eliminate several funcational bugs.

You are the Community Manager there. What does that mean?

As a community manager I take care about what’s going on on bettercodes.org and be a point-of-contact for all questions regarding the bettercodes.org project.

You do not have any advertisement on the website. How is the project funded?

bettercodes.org is a non-commecial and advertising free project. The project is financed thru donations from several firms and institutions, e.g. we received a free credit from Amazon Web Services to build bettercodes.org and the free code hosting on their cloud computing solutions which really works great. Donations are only accepted without any obligations to the project.

The chat on the website reminds me of the Facebook chat. What can you say about that?

The chat is a self-developed feature which is not part of buddypress which we use also. The chat will be delivered to the WordPress and Buddypress community. We see the chat as an instant communication tool where people who work on common projects can communicate in a synchron way. Beside the chat we have an internal messaging system for asynchron communication between project members.

Why should I put my project on bettercodes.org and not use pages like Launchpad or Sourceforge?

First of all we provide a freedom of choice. Users now can choose between GIT and SVN, but we have the integration of additional repository tools and tools like trac, code analytics, automated bug analysis, code quality reviews on our list. Second we combined it with a comprehensive project management plugin which is not finish yet. Third we offer a free code hosting without any limitations in terms of storage size, number of team mates or number of projects. Fourth the whole code hosting runs on Amazon Web Services with a liability of 99,9999%, so your code is secure. Fifth we are very keen about a great design and great usability. We believe that many site have really a lack here.

Bettercodes.org promotes the networking of developers on their platform. How is this done?

We provide to tools that developer can network. We are working now on more comprehensive user profiles and a reputation management system. Our goal is that bettercodes.org will be a place where developers from all over the world can meet, connect and find new and interesting relations to other coders…

What will happen to my data, I leave on the page? Is it possible for not registered users or crawling bots to see my participation in groups, etc.?

Personal data and the protection of data privacy is one of our major goals. We bring the user into full control about his/her data, e.g. compared to other social networks a deletion of a user account is 100% with all related data. We will not use any user data for any commercial activities…

You have code hosting using Git and Svn. What about for example Mercurial or Bazaar?

Well, as I stated above. We have plans to integrate additional code hosting concepts like Mercurial, Bazaar and others. But we are a small team and we need the support from the community. Than we can speed up and provide it earlier…

What are your plans for the future?

We have the vision that bettercodes.org will be the best place for developers in the Internet. But we will not preset this direction or what this really means. We hope that many, many developer will engage with us and participate in the further developments. I don’t know where bettercodes.org will be in 3 or 5 years. It is something like a journey without knowing the final destination…

Many thanks to Sandra Jüssen for the interview.