Sunday, December 4, 2011

Programmers' Advent Calendar: #3 How to find a program's executable (C/C++)

Say you are writing a C/C++ program and from within that program you need to find the path of the exe file. Under *nix systems. Get the pid, "int pid = getpid();" then simply the file "/proc/pid/exe" is a link to the executable. From there you can do whatever you want. You can also find a bunch of other information about your process in the "/proc/pid/" directory. Come back for more tomorrow :).

Programmers' Advent Calendar: #2 Back references with regular expressions

I am a little late writing my advent calendar posts, but I will try to keep up. For this episode, let me tell you about a really useful feature that's available in most search/replace tools (e.g. sed, ruby regular expressions ...).

Imagine this scenario: I am writing a novel whose main character and her sister are called "Sarah" and "Judy" "Williams". There are other characters called "Sarah" and "Judy" however those have other last names. There are also other characters whose last name is "Williams" but with different first names. I always refer to my characters as "FirstName LastName" in my novel.

 Now, my editor and I decided that we will change the sisters names to "Sarah" and "Judy" "Brians". And it's a disaster, because now I have to go and change the names by hand. I cannot replace "Sarah ***" or "Judy ***" by "Sarah Brians" and "Judy Brians" because I will them mess up the other names. I can't also just replace "Williams" by "Brians" because I will again mess up the other names. What do I do? One solution would be replace "Sarah Williams" by "Sarah Brians" then again replace "Judy Williams" by "Judy Brians". However, what if there were 20 characters from the same family? Back references solve this problem. 

 Back references simply select a part of the search term and save it, then I can use this same part in the replace pattern. It is very useful whenever the change location is identified by something that is bigger than the changed part. In this example, I would replace "\(Sarah|Judy\) Williams" by "\1 Brians". \1 will refer back to whatever was between "\(" and "\)". If you had more pairs of "\(" and "\)" then you can refer back to them using "\2" , "\3" ... in the order they were defined. Come back tomorrow for another little useful piece of information. :).

Saturday, December 3, 2011

Programmers' Advent Calendar: #1 Finding a path to a bash script

I have decided to start this series, for the 24 days of christmas, I will dedicate a post each day to tell you about a little thing you might not have known. I will try to stick to small examples or one liners most of them time. However, some biggers stuff will be too good to miss. This is the first post of the Series.

Often enough, you are writing a bash script and you want to find out where is your script. Here is a reliable and repeatable way to find the directory where your path resides.

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

However, if your script changes directories before calling this, it will get the wrong value. Now let me explain how the script works. BASH_SOURCE[0] will return the relative path of the script from the directory where the script was called. dirname will remove the script name from that. Which at the beginning of the script is the same as pwd. If we go to that directory then we are at the same dir as the script. Executing pwd there will give us the absolute path. And this is what we store in DIR.

Come back tomorrow for another post :).

Monday, November 7, 2011

Linux Commands for Beginners

We had organized a talk for the Infotech Student Organization to introduce the students to Linux command line. It was often the case that a student was put in front of a Linux box and was asked to do 'X' research while he had no idea why there was no 'Start' button in the bottom left corner.

A few weeks ago, in a similar situation at UBC, I have seen a student open nautilus as root, navigate to /usr/bin and drag/drop files there. It was an aweful sight.

Back to the talk for Infotech, I was asked to give the actual talk with some demonstrations and examples to the people. I created a tutorial which contains what I believe is the fastest way to get a person started on a bash terminal. It might not be the most conceptually accurate, but it's guaranteed to get your started, _fast_. It is necessary that you read the tutorial in sequence, because I have organized it so that there is not a thing that I will use before explaining first. It will save you a lot of headache.

So let's start:

Command line arguments
when you want to execute a program on a linux terminal, you type the name of the program and press “Enter” or “Return”. Sometimes the programs needs input, for example, a program that prints the date could need an input specifying how would you like the date to be formatted. We will see an example on that later. To give a program input on the terminal, you type the name of the program, then space then the first input then space then the second and so on. it would look like this “ls -l -a -h”, we will see what this does later on.

ls stand for “list” it is use to list all the files and directories in the current directory. ls can have my options. check “man” for details (I will show you how to do that in a few minutes).

Repeating a previous command
on a linux terminal, there is something called “history”, this basically means that the terminal saves the history of previously executed commands. You can access this history in the simplest way by hitting the “up arrow” button, each time you hit it it will go one command further in the past.

ctrl+c means pressing the control (ctrl) key first, then while holding it down, pressing the “c” key. It is using to interrupt the previously entered command or delete the command currently being typed entierly.

man is an interface that is use to read the manuals of programs. Most programs have those manuals correctly setup on your system just type “man command” to see the manual. The manual will open in a “less” window. Check below for further details. If the man of a program doesn’t exist, it’s a convention to try “command --help”, most of the programs will have this print a short description of the program as well as all the options that can be used with it. Try “man ls” and “ls --help”. You can scroll inside the manpage with “arrow up” and “arrow down” or “page up” and “page down” keys. More options are on the way when we discuss “less”.

cd stands for “change directory” it is used to change to a directory. it takes one argument which is either a relative path or an absolute path to a directory. Like this “cd /home/linux/linuxtut” or if you are in /home/linux then “cd linuxtut”. If you call “cd” only without any arguments, it takes you to your home directory. The home directory is specified in the environment varialble “$HOME”. More about that later.

pwd prints out on the command line the current working directory, if you get lost and don’t know where you are, just type “pwd” and it will tell you the full/absolute path to the directory you are currently in.
Remember, you can always type “man command” to know how the command should be used.

mkdir and mkdirhier (mkdir -p)
mkdir is used to create a new directory. it takes one argument, either the relative of absolute path of the directory you want to create. if you want to create dir1 inside dir2 and dir2 doesn’t exist, mkdir will fail. you can use “mkdir -p” or “mkdirhier”(not available on all systems) to create both dir2 and dir1. It will create even the whole hierarchy of directories if more than 2 exist.

cp and cp -r
cp is used to copy a file, enter “cp original copy” both original and copy are the relative or absolute paths to the files. You can also enter “cp original dir” and the file original will be copied with the same filename into “dir”.
cp -r is used to copy a complete directory. r stands for “recursive”. type “cp -r originaldir copydir” also using absolute or relative paths.

rm and rm -r
rm is used to delete a file, it stands for “remove”. “rm file” will remove “file”. to remove a directory and all the files inside it, you can use “rm -r dir”. “rm -r file” will also work. r also stands for recursive. Use rm -r only with caution as it can delete a huge amount of files/directories in a single command.

if you want to remove a directory, rmdir can be used, but rmdir will work only if you have removed all files and directories from inside the directory you want to delete.
you can make rmdir behave like rm -r if you say “rmdir --ignore-fail-on-non-empty dirname”.

mv (rename)
in linux, we don’t have a “rename” option since renaming something is like “moving” it. mv stands for “move” and it takes two arguments “mv original target”, original and target can be absolute or relative paths to a file or a directory.

now we start with file viewing and manipulation commands. cat stands for “concatenate” . It takes one argument “cat file” and it will print that file to the output of the command line allowing you to see the contents of the file. We use cat to view the contents of small files.

more is a program that allows you to view a large file and scroll through it. Otherwise it would be very large to “cat” on the terminal. With more, you get one page of the file, and when you press “space” the next page is displayed. You cannot go back in more.

less is a name given to the program that is opposite to “more” because it does the opposite to what more does. You can remember which to use by this anecdote “less is more than more”. Because less allows you to scroll both up and down as well as search the file. It’s used like this “less file”.

head is used like cat but it only concatenates the start of the file. It will print the first 10 lines of a file when used like this “head file”. you can change this by using “head -n x file” it will print x lines from the start of the file.

tail will do the same as head, but to the end of the file. you can use the same -n option.

grep is a very powerful command that allows you to search a file for some pattern and it would print out the lines containing that pattern. For example, you had a .c file and wanted to see if a specific variable is used in it you can use “grep pattern file” and grep will print all the lines containing your pattern. the -i option tells grep to “ignore case”. the -r tells grep that you will specify a directory instead of a file and that grep should search this directory recursively. the -n option tells grep to output the line number for the output lines.

echo is used as follows “echo string” it will simply repeat that string for you. so the output of “echo Hello” is “Hello”. But echo is particularly helpful when you want to see the values of environment variables, you can use it as follows “echo $VARIABLE” and it would output the value of that variable.

When you "ls -l" you get some crazy symbols on the left similar to "drwxr-xr-x". These represent the file permissions. Here is how they work.
You have 10 letters: "0123456789"
0 will be 'd' if it's a directory and '-' otherwise
123, 456, 789 correspond to the rights for "owner", "group" (owner's group), "every body else"
so, 1,4,7 are for the "read" right
2,5,8 are for the "write" right
3,6,9 are for the "execute" right
If you have the right, there will be an 'r', 'w', or 'x' respectively, otherwise a '-'
The command to change those is chmod. To change something, you must have a "write" right, or be a root.
chmod works in 2 ways:
Either write "chmod xxx file" where x is a single octadecimal digit corresponding to the rights you give to each group, where the 1st x is for the 1st group and so on.
As a side note, here is a table for octadecimal transformation into binary:

  • 0 -> 000
  • 1 -> 001
  • 2 -> 010
  • 3 -> 011
  • 4 -> 100
  • 5 -> 101
  • 6 -> 110
  • 7 -> 111
so, now map the 3 binary digits to "rwx" and you get your rights for one group. For example, 5, which is 101, represents r-x rights.
the other way to use chmod is to do use some letters
  • 'u' = user (owner)
  • 'g' = group
  • 'o' = others
  • 'a' = all (the 3 permissions)

  • 'r' = read
  • 'w' = write
  • 'x' = execute

  • '-' = remove
  • '+' = add
and you write it such as "chmod [a|u|g|o] [+|-] [r|w|x]". What this means in english is, write chmod, then write any combination of "augo" then write '+' or '-' then write any combination of "rwx". So u+x will give the owner execute right and og-rw will remove the read and write from others and group.
Take care because sometimes a-r or a-w or u-r or u-w can be dangerous if you don't have root access. There are a lot of other options, consult the chmod manual (man chmod) for the full details.

chown (user:group)
chown is use to change the owner of a file. "chown new_owner:new_group file". Don't forget to check "man chown".

pidof stands for “process id of”. It searches in all the running processes and returns the id of the matching ones. it takes one argument “pidof process-name”

kill is used to kill a process. I takes 1 argument, the pid of the processor. it is often used in combination with pidof in this way “kill `pidof process-name`” those are not ‘ single quotes. see below for more about that.

those characters are called “back tick” they are enters by hitting the key right below ESC on a US english keyboard. They are used to tell bash to execute the command between them first then put its output in this place then execute the other command. they can be used like this “kill `pidof process-name`” and this would be exactly equal to “kill process-id” if you know the process id.

we call this the “pipe” character. it’s on your keyboard usually at Shift-\. The pipe does exactly what it sounds like, it pipes the standard output from the command before it to the standard input of the command after it. We usually use | to search in the output of a command for example like this “lspci | grep -i “audio”. lspci lists all the pci devices connected to your computer and its output goes to the input of grep. grep searches the input for the word “audio” and the -i tells grep to do a case insensitive search.

>, >> and <
both of those characters are called redirection characters. We will not go into much details about them. In their simplest use, we use them to direction an output of a command to a file. this way “pwd > file” for example. > will create the file if it doesn’t exist or overwrite it if it exists. >> is used in exactly the same way except if the file exists it will append to it instead of overwrite it.
< is used to direct a file to the standard input of a program. It is not very often used because most software that needs a file as input will have the option of taking the filename and reading the file itself. but you can try it with grep “grep -i pattern < file”

date is used to output the current date. it can also be used to format a date in the format you would like. read the man page

cal will ouput the calendar.

uptime outputs mainly how long has it been since the system was started. check the man page to know the meaning of the other things.

vim and nano
vim and nano are terminal based text editors. If you need to edit a file, try "nano file" this will open that file in a terminal based text editor called nano (notice that file is a command line argument to nano). You can navigate nano using the arrow keys. Some help is displayed in the bar at the bottom. For example, ctrl+x will exit nano.

wget is a command that fetches/downloads a file from the internet using different protocols.

Wednesday, February 2, 2011

My position regarding events in Egypt

1. Mubarak's reaction to the protests should have happened BEFORE the protests took place. A true patriotic president would have pushed for the protesters' demands on his own, without any protests since those are basic human rights and steps towards real democracy. Mubarak had thirty years to work towards those goals and he seemed to totally ignore them and work for an unpublished agenda. For this reason, this guy CANNOT be the Egyptian president for one day longer. The only thing keeping him in power was his power, this was clearly destroyed in the protests.

2. Mubarak's reaction during the protests were reactions of a man who is clinging with all he has to his chair. Starting from trying to control the protests through riot police, the meaningless speeches, the slow reactions to the people's demands, ignoring the people demanding him to step down, the military showdown over Tahrir Sq. among many other things.

3. Appointing a vice president and allowing opposition parties to be heard and represented in the presidential elections is a normal thing that should have been done around thirty years ago. Plot Mubarak's actions on a graph and you will immediately see the patterns emerge. He never does something for the good of the people unless he absolutely has to.

4. The opposition groups were utterly stupid during the protests. I, a total political ignorant, predicted that Mubarak announcing that he will not run for the upcoming elections in a few months will result in a lot of unrest for the protesters although this is one of the best scenarios to support a transition of the power without chaos taking over the country. The opposition leaders should have made a formal proposal as follows:

- Mubarak does not run for any further elections, neither will his son.

- Amendments of the constitution that guarantee of presence of a number of diverse candidates.

- Further amendments that guarantee the supervision from the international community of the elections and that guarantee free and fair elections.

- Even further amendments to limit the number of successive presidential terms to only two.

- Mubarak does not pursue his presidential roles until the elections and all roles be transfered to the vice president Omar Sulaiman.

- Change of the governoment.

- Undoing all actions of media and internet censorship immediately

Then push Mubarak to give in for this proposal. Had this happened, everybody would be on his way home happy by now.

5. The Egyptian media and the Egyptian state TV were completely unprofessional during the protests. Coverage of the events were almost always either making false claims or massively under-reporting what is happening. The Egyptian media seemed to be totally driven by politics. This further supports other evidence, like internet censorship, that Mubarak is still, as always, ready to do whatever he can to stay in power. So far, there has been no indicators that he will discontinue this behaviour.

6. The will of the people is the utter most important thing in the whole matter. If the people say that the president goes, then he immediately goes. It is the people's country and if the majority chooses to destroy it, then it is their complete right. Most of Egyptians, and especially Mubarak, seem not to understand this at all. By the way, this is called "Democracy".

Conclusion, I am still not happy about the current state. Mubarak must stop all presidential roles immediately as he cannot be trusted to be in power any further. It might be acceptable for him to just fill the chair for this transitional period until the upcoming elections in 8 months.

Friday, September 10, 2010

عباس بن فرناس never broke his neck !!!

It's true, Abbas 'bn Fernas never broke his neck, in fact, his flight attempt is considered succesful. Abbas 'bn Fernas actually _settled in the air and flew untill he fell a long distance away_ as described by 'bn Hayyan. 'bn Fernas was a scientist and he did _not_ do a stupid thing. He did a successful flying attempt based on scientific calculations and observation of birds, which proved correct. It is said that 'bn Fernas failed to land correctly, which should be done by flaring his wings as commonly practiced in hang gliders, and hurt his back (some sources said that he only broke a rib). Abbas 'bn Fernas' flight was the first flight attempted by a human in the documented history.

Monday, September 6, 2010

Basbousa Recipe

After so much time researching and trying. And with the help of Ahmed Garamoun, we finally came to the perfect recipe for the very much loved oriental sweet "Basbousa"

Revised recipe, written on 6th of september 2010

1/2 kg of "de2i2 semid" or griess in german (weichweizen)
300 gm of sugar
300 ml of water
around 100 gm of molten butter
1.5 tsp active dry yeast

500 ml (2 cups) of sugar
375 ml (1.5 cups) of water
1/2 tsp lemon juice

Mixing bowl
30 cm lightly greased pan for baking

Mix all dough dry ingredients, then add the butter then the water while mixing till a loose paste forms. Let it sit for 10 minutes.
Put in a 180 degrees oven for around 50 minutes (until golden red/brown)

Once you put the dough in the oven, add the water to the sugar to make the syrup, bring to a boil, after all sugar is dissolved add the lemon juice and set aside to cool.

Once the dough is out of the oven, sprinkle the syrup on top lightly (no pouring) until all the syrup is on the dough (dough could take a little time till the syrup is absorbed)

Cover (with aluminum foil) and let it cool to room temperature then cut and serve.

Almonds or other nuts could be sprinkled on top of the dough before baking, press them lightly inside the dough after sprinkling.
about 1/2 cup of coconut could be added to the dough.

Let me know your feedback

Assault on Entebbe

Please watch the videos here

This is what nations do for their people... I feel shame for my nation.

Alcohol content in bread

I want to broadcast this message, because many people don't really realize it.

There is an organizm called Saccharomyces cerevisiae. Or what we know as "Yeast". When yeast is alive, it eats sugar and roduces water, alcohol and CO2. There is no way to avoid that. The same yeast that is used for brewing alcoholic beverages is used for baking, which means, the same process that produces water, alcohol and CO2 happens in each and every loaf of bread. A study has been done to analyze the alcohol contents of bread, it has been found that a normal loaf of bread, bought from the bakery contains between 0.04% to 1.9% alcohol. 1.9% is a really close number to normal light beer, and I believe is by far superior to most baked food whose labels include alcohol among the ingredients. I would thus conclude, I don't think it's correct to have an alcohol phobia in the sense that many people do, because you already get your dose of alcohol each and every day. I see a more rational way to think about it, how much alcohol starts to get me drunk. And thus, drinking an alcoholic beverage, or eating something that contains alcohol in the ingredients is totally okay, because _bread_ is one of those things. You just need to watch for the amount of alcohol you are consuming. Based on personal experience, an average adult man would start feeling he drank alcohol after consuming around 1.5 liters of light beer (2.5%) or 500-750 ml of normal beer (5%). For non alcoholics, I am not inviting you to be drunk all the time, I am not encouraging you to drivink alcoholic beverages, I am just calling for you to think about your alcohol phobia. Alcohol is not that scary, in fact, you drink some every day !!

References: (The section about Alcohol content in bread)

Sunday, September 5, 2010

Discussion - Am7aya f forn afrangy

(This post is related to

Well, finally I got the book, at least for a few weeks to read it. (Thanks to Youstina Daoud for that)
Me, having lived 30 months so far away from home, I have lived most of the experiences Christine is talking about in the book, only more intense. I'm astonished of how similar was the way I reacted/coped with things to the way she did. I would like to comment/discuss some part of the book that has been a great controversey for me. It's chapter 10 "Masr Tel3et Menhom".

Masr, eh heya masr?? masr, ana etwaladt henak, w law kan el e5tyar f idi makontesh e5tart dah. 3esht awel 20 sana men 3omri henak, w bardo law kan 3andi el e5tyar makontesh e5tart dah. As7aby w ahli wel nas el ba7ebohom henak, dih yemken el 7egga el wa7ida el sa7, bas lazem a2oul enno ana ma2adetsh kol el wa2t dah ma3 nas tany 3lshan a3raf hal el nas el 3ereftohom f masr dol mo5talefin walla dah el tabi3i. W mesh ma3na enno 3agebni el nas el f masr enno lazem a3od f masr, fi mogtama3at masreya _nedifa_ f 7ettat tanya ktir.... ana moshkelti ma3 masr mesh mawke3ha el go3'rafy wala el 7okouma wala el gaww wala el nadafa wala ay 7aga men el 7agat dih. ana moshkelti ma3 masr el masreyiin, el masreyiin lesabab 3'er ma3loun fakado el e7teram bekol ma3anih. y3ni tela2i wa7ed afel 3alek fel rakna w tale3 bethom yenam w howa 3aref kwayes awi enno 3arabitak dih mesh men el 7etta w shwaya sa7ebha hayob2a 3ayez yemshi. howa nafseyan mestaraya7 tamaman w byetla3 beitoh yenam w yesha5ar. ana ba2oul fakado el e7teram 3lshan bel tasarof dah, walaw enno momken nas ketir te2oul 3ady mesh moshkela kebira, 3lshan el ragel 7atta law mesh ased bekol salasah karrar enno ana wa7ed a2all menno w enno howa momken yenam f bethom w ana 3ammal adawar 3aleh 3lshan yetalla3 3arabito. ana 3aref enno a3'labeyet el nas mesh waselohom el mawdou3 dah kwayes, bas lamma teshoufh el nas el f almanya (ana a3ed f almanya) byetsarafo ezay hatefham, mesh hatefham ana 7asses b eh w bafakar b eh ella lama te3ish f west sha3b mo7taram. tabba2 ba2a el masal dah 3ala kol el bani2admin, 3al mwazaf fel morour, 3al bayya3, 3al zabet, 3ala kol ahl masr. el nas fakadet e7teramhom w ta2derhom lel nas el tanya. ana law mo7taram, mesh ha3od ma3 el nas dih, belzat law 3andi el forsa arou7 7etta tany. Harou7 a3ish barra, mesh ba2oul hansa as7abi w hansa zekrayati, ana hagy saye7 (zay el walad el sha3ro tawil el Christine etkalemet 3aleh fel ketab), hagy _mo7taram_ hanbeset, w atfassa7 w arou7 el ahramat w sharm el sheikh w arawa7 betna.
fi mawdou3 tany Christine betetkallem 3aleh f chapter 11. Ana 3ayez awasal resalah, e7sas enni fel beit. Enno el 7etta dih beta3ti, enni a5bat el 3arabeya w awel mad5ol el beit ab2a mettamen, fi ra2yii, law 7ad 3ash barra masr aw barra beito lefatra tawila men el wa2t (1,2 years..) el e7sas dah ra7, w 3'aleban mesh hayerga3 tany, w belzat lel welad. hayiigi makano e7sas tany.. e7sas enno ana lazem atsarraf, e7sas enno law kassart 7aga hatedfa3 tamanha, law 3ayez teshteri 7aga lazem twafarlaha, law 3ayez takol lazem totbo5, law 3ayez telbes lazem te3'sel.... hayiigi makano e7sas _el mas2ouleya_. e7sas esteklaleyah, e7sas enno ana mesh me7tag 7atta baladi wala beity 3lashan mamotsh. dah mostawa aa5ar men el e3temad 3ala el nafs. el e7sas dah sa3b awi fel awel, hat7ess enno malaksh sanad, mafish 7ad yenfa3 a3ayatlo w a2ollo ta3ala el7a2ny. w a7san bektiir enno el kalam dah et3amal fina delwa2ty, 3lshan keda keda lama hanetgawez hayet3emel fina, fakkaro fiha enno dah _demo version_, nos5a tagribeyah, mabtedfa3sh fiha flous. 3lshan ba3d keda mayenfa3sh yob2a fi tagarob. ana basta3'rab men el ahali el bet2affel 3ala wladhom (5osousan el banat) w mota5ayelin enno keda homma beysa3do. keda homma beyzawedo el ta3'ayor el haye7sal lama wladhom hayob2o modtarrin 3'asb 3anhom yetsarafo.