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
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
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
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
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
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.

rmdir
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.

cat
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
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
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
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
tail will do the same as head, but to the end of the file. you can use the same -n option.

grep
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
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.

chmod
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
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
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
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
cal will ouput the calendar.

uptime
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
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.