Friday, September 28, 2007

Messing with arrays in bash

A couple of days ago, a problem was being discussed in #bash.

`How does one append to every element of an array?'

The usual answer is, run it in a loop. The better answer, however, is a one line parameter expansion thingie. Well, here's your answer:

$ array=( "${array[@]/%/foo}" )

That's it. One line of code to replace an entire for loop.

Here's a working example:

$ array=( foo bar baz )
$ echo "${array[@]}"
foo bar baz
$ array=( "${array[@]/%/foo}" )
$ echo "${array[@]}"
foofoo barfoo bazfoo

So how does it work?

Well, what we have here is actually the pattern matching and replacement operator from the Parameter Expansion facility of bash. The general syntax is as follows:

${parameter/pattern/string}

This way, the pattern to be matched can be replaced with the string in the value of the variable. When applied to an array index of either * or @, it performs the match and replace operation for every element of the array.

One curious feature of this operator is the use of %.

If the pattern to be matched starts with a %, it is matched at the end of the string. In our case, we've simply matched nothing at the end of the string and replaced it with foo. The result is that foo is appended to every element of the array. Cool eh?

Just be mindful of the double quotes in case your array elements have spaces or newlines in the values. :)



Once upon a time, I learnt that the expansion of "$*" or "${array[*]}" results in all the values separated with the first character of the value of $IFS. I always wondered where this feature could be used. Then all of sudden, I ended up using it twice in the space of two days.

The first problem was creating a few directories. Brace expansion was ideal. The values for directory names were coming from an array. Here's how I solved it.

(
DIRNAMES=( foo bar baz )
oIFS="$IFS"
IFS=","
eval mkdir -p /foo/{"${DIRNAMES[*]}"}
IFS="$oIFS"
)

I've put the entire thing in a subshell because I'm changing the value of IFS, which could be dangerous in a script. By using a subshell I'm making sure that rest of the script won't be affected. On top of that I'm saving the original value in $oIFS just to be sure. Yeah, paranoid. :)

eval is necessary because brace expansion happens before the parameter expansion takes place.

Anyway, "${DIRNAMES[*]}" actually expands to:

foo,bar,baz

So the whole command becomes:

$ mkdir -p /foo/{foo,bar,baz}

Nifty. :)

Here's another one.. I needed to feed some values from an array into a regex.

$ EXTENS=( txt c cpp h )
$ IFS="|"
$ awk "\$2 ~ /\.(${EXTENS[*])$/ { foo; }"

The expanded value becomes:

(txt|c|cpp|h)

:)

Friday, June 29, 2007

Happiness with AIDS

We all misinterpreted HIV's intentions; really! Check this out if you don't believe me:

That was a mousepad at the IT department of a client we serviced; yesterday. I wonder how several dozens of those ever got printed!

Thursday, May 17, 2007

Gentoo Bugzilla Exploration!

Heheh, `interesting' conversation in #gentoo-amd64 today.. I'm blogging here with the guys' permission :)


[07:00] [floyd_n_milan] !bug 1
[07:00] * floyd_n_milan kicks jeeves! you stupid bot!
[07:06] [angelos] mhm
[07:06] [angelos] Bug #1 does not exist.
[07:07] [angelos] tis what bugzie says :p
[07:07] [perry_] !bug 2
[07:07] [jeeves] perry_: https://bugs.gentoo.org/2 nor, P2, x86,
tneidt@fidnet.com->micke@codefactory.se, RESOLVED, FIXED,
How do I attach an ebuild.
[07:07] [floyd_n_milan] that's weird :S
[07:07] [floyd_n_milan] how do i attach an ebuild? o.0
[07:07] [floyd_n_milan] to the bugzilla?
[07:07] [floyd_n_milan] maybe
[07:07] [floyd_n_milan] bug 1 was.. we need a bug tracking system!
[07:07] [floyd_n_milan] there ARE bugs!
[07:08] [angelos] yeah, flying around everywhere
[07:08] [perry_] lol
[07:08] [perry_] bug 3 was the guy attaching an ebuild
[07:08] [jeeves] perry_: https://bugs.gentoo.org/3 nor, P2, x86,
tneidt@fidnet.com->danarmak@gentoo.org, RESOLVED, FIXED,
poedit-1.1.5.ebuild
[07:09] [angelos] yay poedit is still in the tree!
[07:09] [floyd_n_milan] 1.3.6 hmm
[07:11] [angelos] grubconfig is evil
[07:11] [angelos] bug 666
[07:11] [jeeves] angelos: https://bugs.gentoo.org/666 nor, P2, x86,
mr.big@biglinux.de->woodchip@gentoo.org, RESOLVED, FIXED,
grubconfig-1.5.ebuild
[07:12] [floyd_n_milan] lol
[07:12] [Draconx] bug 1337
[07:12] [jeeves] Draconx: https://bugs.gentoo.org/1337 nor, P2, x86,
danarmak@gentoo.org->davisjp@gmail.com, RESOLVED, INVALID,
Bugzilla asks password over and over for every page
[07:12] [Draconx] invalid my ass


Seriously, IRC can be great fun.. and a huge waste of time at times too :P

Sunday, May 06, 2007

Magicians

I stumbled upon this beauty today:

Sunday, April 15, 2007

श्यामची आई - एक सुंदर विचार!

"या जगात नुसते प्रेम, केवळ दया असूनच भागत नाही. जीवन सुंदर व यशस्वी करण्यास तीन गुणांची जरुरी असते. पहिली गोष्ट म्हणजे प्रेम पाहिजे. दुसरी गोष्ट म्हणजे ज्ञान आणि तिसरी गोष्ट म्हणजे शक्ती. प्रेम, ज्ञान व बळ या तीनही गोष्टी ज्याच्याजवळ आहेत, त्याला जगात कृतार्थ होता येईल. प्रेमहीन ज्ञान तेही व्यर्थ; ज्ञानहीन प्रेम तेही फुकट; प्रेमज्ञानहीन शक्ति व शक्तिहीन प्रेम व ज्ञान तीही व्यर्थच. माझ्या अंगात शक्ती असली; परंतु दुसर्यावर प्रेम नसेल, तर त्या शक्तीचा दुरुपयोग व्हावयाचा. मजजवळ ज्ञान आहे; परंतु दुसर्यावर प्रेम नसेल, तर त्या ज्ञानाचा फायदा मी दुसर्यास देणार नाही आणि प्रेम असून ज्ञान नसेल, तर ते प्रेमही अपाय करील. एखाद्या आईचे मुलावर प्रेम आहे; परंतु त्या मुलाची आजारीपणात कशी शुश्रूषा करावी, याचे ज्ञान जर तिला नसेल, तर त्या आंधळ्या प्रेमाने जे खावयास देऊ नये, तेही ती देईल आणि तिच्या आंधळ्या प्रेमानेच बाळ मरून जाईल! समजा, आईजवळ प्रेम आहे. ज्ञानही आहे; परंतु ती जर स्वतः अशक्त आणि पंगू असेल तरीही तिच्या प्रेमाचा व ज्ञानाचा फायदा मिळणार नाही. प्रेम, ज्ञान व शक्ती यांचा विकास जीवनात हवा. प्रेम म्हणजे हृदयाचा विकास; ज्ञान म्हणजे बुद्धीचा विकास व शक्ती म्हणजे शरीराचा विकास. शरीर, मन व बुद्धी या तीनहींची वाढ जीवनात हवी."

श्यामची आई, रात्र अकरावी: भूतदया.
मा. श्री. साने गुरुजी.

Tuesday, March 06, 2007

The `Different' Colours of Holi

I stopped celebrating Holi a few years ago. I don't really know why, but maybe because I didn't like all those colours messing up my nice long hair back then :P Anyway, this year I decided to join in.

YUVA (Youth Ultra Vision Association), of which I'm a member, decided to visit the folks at Aasara (आसरा). It is an organisation working towards the welfare of homeless or underprivileged children. We visited two of their shelter homes. One in Thane and the other in the village of Digha in New Mumbai. It turned out to be a unique and immensely satisfying experience.

How does one imagine how much we have to be happy with our lives, no matter how difficult/cruel/uncomfortable/unfair it may seem? We can't really know how many privileges we enjoy till we meet the folks who are not so lucky as we are. I'll tell you a couple of `stories' I heard about a few of the children from the people working for Aasara.

One of the girls, somewhere around 9 years of age, had been abandoned by her parents. A brother-sister pair, even younger than 9 I think, had no father. Their mother couldn't afford them. These children were taken in by the shelter home. They were fed, given clothing and a home. They were schooled.

There were three or four teenagers, about to take their SSC examinations. One of them wants to be a computer engineer. Another has a job. He's a welder. Another wants to be a singer. Obviously, we asked him to grant us the pleasure of listening to him. He obliged.. and really, he sings mighty well. So what's so special about these three cases? Well, they have been in the shelter home for a decade now. They've been taken care of and they've been provided as many facilities as possible.

Aasara gets its finances off the donations from various people and NGOs. Aasara has a computer lab, courtesy of such donations. Aasara had the musical training arranged for the interested. An NGO was so impressed with the work Aasara does, they decided to finance ten of the kids. Hmmm. So, even with the absence of `luxuries', which have become our necessities, at least the real basic needs of these children are taken care of by Aasara. Food, shelter, clothing and education.

So what's with YUVA being there?

What we were told was that there was one important aspect missing from these children's' lives. Socialisation. The need to feel and be treated as `normal'. To be a part of the society rather than being abandoned from it. This gap, between the society of `normal' people and these kids, YUVA tried to fill. A squirrel's share maybe. But hey, at least we tried!

We were told at the first shelter home that initially, the Aasara people have to go reach out to the slum children, the children on the streets or railway stations and bring them to the shelter home. The mentality of these different groups are very different and it isn't really easy to get them to mix with each other, but the effort is put in. We decided to add another dimension to this. As the first step, the YUVA members visited these people and celebrated Holi with them.

The festival of colours was painted in very different colours this year, at least for me. Colours of love and compassion. Do you see them?

P.S. YUVA will be keep visiting them in the future, on a regular basis. We are also planning to help out in any way possible. Whatever we can donate... money, books, time.. Whatever we can share.. knowledge perhaps? We shall see. There's a lot to do. Wish us well.

I took a few pics, which I've uploaded on Flickr here: http://www.flickr.com/photos/floyd_n_milan/

Monday, February 12, 2007

Processing procmail logs in bash

Well, the comments in the script should be self explanatory. It was awesome fun writing this! See if you `like' it ;)



#!/bin/bash
#
# Date: Monday 12th February 2007
#
# Processing procmail logs.
# It copies the original procmail log file to a
# temporary file called procmail after some grepping.
# After that, another temporary file procswapped is
# created and processed. Both files can be safely
# deleted afterwards.
#
# Only one drawback, it requires grep 2.5 or above.

# This is the input:
# From theobald@bar-plate.com Sun Feb 11 17:04:36 2007
# Subject: [SPAM] [+++++++++++++++] Re:
# Folder: /home/tnowak/Maildir/.Spam/new/_DzB.U6zzFB.dingo 13416
# From geuzzld@eriksbikeshop.com Sun Feb 11 17:30:40 2007
# Subject: [SPAM] [+++++++++++++] Still waiting
# Folder: /home/tnowak/Maildir/.Spam/new/_SFC.wS0zFB.dingo 30131
# From commercialtalk.com@esoleau.com Sun Feb 11 17:49:00 2007
# Subject: [SPAM] [+++++++++++] She will love you more than any other guy
# Folder: /home/tnowak/Maildir/.Spam/new/_5QC.8j0zFB.dingo 2309

# TomekN had run this much:

grep -B1 "\[SPAM\]" .procmail.log | grep -v "\--" > procmail

# Here's the procmail file at this stage:
#
# From trutawan056@yahoo.com Sat Jan 27 22:06:05 2007
# Subject: [SPAM] [+++++++++++++++] =?windows-874?B?odLDqNG0t9PhvLm608PYp8PRocnS
# From aw-confirm@ebay.com Sat Jan 27 22:39:56 2007
# Subject: [SPAM] [+++++++] You're a Silver PowerSeller Now!
# From 863kurtis@lightningdezignz.com.au Sun Jan 28 01:56:00 2007
# Subject: [SPAM] [+++] Fwd: Too busy to go back to school,{} but need a Un
# From trutawan055@yahoo.com Sun Jan 28 03:47:48 2007
# Subject: [SPAM] [+++++++++++++++++] =?windows-874?B?odLD46rp4rfDyNG+t+wgtdS0te

sed -ne '/^From/{
s/^/ /
h
n
s/^ *//
G
p
}' procmail > procswapped

# Here's the procswapped file at this stage:
#
# Subject: [SPAM] [+++++++++++++++] =?windows-874?B?odLDqNG0t9PhvLm608PYp8PRocnS
# From trutawan056@yahoo.com Sat Jan 27 22:06:05 2007
# Subject: [SPAM] [+++++++] You're a Silver PowerSeller Now!
# From aw-confirm@ebay.com Sat Jan 27 22:39:56 2007
# Subject: [SPAM] [+++] Fwd: Too busy to go back to school,{} but need a Un
# From 863kurtis@lightningdezignz.com.au Sun Jan 28 01:56:00 2007
# Subject: [SPAM] [+++++++++++++++++] =?windows-874?B?odLD46rp4rfDyNG+t+wgtdS0te
# From trutawan055@yahoo.com Sun Jan 28 03:47:48 2007

while read line
do
subarray[$i]="$line"
((i++))
done < <(egrep '^Subject' procswapped)

# I've read the Subject lines into an array called subarray
# Now I'll sort them according to the number of '+'s
# I've created a new array called plus, which has only the sorted
# '+' patters, including the [ at the beginning and the ] at the end.
#
# Use sort -r if you want the list to be reversed.

plus=($(for (( i=0;i<${#subarray[@]};i++ )); do echo "${subarray[$i]}" | egrep -o '\[\+*\]'; done | sort | uniq))

# Now the final work. The array plus is formatted into a form sed
# will understand as the address, by escaping the proper characters using
# guess what, sed!

for (( i=0;i<${#plus[@]};i++ ))
do
sed -n "/$(echo ${plus[$i]} | sed -n 's/\[\([^[]*\)\]/\\[\1\\]/p')/{
N
p
}" procswapped
done

# Here's the final output:
#
# Subject: [SPAM] [+++] Fwd: Too busy to go back to school,{} but need a Un
# From 863kurtis@lightningdezignz.com.au Sun Jan 28 01:56:00 2007
# Subject: [SPAM] [+++++++] You're a Silver PowerSeller Now!
# From aw-confirm@ebay.com Sat Jan 27 22:39:56 2007
# Subject: [SPAM] [+++++++++++++++] =?windows-874?B?odLDqNG0t9PhvLm608PYp8PRocnS
# From trutawan056@yahoo.com Sat Jan 27 22:06:05 2007
# Subject: [SPAM] [+++++++++++++++++] =?windows-874?B?odLD46rp4rfDyNG+t+wgtdS0te
# From trutawan055@yahoo.com Sun Jan 28 03:47:48 2007
#
# Sorted, based on the spam level indicated by the number of '+'s.

Sunday, January 21, 2007

Newlines and sed

Blogger gave me hell trying to post this, so I asked techno_freak to post it for me, on his blog; which he kindly agreed to :)

Thanks dude.

Here's the link to the post:

http://technofreakatchennai.wordpress.com/2007/01/19/newlines-and-sed/

Wednesday, January 17, 2007

Console Locking

I stumbled upon this today:

* app-misc/vlock
     Available versions: 1.3-r1 1.3-r2
     Istalled: none
     Homepage: ftp://ftp.ibiblio.org/pub/Linux/utils/console/
     Description: A console screen locker


Pretty handy stuff. You can lock just the current console with vlock -c. That's quite basic. I liked the vlock -a option. It locks all the consoles. You can't even switch them using Alt+Ctrl+F* keys. Pretty handy.

Thursday, December 28, 2006

A Linux Brochure - Short Version..

In response to this: http://mm.ilug-bom.org.in/pipermail/linuxers/Week-of-Mon-20061225/056339.html

Here's what came into my head. For Windows Desktop users. Sounds much like an advertisement.. It is!

Are you a user sick of viruses, spyware and the like? Would you rather use something more secure? If your answer is "yes", congratulations! You can have much better security and stability!

Are you a user who needs to use the computer for surfing the Internet, emailing, playing multimedia etc? Would you rather give something else a try if you could do your routine work on the computer, in a more cost-effective way? If your answer is "yes", rejoice! Your wish has been fulfilled!

Are you a user who likes to have choices? Would you rather choose yourself amongst the various options available rather than someone else choosing for you? If your answer is "yes", cheer up! You can have your say!

Are you a student who wishes to learn computer sciences? Are you frustrated that you can't
look under the hood of the programs you use to learn how exactly they work? If your answer is "yes", fear no more! You have the freedom!

Choices, freedom? Where? You ask this question because you have not heard of Free and Open Source Software. Maybe, you already use some of it, but you don't know what it exactly is!

Would you care then, to join us millions of users in the wonderful world of Free and Open Source Software? It offers you choices. It offers you freedom. It gives you much better security and stability. It gives you an alternative that could be better for your computing needs.

I give you a Linux based Distribution, ladies and gentlemen! It is an Operating System to run your computer. It includes all the software you'll need. It doesn't even cost much In fact, it can give you much better quality even at no cost whatsoever. If you need and are capable, you could modify it the way you want. Not satisfied?

A Linux based Distribution offers you:
  • A choice of user interfaces. They offer different looks, different level of customisability, different options etc. Customise the way you want, for your needs and according to your choices.
  • A multiuser solution. Many users could share a single computer but still keep their own settings and data completely isolated from the other users of the computer.
  • Top notch security. No spyware or viruses. Built-in firewalls.
  • Unprecedented stability. You can keep your computer running for a long time, without a problem.
  • Choices in software applications. You need not stick to just one email client for example. There are many, equally good, choices. Take a look. Pick the one you want.
  • Excellent community support. Millions use various Linux based Distributions. They communicate and help each other. Join them!
  • And blah blah...
Interested? What are you waiting for? Give it a try! What's more - you need not even install it on your computer to give it a try! Just pop in a LiveCD, restart your computer and it'll run from the CD itself - completely usable!

Now that's just irresistible, isn't it?

Spice it up with screenshots. Tell people where to get hands on the CDs, some community information and basic documentation so they could get started.

The long version should include the history, in short, of both the GNU project and Linux. The history of GNU should tell people that this software exists to empower the user, not for allowing some company to make profit out of enslaving its users.

The long version should also include information about the software along with the screenshots. Various distributions can be introduced.

This is the mail that informed ILUG-BOM about this post:
http://mm.ilug-bom.org.in/pipermail/linuxers/Week-of-Mon-20061225/056347.html