Automating the interaction

2009/01/22

A pretty nice feature of shell scripting is “<<" operator.
It allows basically to launch interactive commands within a bash script.

For example I wanted to add a small but very useful function to textmate, I want to be able to know all the possible informations about a whatever function or data type.

The default info function in the haskell bundle unfortunately is only able to retrieve informations about prelude functions or functions used with the full path.
(eg Data.List.lookup and not simply lookup ).

Using ghci I should load the file (which automatically loads also the needed libraries) and type :info.
This is really easily done using “<<".

I added a command “info about” with the following code in my haskell bundle and now if the file is correct (it compiles) you are sure that you’ll get the informations about the function you’re looking for.

GHCI << EOF
:load $TM_FILEPATH
:info ${TM_SELECTED_TEXT:-$TM_CURRENT_WORD}
EOF

I also think that there should be a better way to communicate with ghci (and using System.Enviroment to get the arguments) but I didn’t find any, this solution even if not really elegant works pretty well.

This “approach” can be used also with many interpreters, sometimes I find it really useful.


Life behing a proxy

2009/01/19

Staying behind a proxy can be very annoying, with macosx it’s nice and easy to set a global proxy.
Unfortunately command line programs don’t care about those settings, so we have to manually set them every time.

With the script you’ll find below we set globally the proxy for ftp, http, rsync and subversion:

Read the rest of this entry »


Copy paste from the command line

2008/12/07

If you are at least lazy as me and you use macosx and you hate the mouse I just found this little “shortcut”.

Just go to your favorite terminal and do something like that:

sh-3.2# pbcopy < file

Now in your copy buffer you have your text file (pbcopy defaults to ASCII, see man pbcopy for further informations) and you can paste it everywhere you want or using pbpaste from terminal.

Of course you can also put them in pipes, as for example this deletes comments from a shells script and puts the result in the copy buffer:

sh-3.2# grep -v '^#' file | pbpaste

Otherwise you should:

  • open the file
  • select all
  • copy
  • close the file
  • This is much faster, isn’t it?


    Random fortune cookie

    2008/12/04

    One of the nicest thing of linux I found at the very beginning of my *nix file was the automatically generated fortune at every login (with slackware).

    Well I know it’s not the most important thing for an operating system but it puts me in a good mood to start working in my favorite shell.
    Another nice program is cowsay , a funny cows (but not only) that tells you something.

    Putting them together you can have a random ascii-animal which tells you a random famous sentence!

    Just set your correct COWFILES directory and put it somewhere ;)

    COWFILES="/opt/local/share/cowsay/cows"
    NFILE=$(ls -l $COWFILES | wc -l)
    NUM=$(jot -r 1 1 $NFILE)
    COW=$(find $COWFILES -iname '*.cow' -print | sed -n "$NUM p")
    
    fortune | cowsay -f "$COW"
    

    If on your system you don’t have jot then you can obtain the random number line also in this way:

    NUM=$(($RANDOM % $NFILE))
    

    Read the rest of this entry »


    Downloading the whole rfc database

    2008/12/04

    This is my first post and I created it with textmate , which has a wonderful bundle to blog directly inside it.
    I created a little function which allows you to download the whole database of the request for comments:

    function update_rfc() {
    	RFC_DIR="$HOME/howto_guide/rfc/"
    	INDEX_FILE="1rfc_index.txt"
    	INDEX="http://www.ietf.org/iesg/$INDEX_FILE"
    	SITE="http://www.ietf.org/rfc"
    	[ -d "$RFC_DIR" ] ||  mkdir "$RFC_DIR" || return
    	cd "$RFC_DIR"
    	[ -f $INDEX_FILE ] || wget $INDEX || (echo "error can't get the index" 1>&2 && exit 2)
    
    	grep -E '^[0-9]{4}' $INDEX_FILE | awk -F ' ' '{print $1}' | while read NUM
    	do
    		FNAME="rfc$NUM.txt"
    		[ -f $FNAME ] && continue
    		# otherwise just try download it
    		echo "downloading rfc number $FNAME"
    		wget "$SITE/$FNAME" > /dev/null || echo "rfc $NUM not downloaded" 1>&2
    	done	
    }

    Just set where you want to download them in the variable “RFC_DIR”, put the function somewher (“.bashrc, .bash_profile” or whatever) and call it from the shell.
    You need to have wget installed, the other commands should be present on every *nix machine.

    Pay attention because it’s pretty big:

    du -sh $HOME/howto_guide/rfc
    266M	/Users/andrea/howto_guide/rfc
    

    Follow

    Get every new post delivered to your Inbox.