Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

2012-08-17

How to check if a variable contains a string

string="this string contains a test substring"

if [[ "$string" == *test* ]]
then
        print "substring found"
fi 
 

2011-12-19

The most influential book from my bookshelf

The UNIX Programming Environment, by Brian Kerningham & Rob Pike.


This is the only book from school I still read from time to time.
It wasn't even a mandatory one: just a suggested reading for the computer science lab back in '89.
The title is somewhat misleading: it's not about programming as much as about philosophy.
I can actually say that this book has somewhat shaped my way of thinking.
It's worth reading even for people that doesn't work on unix machines: the Bladelogic Network Shell is based on these concepts as well.

2011-10-13

Dennis Ritchie (1941 - 2011)

One of those obscure heroes that might will never receive the recognition they deserve.
I've found this little gem on slashdot

main()
{
    printf("Goodbye, World");
}

He may not have been glamorous and "visionary", but indeed he shaped our world to a great extent.

2011-08-21

Copying files with on-the-fly compression

having to copy a really big file in the shortest time, this might be useful:

gzip -1 </path/to/sourcefile | ssh -c blowfish-cbc target.host gunzip ">" /path/to/destfile

the light compression (-1) seems to be a good tradeoff: higher compression ratios lead to higher overall transfer time.
the blowfish encryption puts lower strain on the cpu.
using an old 10 Mbit connection, i've copied a vm disk file with a throughput of 13.980.000 bytes/sec

2011-08-19

X bandwidth usage

while evaluating X access to linux boxes i've made some stats using different access protocols:
  1. nx client from www.nomachine.com (encapsulate and compress X protocol)
  2. vnc
  3. ssh -X
i've recorded a sample session with wireshark and here are the raw numbers:


Client                       Bytes on the wire
nx (lan quality)             4924556
ssh -X                       4383203
vnc (16 bit high quality)    4256691
nx (adsl quality)             586752
nx (modem quality)            480296
vnc (8 bit low quality)       433575


aside from the naked data, there are some other factors that might weight in:
  • at low data rates, nx feels snappier and looks better than vnc.
  • at high data rates, plain X with ssh tunnelling seems a better choice, but only if you are using a native linux client: the nx viewer can run on windows with less effort than an Xming+putty setup, and it can publish the full Gnome/KDE desktop of the remote machine.

2011-08-16

CSV Parser

It might take a fool to write a CSV parser in zsh.
Here's one.
It should work in bash too.

#
# read a csv formatted file
# filed1,field2,etc
# fields can be quoted if they contain a comma
#
readcsv()
{
    ifs=$IFS
    IFS=$'\n'

    csvfile=$CSV
    # read full file stripping EOLs
    table=( $(cat $csvfile | tr -d "\r") )

    # line scan
    for line in $table
    do
        campi=( )
        campo=''
        ncampo=1
        quoted=0

        # we scan all the line
        for (( i=1 ; i <= $#line ; i++ ))
        do
            carattere=''
            if [ $line[$i] = \" ]
            then
                # we found some quotes to strip out
                if [ $quoted = 1 ]
                then
                    # closing quotes
                    quoted=0
                else
                    # opening quotes
                    quoted=1
                fi
            else
                if [ $line[$i] = , ]
                then
                    # comma found
                    if [ $quoted = 0 ]
                    then
                        # if it's out of quotes then it's the end of a field
                        campi[ncampo]=$campo

                        let ncampo=$ncampo+1
                        campo=''
                    else
                        carattere=','
                    fi
                else
                    carattere=$line[$i]
                fi
            fi
            # append char to current field
            campo=$campo$carattere
        done
        # at the end of the line all goes into the last field
        campi[ncampo]=$campo

        # now you can access all the field with an array
        #$campi[1]
        #$campi[2]
        #$campi[n]

    done
    IFS=$ifs
}