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.

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

Review view mirror cd -- where have I been

You can add the following to your $HOME/.bashrc start up script. Then you can easily navigate to directories you have been visited before. Credit goes to [http:/geocities.com/h2428 Petar Marinov], who put this version of cd_func in the public domain. Tweaks by Jose Jimenez. use cd -- to get the list of directories you have previously visited. Use cd -3 to go to the third one on the list.

cd_func ()
  local x2 the_new_dir adir index
  local -i cnt

  if [[ $1 ==  "--" ]]; then
    dirs -v
    return 0

  [[ -z $1 ]] && the_new_dir=$HOME

  if [[ ${the_new_dir:0:1} == '-' ]]; then
    # Extract dir N from dirs
    [[ -z $index ]] && index=1
    adir=$(dirs +$index)
    [[ -z $adir ]] && return 1

  # '~' has to be substituted by ${HOME}
  [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"

  # Now change to the new dir and add to the top of the stack
  pushd "${the_new_dir}" > /dev/null
  [[ $? -ne 0 ]] && return 1

  # Trim down everything beyond 11th entry
  popd -n +11 2>/dev/null 1>/dev/null

  # Remove any other occurence of this dir, skipping the top of the stack
  for ((cnt=1; cnt <= 10; cnt++)); do
    x2=$(dirs +${cnt} 2>/dev/null)
    [[ $? -ne 0 ]] && return 0
    [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
    if [[ "${x2}" == "${the_new_dir}" ]]; then
      popd -n +$cnt 2>/dev/null 1>/dev/null

  return 0

alias cd=cd_func

if [[ $BASH_VERSION > "2.05a" ]]; then
  # ctrl+w shows the menu
  bind -x "\"\C-w\":cd_func -- ;"

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.


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


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

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

if [ "$1" = "--dir" ] ; then
    cd $DIR

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

    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" {} \;