Random Wisdom

Tag: scripting

Create links with absolute paths in Linux

by on Jan.16, 2010, under How To ..., Linux, Software

The default behaviour of the linking command (ln) is a little strange under certain circumstances. Since it creates the links using the literal value of the target, symbolic links created using relative path structures can often fail. Consider the following:

$ ln -s targetfile ../src/targetfile_link

Without a doubt, ‘targetfile_link’ will be a broken symlink since it links to a target that it assumes is in the same directory:

$ cd ../src && ls -l targetfile_link
lrwxrwxrwx 1 mafgani mafgani 5 2010-01-16 18:19 targetfile_link -> targetfile

This is quite unfortunate since it clearly clashes with the way that the linking mechanism should work intuitively.

The solution is to force ln into automatically appending the absolute path to the target files. This can be achieved by using a simple shell script that acts as a wrapper for the real linking command:

#!/bin/sh

# Step through the supplied arguments and append the absolute
# path to targets that exist
for ARG in $@
do
  if [ -e $ARG ]; then
    LNARGS="${LNARGS} ${PWD}/${ARG}";
  else
    LNARGS="${LNARGS} ${ARG}";
  fi
done

# Execute the actual link command with the modified args
exec /bin/ln ${LNARGS};

There are two known caveats:

  • The link is ‘sub-optimal’ if created from within the destination directory (the absolute path contains ‘../’s). It will still work however.
  • The links will always be absolute. If that is undesirable, save the script as ‘absln’ or something other than ‘ln’.

Using ‘absln’ instead of ‘ln’ in the previously described scenario now produces a working symlink:

$ absln -s targetfile ../src/targetfile_link
$ cd ../src/ && ls -l targetfile_link
lrwxrwxrwx 1 mafgani mafgani 16 2010-01-16 19:13 targetfile_link -> /tmp/files/targetfile
1 Comment :, , , , , more...


Command line search utility

by on Dec.27, 2005, under How To ..., Linux, Software

While studying for one of the finals this year, I felt the need for a CLI search utility that would search on Google, Wikipedia, Google Images, etc. I didn’t know of any tools that would already do this so I decided to write my own little bash script:

#!/bin/bash
#Needs the htmlview package

opt="$1"
str="$2"


#Create the search string
until [ -z "$3" ]
do
  str="$str+$3"
  shift
done

case "$opt" in
    "google"  )
    htmlview http://www.google.com/search?hl=en\&q="$str"&btnG=Google+Search &
    ;;

    "image"   )
    htmlview http://images.google.com/images?q="$str"\&safe=off &
    ;;

    "wpedia"  )
    htmlview http://en.wikipedia.org/wiki/Special:Search?search="$str" &
    ;;

    "scholar" )
    htmlview http://scholar.google.com/scholar?q="$str"\&ie=UTF-8\&oe=UTF-8\&hl=en\&btnG=Search &
    ;;

    "ieee"    )
    htmlview http://ieeexplore.ieee.org/search/searchresult.jsp?queryText=\%28\%28"$str"\%29\%3Cin\%3Emetadata\%29 &
    ;;

    *         )
    echo "Usage: search engine searchterm [searchterms]"
    echo
    echo "Engines: google    Basic Google websearch"
    echo "         image     Unfiltered Google image search"
    echo "         wpedia    Wikipedia (English)"
    echo "         scholar   Google Scholar"
    echo "         ieee      IEEE Xplore (needs subscription)"
    echo
    echo "Example: search image batman"
    ;;
esac

echo

exit 0

It makes use of the htmlview package to discover the default browser and display the results. The use of the script is quite straightforward:


[darkknight@darkworld bin]$ search image batman begins

As is, it considers all search terms. Fancy things like Boolean expressions are not supported (yet :)). A copy of the script can be found here.

1 Comment :, , more...