A blog about my experience in the IT world.

"Pipeculiar" WHILE behavior in ShellScript

December 13, 2013

While doing some scripting in shell I noticed an odd behavior, i was losing the value of my variable, digging around, i found that although the value was correct inside the while cycle, as soon as it finished the value was lost.
An example of such behavior is the following statement:

user@localhost:[/tmp]$ FICH=file.txt
user@localhost:[/tmp]$ echo "this is dog" > $FICH
user@localhost:[/tmp]$ cat $FICH | while read line; do var="$line";  echo "Inside: $var"; done
Inside: this is dog
user@localhost:[/tmp]$ echo "Outside: $var"
Outside:

Why this happened wasn't at all clear, but after some searching was done this piece of information shed light on the subject:
command | while read var1 var2 ...; do
   # process each line, using variables as parsed into $var1, $var2, etc
   # (note that this is a subshell: var1, var2 etc will not be available
   # after the while loop terminates)
   done
Using the pipe to send stdin to the while cycle, was creating a subshell where variable were assign the correct value, but as the while finished so did the subshell causing the values to be lost.
A quick modification to the to method of sending stdin to while, redirection instead of pipes, fixed the issue:
user@localhost:[/tmp]$ while read line; do var="$line";  echo "Inside: $var"; done < $FICH
Inside: this is dog
user@localhost:[/tmp]$ echo "Outside: $var"
Outside: this is dog
user@localhost:[/tmp]$

The reason can be found here:
A pipe is there to hook the stdout of one program to the stdin or another one. Two processes, possibly two shells.
When you do redirection (> and <), all you are doing is remapping stdin (or stdout) to a file. reading/writing a file can be done without another process or shell.

[C#] How to calculate multiple DateTime average

September 23, 2011

After googling around I couldn't find any good solution for the problem.

Found a few suggestions of converting a DateTime value into ticks that could work. But given a large enough  number of DateTimes there isn't a data type big enough to hold the cumulative value needed to calculate the average, since there are 10.000k ticks in a second.

So, took a little shortcut and used an identical approach with seconds instead:


public DateTime averageDateTime(List collection)
{
double totalSec = 0;
for (int i = 0; i < collection.Count(); i++)
{
TimeSpan ts = collection[i].Subtract(DateTime.MinValue);
totalSec += ts.TotalSeconds;
}
double averageSec = totalSec / collection.Count();
DateTime averageDateTime = DateTime.MinValue.AddSeconds(averageSec);
return averageDateTime;
}

Solved!

Get Your Site Up and Running!

March 4, 2008






For most of you this is pretty much basic information, but there are those you are just entering the world of online business and could need a push in the right direction.

Want to have an online Website? There are just a few things you have to do before going online.


  • Your Site

If you are building a website you must have a purpose! Weather it be boost your sales, expose a product or a service, share information of any kind, etc...
So this is the first step, having a purpose.

Also, you should think what you want from having an online website. Specially if it's a commercial website. Set your goals for short and long term.

You already know what your website will be about, now you should define the information it will have. Short simple texts about your business/activity and a few photos are a great starting point.


By now you have the purpose and the stuff you want to have online! Time to start the real deal.

The following topics are not in the correct order (there's no correct order...). If you are an absolute beginner i'll recommend getting in touch with the person/company that will be responsible for the creation of the website first.

  • Domain Name

A domain name is pretty much a word that identifies a computer (or for our purpose a website) on the internet. e.g. blogspot.com
Choosing the domain is a critical step because it will be how your site will be known online!
In a perfect world your domain should be the name of your company, but by any reason, that word can be unavailable (some registered it before you) and you'll have to choose another...

You can use Domain Tools to check if a domain is available, or if not, who has it.

After choosing a domain, you have to register it, and for that you need a Domain Name Registrars, someone who will register the domain in your name. Check this list of the ICANN accredited registrars.

  • Hosting

At the same time who seek the perfect domain, you can also search for web hosting. Online space where you can store your website. There are plenty of hosting services to choose around.
But the type of hosting you need depend of what kind of site you want.

  • Web designer

If you can't build your own site, you'll need some who do it for you. You need a web designer!
It can be a person or a company, it will help you out through the whole process of creating your website.


Personal Note:
I provide all the above services, so if there you have any question ask right away!

Anonymity on Internet

February 24, 2008






As the role of internet in our day-to-day life increases, both at the professional and individual level, there's a factor that is gaining "new" importance, anonymity.

An user must be able to both be completely anonymous, for regular mainly browsing, and on the other hand, be able to complete prove that "he is who he says he is", especially in order to use some services and secure communication.


Off course, there are those who can argue that if the identify of the user is always know, the internet environment would be much safer; Avoiding countless cases of psychological violence, virtual bullies, etc...
Actions that most users wouldn't do if they weren't behind a mask (nickname) that can be changed at will.

But with the creation of such an environment wouldn't we be destroying internet's essence, the last "free world" where anyone can be whatever they want?

We know as a fact that behind this layer of anonymity there are several illegal acts both against people and entities; Society rules still didn't penetrate many online bastions.
But also, what's considered illegal in one region isn't in other. And internet is the tool that provides a world-wide standard in terms of liberty and freedom of speech. Giving many people the possibilities online that they are denied in there own countries.


Bottom line, internet should be boundary free; Being society responsible for equipping users with a moral and ethical code to be used both in the online world and on "real life". Ultimately, internet users are the ones who have to decide what internet will be!

Before Calling IT Support

February 3, 2008






IT Support is a necessary evil. It exist because computers expanded beyond the universe of people that actually know how to work with them.

Whether you work in an office, bought a DSL Modem or purchase the latest version of a software, you'll probably have access to some kind of tech support by phone or email.

In this kind of service there are three figure involved, the user, who asks for help/information, the technician, who provides the solution, and the most important, information that travel back and forth between the user and the technician.

In order to a successful conclusion of the process the user must be given a solution for is problem or an answer to is question. The efficiency (time and quality of response) depends both on the user and on the technician. In this post i'll focus on the user and the proper way of explaining the reason for calling IT Support.


Before Calling:

Ask yourself what's wrong, you don't need to be a computer specialist to check a few things on your computer. So, check what you know, this often solves the problem or can provide good information for what the problem might be.

Reboot, it's amazing how many problem this action can actually solve.

Have the information ready, if you have to call for help you will have to provide some information. So get the generic information of your computer (os type and version, some hardware specs) and be very specific when coming to problem/bug reporting, be sure to note the error message and, if you can, the actions you did before the error appear.


On call:


Tell what you know, shortly describe what the matter, and the information you gathered on the situation. Once again, be specific!

Follow instructions, when you're told to do something, think first, you're the one interacting with the computer, if it doesn't make sense ask for the reason to do it, if the instruction isn't clear ask the technician to be more specific. Try to understand the solution in order to be able to solve future similar situations.

Know who you talk with, if you have to call again you know someone that already knows most of the situation.Also, there are bad professionals everywhere, so if you're told to do something that damages the system even more it's the technician responsibility.



And finally, be polite; Everyone is working here, so leading the situation in a calm manner is easiest way of getting your problem solved .

Tools of the Job: Eyes

December 21, 2007






As anyone who uses computer all day long has, or probably will have, some kind of problem with his/hers eyesight. Being in a profession that demands constant reading and writing, this problem is not to be ignored.
So, acknowledging that degradation of vision is something that comes with age, what can we do to prevent blindness (total or partial) in the later years of our life.

There are some good practices that can be followed in order to diminish (or even suppress) the degradation of eyesight overtime.

Note to attention: Eyesight problems are often genetic (as said here!), and about this there's nothing you can do. Medicine is probably the best way to deal with them...

Back to the good practices:

Feed your Eyes

Don't forget to include Vitamin A in your diet. This vitamin is found specially in carrots and fish (you also find it in a lot of other foods, but in lower quantities). Deficit of Vitamin A, means impaired vision and night blindness!

Get them Wet

A dry eye, means there's no (or not enough) tears. This can lead to red eyes (irritation), blurred vision, severe discomfort or even infection. People with contact lens suffer a lot with this. Fortunately there are drops you can use to help with this situation. If the situation is chronic medical examination is necessary.

Sleep Well

As any organ of human body eyes need to rest. So sleep! Preferably with your eyes shut...


This were some general advises, but there are some more specific to who has to deal with computers all day long.


Keep the distance

Getting a balance between the distance from the monitor and the size of the fonts in the screen can be a hard trick to pull. You shouldn't have to do any effort to read letters on the screen, adjust both your distance and the monitor's brightness (you're not looking to the sun!) to facilitate reading.

Light it Up

Don't go dark! Always have good illumination when using a computer, natural light is preferable. During night use a desk lamp (that's what I do) or some kind of back light. Avoid light reflection on the monitor, any glare or seeing the light source.

Approach Angle

Try to have the monitor in the same level of your eyes, if that by all means impossible choose a lower angle. This way you avoid not only a stiff neck but also looking in a more natural angle.

Get Up and Away

Don't over use your computer. Many of us spend most time of the day staring at some monitor/tv, so get every and all excuses to get away from it. Go fish...

Don't Stare

When we hit the "zone" it's easy to stare at the monitor minute after minute with don't even blink, so look away, close your eyes, do what ever you want... just don't stare to the monitor all day long!

Get Protection

Monitor filter's are old fashion, new monitors already bring built-in technology that allows a wide number of configuration (brightness, contrast, etc...), so use them...
Secondly get your eye exam once a year to check if everything is OK!



This is nothing you shouldn't know by know, but its a good reminder ;)

Web 2.0, What Is It?

December 5, 2007

The term Web 2.0 isn't new, it has been around for a few years. But if you still don't know what is it, you have by now felt its effects.

Well, Web 2.0 is mainly a concept. A concept about how information should be organized on the internet. It's the understanding that each site is not an isolated object but a part of the all. And, of course, the role an user has in the interaction with the websites.

Information on the internet is not plain text, but series of data that can be read both by humans and computers. In order to help computers decide what is relevant for human information start to be categorized (or tagged). This tag is attributed by other humans helping this way to find a specific topic in a future search. Information isn't still either, it needs to be updated and share. But old systems made it hard, so new languages were created that enabled an easier sharing.

With Web 2.0 also appeared new types of websites. The explosion of blogs (like this one) with millions of people sharing their points of view (like me); Social bookmarking, with people sharing stuff they liked online; And wikis, that truly are universal encyclopedias. And many many more...
This posted a change in the way people perceived websites, they stop being only information givers but also being able to perform a service (whether it be finding bookshops in your town or publish your latest video clip).

Web 2.0 also brought new design concepts;Glossy buttons, round corners and image reflection (Apple inspired), enabled by languages like CSS and programs like Photoshop, websites gained a softer clear look, very different from old websites that were square (table based) and filled with high contrast colors.

This is a little picture of what Web 2.0 is about, and how it affect how everyone sees and uses the internet.

For further information:
Wikipédia - Web 2.0
What's Web 2.0 - O'Reilly
YouTube - Web 2.0 ... The Machine is Us/ing Us _ Nice clip about information organization.
Or just search Web 2.0 in Google...