Skip to Content

Day 10: on white belts

Posted on 3 mins read

I have something to confess. Today, I failed to follow one of the two patterns I described a while back: I didn’t wear the white belt. I charged with my prior knowledge into a domain I had never tackled before. And the results were as disastrous as you may imagine them.

The whitest bash belt to have ever bashed

I was attempting the following exercise: Contacts program in bash and I decided it was important to not have duplicates. Because you never want to find out you have two entries by the name Emergency Doctor and not know which one to call in, you know, an emergency.

In my mind, this seemed trivial. At the moment of adding a new contact, I would go to my current list, check if any of my contacts already had the same name, and if so, politely ask myself to give this new contact a different name. Easy Peasy. A quick 5 minutes of googling later, I found out that you can use grep and wc in conjunction for exactly what I wanted. So, I went ahead and wrote the following masterpiece:

already_exists() {
	return grep “$@” $CONTACTS | wc -l
}

Yeah… that doesn’t work. Loudly. It yells at you, begging you to stop. Looking back at it, I should’ve known something was wrong at this moment, but I didn’t. I just went and kept trying to make my JS mindset work. So, I did this:

already_exists() {
	numresults= $(grep “$@“ $CONTACTS | wc -l)
	return numresults
}

And it didn’t blow up. The word Success started flying through my mind ✨. Then I tried to use it and nothing happened. Turns out, this doesn’t work either. This is the result of calling that function, no matter the conditions:

already_exists “Fluffy”
 
 

If you’re wondering where the result is, it’s right there. It’s nothing. Nichts. Nada.

You can’t return anything other than exit codes in Bash.

I’m not going to eternally bore you with all my attempts at making that work, including some futile debugging attempts. What’s important here is that I was trying to fit my old knowledge into a new domain. I was attempting to solve this problem like I would’ve solved it in Javascript. I wasn’t wearing the white belt at all, and if only I had remembered my own writing, I would’ve understood that maybe things work differently in Bash. Instead of assuming that it was all some silly syntax error, and that my thought process was accurate.

It can be pretty easy to forget ourselves and fall into this trap. Like a child who really wants to solve a puzzle and starts mashing pieces together, forcing them to fit. Or the young me who disassembled a Rubick’s Cube only to piece it back together in the right order. We often hear the phrase We are the sum of our experiences. And while that is what makes us who and what we are, it can sometimes work against us.

P.S: I eventually figured out what was happening, and ended up doing something like this:

already_exists() {
	grep -i -q "$@" $CONTACTS
}

# Then, somewhere else
already_exists “Real Name”
if [ $? -eq 0 ]; then
	# That means it already exists. 
fi

(Which works, although I’m pretty sure it’s far from optimal).

comments powered by Disqus