Archive for the ‘Linux’ Category

Decode command line arguments given to a BASH script

August 25, 2009

Try the elegant way, at last!

For instance, imagine that your programme has the following arguments:
./myprog -s /source/directory -d /dest/directory -c deep

Then, the script myprog should contain a piece of code like this:


if [ $# -eq 0 ] ; then
echo "Usage: $0 -s -d -c"
exit 1
fi
while [ $# -gt 1 ] ; do
case $1 in
-s) source_dir=$2 ; shift 2 ;;
-d) dest_dir=$2 ; shift 2 ;;
-c) copy_mode=$2 ; shift 2 ;;
*) shift 1 ;;
esac
done

Of course, this is only a hint. Just tailor the decoding according to your needs.

UNIX Network Programming by Stevens

July 6, 2009

UNIX Network Programming Volume 1, Third Edition: The Sockets Networking API
by W. Richard Stevens; Bill Fenner; Andrew M. Rudoff

can be found online at safari books.

in addition:

just realised that it is not the complete version, just previews. sorry!

sed one-liners

March 24, 2009

Handy one-liners for UNIX stream editor sed.

scp file completion on remote host.

November 21, 2008

Next time you try to scp a file to or from a remote host, try and see if autocompletion is support in the same way it works for cp. You probably need to have ssh keys setup and an ssh agent running.

e.g. scp host.example.com:<press tab tab to autocomplete and see a list of time>

Using ssh forwarding to retrieve papers instead of VPN

November 5, 2008

So far I was using VPN to connect to my university network when I needed to download a paper from home, but that meant to temporarily lose my network connection, with all the hassle attached.

Today I discovered a much simpler method:

1) Open a ssh port forwarding to the remote machine (university server with IEEE or ACM subscription) in this way:

ssh -D 8080 -N <username>@<server address> &

where -D indicated the local port to forward, and -N avoid opening a shell.

2) Download the FoxyProxy Firefox extension, and configure in order to use a proxy on localhost:8080.

3) Add rules to FoxyProxy so that the proxy is active only when needed (*.ieee.*, *.acm.* etc…).

Thanks to Timo Reimann for having suggested that.

making a bootable windows iso from a local filesystem

October 25, 2008

mkisofs -b <boot.img> -no-emul-boot -boot-load-seg 1984
-boot-load-size 4 -iso-level 2 -J -joliet-long -l -D
-relaxed-filenames -N -V WinXP -v -x .DS_Store -o <windows.iso> <inputDir>

where <boot.img> is the location of the .img file for windows
<windows.iso> is the location and name of the output and <inputDir> is
teh location of the input filesystem

Sage to Google Reader OPML converter

September 24, 2008

For some reason, when exporting rss feeds from Sage, a Firefox plugin, to an OPML file it does not save the feed (i.e. the xml/rss/atom/whatever) link, but the feeds themselves, i.e. the content of the feeds.

If you have the same problem, here’s a fast-and-dirty solution.

  1. export your Firefox bookmarks in HTML format, in a file called, say, bookmarks.html
  2. using a bash shell, extract the feed lines into a file called feeds.xml doing
     grep FEEDURL bookmarks.html > feeds.xml
  3. create the following script called “scriptfeed.sh”
    #!/bin/bash
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    echo "<opml version=\"1.0\">"
    echo "<head><title>RSS Subscriptions</title></head>"
    echo "<body>"
    while read line
    do
            temp=`echo $line | awk -F"<" {'print $3;'}`
            feed=`echo $temp | awk -F"\"" {'print $2;'}`
            url=`echo $temp | awk -F"\"" {'print $4;'}`
            text=`echo $temp | awk -F">" {'print $2;'}`
            echo "<outline text=\"$text\""
            echo "  title=\"$text\""
            echo "  type=\"rss\""
            echo "  xmlUrl=\"$feed\""
            echo "  htmlUrl=\"$url\"/>"
    done < feeds.xml
    echo "</body>"
    echo "</opml>"
  4. Give the script the right to execute
    chmod +x scriptfeed.sh
  5. Launch the script on the output file, say, exp.xml
    ./scriptfeed.sh > exp.xml

I know, it’s dirty and tricky, but it works :-P

Undelete with grep

July 3, 2008

Earlier this afternoon, I did something really really really really really^(infinity) careless and stupid.
I was working on a bash script containing various commands to automate some things that had taken me days and days to learn and get my head round. The script was outputting its results in a directory, which i was continuously deleting, as I repeatedly (re)ran the script to debug and improve stuff.

Cue moment of complete madness:
instead of typing rm -rf [output_dir]
i typed rm -rf [script_filename]

…and then it was gone.

Quick… backups…
Oh… I forgot to back it up or take a copy before I started editing in improvements didn’t I

big sigh…. this is unix… no easy undelete available (at least not that I know of)
now what… ? This would take me days and days to reproduce.

some frantic googling later… this linuxforums thread came to the rescue!

Now I didn’t actually unmount the partition because I had an active session with lots of other stuff i needed not the best idea… always unmount while doing this

- STOP everything and DO NOT edit anything more (to prevent the diskspace being overwritten)
– Unmount the partition
– run the following command:
grep --binary-files=text -500 Ā«search_stringĀ» /dev/sda4 > output.txt

where:
– 500 is how many lines before and after the seatch_string to display
– search_string is a string contained in the file to be recovered
– /dev/sda4 is the partition from which the file was deleted

This reads through the whole partition, prints the number of lines before/after the string pattern into the file output.txt

Then you can search through the output.txt file and hopefully find a block of text that corresponds to the content of the file…. ready for copying/pasting/saving into a new file and immediate backing up onto every partition on the machine…. onto every USB memory stick you can find lying around your desk and in your pockets…. via ssh to every remote machine you own or have an account on… … … etc.

There are also various tools available like magicrescue (on Ubuntu sudo apt-get install magicrescue), but in this instance it didnt work for me – probably coz I didn’t create the recipe file correctly or didn’t know how to run it properly.

But good old grep was enough.

Remove all binaries from a directory tree

June 4, 2008

Here is a one liner that packs some serious bash punch:

rm `find . -type f -exec file ‘{}’ \; | grep -i linux | awk -F: ‘{print $1}’`

Restarting Sound Server in Kubuntu

May 29, 2008

If occasionally your sound stops working, you can try to restart the sound server:

sudo /etc/init.d/alsa-utils restart

If this doesn’t work, try to kill the processes that block it:

1) Identify them:

lsof | grep pcm

example output: esd 5080 name 70u CHR 116,6 13639 /dev/snd/pcmC0D0p

2) kill ‘em:

kill -9 5080

3) restart alsa:

sudo /etc/init.d/alsa-utils restart

That should do the trick. Thanks to the person who posted it here.


Follow

Get every new post delivered to your Inbox.