Tip of the day

From RidgeRun Developer Connection

Jump to:navigation, search

Random collection of ways a Linux desktop computer makes your life easier. Also contains ideas for effectively using the RidgeRun SDK.

Contents

Helpful shell scripts

My rule-of-thumb is if you type the same 3 or more commands over and over again, you need to use a shell script to automate that task. Being able to automate tasks is the main reason I like the command line interface over a GUI interface.

To make it easy to create, modify, use, and backup your scripts, we first need a logical place to store them.

Store shell scripts in $HOME/bin

Since starting to use a Posix based O.S. back in 1985 (HP-UX), I have always stored my personal helper scripts in $HOME/bin. I am not sure who taught me to do that or why, but it has worked well for years.

You need to create the directory and then modify your patch so your shell can find the scripts. I use bash, so I modify the PATH in $HOME/.bashrc

mkdir $HOME/bin
echo 'export PATH:$PATH:$HOME/bin' >>  $HOME/.bashrc
export PATH:$PATH:$HOME/bin

Now you can store your scripts (examples shown below) in $HOME/bin and run them by just entering the script name.

For example, if you want to add the finds script below to your $HOME/bin, you can use

sudo apt-get install geany
geany  $HOME/bin/finds      # copy and paste the script into your geany window, save, and exit
chmod ugo+x $HOME/bin/finds

finds - find a string in common text files

Someone once told me the best software engineering tools are find and grep. I am not sure I agree, but I know that the tool I use many times a day is finds, a simple script to recursively look for text in a tree of source code, like the Linux kernel. finds looks in all common source code files (C, header, etc) for a search string. Each line that contains the search string is printed out.

Usage

finds [--dir <dir>] [-a] <search string>

finds

Here is a simplified version. You can other file extensions as needed.

#!/bin/sh
# Todd Fischer, RidgeRun
#
# usage: finds [--dir <dir>] [-a] <search string>

DIR=
if [ "$1" = "--dir" ] ; then
    shift
    DIR="$2"
    shift
    CWD=`pwd`
    cd $DIR
fi

if [ "$1" = "-a" ] ; then
    shift;
    find .  -follow -and \
        -not -path \*svn\* -and -not -path \*\#\* -and \
        -not -path \*doc\* -and \
        -not -name \*~ -exec fgrep -H -i -e "$*" {} \;
else

    find .  -follow -not -path \*CVS\* -and \
        -not -path \*svn\* -and -not -path \*\#\* -and \
        -not -path \*/.pc/\* -and \
	\( \
          -name \*.c      -or \
          -name \*.h      -or \
          -name Make\*    -or \
	  -name Config\*  -or \
	  -name \*.html   -or \
	  -name Kconfig   -or \
	  -name .config   -or \
	  -name \*.vala       \
        \) \
        -and -not -name \*~ -exec fgrep -H -i -e "$1" {} \;
fi
<pre>
Navigation
Toolbox