Well this is a pretty interesting topic. And thus I wouldn’t be able to do it much justice if I were to talk about it with my given aboumt of Linux knowledge. However, not all is lost. Here are links to a series of blog post that I think do a pretty darn good job of explaining the process. Hope you enjoy.
No big news yet. Still exploring though. Recently I have been trying to code a balanced binary tree. As for why; I am trying to solve ORDERS on SPOJ. Doing a naive with arrays and linked list are too slow. However, you can use the same approach with trees (balanced trees actually) and have your solution accepted. I already know how to solve it in my head, the only hindrance is actually creating a balanced binary tree. Well I think I’ll take a break from thinking about it for a while and move on to another problem. Then come back when my skills have matured (lord knows they have a lot of that to do).
Apart from balanced binary trees I’ve also read about heaps. The one I find most interesting is a variant called an interval tree. Basically suppose node X is the interval [a,b] with a<b, then its two children are the intervals [a,c] and [c+1,b], where c is the midpoint of [a,b]. I came across this structure when reading a TopCoder tutorial on how to solve the Range Minimum/Maximum Query problem. Though I have yet to actually practice using any of these methods, the tutorial did a good job of explaining so I don’t imagine it being all that hard.
Well not too much new has gone on. I’ve recently started playing around with the Bluetooth capabilities, but don’t have anything useful to say thus far.
But don’t worry I have something that might be interesting. Though not entirely new it will combine some commands that have been previously mentioned, and make life easier.
Remember back when I first talked about kill, I said that you only had to type
ps ux | less then
kill <process number>. Well the only “problem” with this approach is that you have to search through the list of process to find the one that you want. So here is a cool little trick that might help, if you know part of the process name, that uses
ps ux | grep "audacity"
This will only output all the process whos “name” contains audacity. This is pretty useful, especially if you are lazy and not in the mood to read through the rest of the rubbish. You will still have to use
kill but all the real work has been removed.
Well as promised it is time to explain parts of my script. Well I shall start at the beginning. The !#/bin/bash tell the OS where the interpretor for the script can be found. The following line is just a place where most of the work will be done. This should actually be changed to cd ~ if I were planning on sharning this script (which I am).
Now for the more “interesting” aspects.
ls -a | grep “^\.” > temp
This line will first list all the fils found in a directory (ls -a). Then it will find those that begin with a ‘.’ (dot) and print them to a file called temp. This is done through the use of grep “^\.” grep works with regx, and in the one used here it will match ‘phrases’ that begin with a ‘.’ (dot). Pretty much grep can do a lot of things (or so I have read) but they rely on regx so needless to say I am currently working on learning regx. Anyway back to the code. After this I do a search to find a file called .myPw, this is where the password will be stored. So if the file doesn’t exist we shall create it, along with a password to place into it.
After this I basically use some basic loops to test the user’s input to see if it matches the password. I won’t talk about the loop structure since 1: this isn’t a tutorial on bash, and 2: it is easy enough to figure out from just reading the code. I won’t explain what echo does, if you don’t know just click on the man link at the top of this blog and then click on echo. It’s actually a pretty “simple” command.
One last thing, I would like to point out that after making the file temp I deleted it once I no longer needed it. This turns out to be somewhat optinal (I’ll explain in a moment), but I think it’s just “polite” to delete temporary files you put on someone else’s system. Now for why this is optional. The way the code is set up, if there already exist a file called temp it will overwrite it. If however I had used
ls -a | grep “^\.” >> temp
It would append the output to the end of the file (in which case who knows what was in there before and how much you will have to seach through while looking for .myPw). So there is a BIG difference between > and >>, both direct output to a file but in differnt fashions.
Here is my first “real” script. It does what it was designed to do, but not as nicely as I would have liked. So needless to say there will be some updating. As for the script itself it is supposed to prompt the user for a password before opening firefox. If a password doesn’t currently exist it will ask them to make a new one, that will be saved into a “hidden” file. Really it’s pretty easy to get around but it should be enough to keep your computer illiterate friends away/busy at least.
Here is the script:
#!/bin/bash cd <path> #location of password file there=false ls -a | egrep '^\.' > mytemp #chek if file with password exists while read f do if [ "$f" = ".myPw" ]; then there=true; fi done < mytemp rm mytemp #if not make it if [ "$there" = "false" ]; then echo "Enter a password:" read newPass echo $newPass > .myPw chmod 777 .myPw #make possible to read and write to file (needed for later on) fi input=: correct=false j=: #get actuall password while read j do PW=$j done < .myPw #read until correct pw is given while [ "$correct" = "false" ] do echo "Enter password or ^C (Ctrl + C) to quit:" read input if [ "$input" = "$PW" ] then correct=true else echo "Incorrect password" fi done firefox& while : do ii=2 done
Sorry it’s not in the best font but at least the formatting is there. Anyway the part I don’t like is the infinite loop at the end. However, I haven’t found a way to keep firefox open if it’s not there. With out it, once the correct password is entered the script will end but firefox won’t start.
Well in my next post I’ll try to explain what each line does. Should be a good exercise in talking about terminal commands.
So you’re messing around and all of a sudden a program or 2 or even more stop responding. What do you do? Well for starters you DON’T restart your system, well not initially anyway.
If a program (process) starts to act up and gets out of line, you are perfectly within your rights to go psyco and
kill it. To use this command there are a few things you need to know, well really just one thing (other than some options you can use), and that’s the process number! This isn’t too hard to find just enter
ps ux | less, the
less is there so that you can scroll just in case the output is more than one page long. This will list all of the processes that you (the user) are currently running. However you can also enter
ps aux | less, to list ALL process that are running. Reading the table isn’t difficult so once you find what you are looking for just remember the number.
Now the fun begins. Suppose that the process number happens to be 7331. Then the first thing to try is
kill 7331. If this doesn’t work then enter
kill -9 7331. The -9 will force the process to be killed without interruption (which might have caused
kill 7331 not to work). A command similar to
killall, which works with process names rather than process numbers. So if 7331 were say firefox you would enter
killall firefox. I should note that
killall will end ALL instances of the program (firefox in this case) and not just the one that is misbehaving (7331). More info on
killall can be found here.
Here is a little video to help you remember all of this.
Well today I decided to learn Bash. One of the tutorials (though not all that great) had a pretty good introduction on some terminal commands. The best job was done with the
echo command. First I’ll show some examples then explain what they each do.
echo "hello world"
- echo hello world
- echo “Number2 Atoms Zzz…” | tr ” ” “\n” | sort
The first line will print “hello world” to the screen, while the second will print “hello world”. Notice how the extra space disappeared. This is because the string is not in quotation marks. The final example will display
This is due the fact that the
tr command will replace all the spaces with new lines, then
sort will sort them in lexicographic order. I’ll hopefully explain more about
tr in later post since it seems that it can use regx. But basically
echo prints whatever follows it to the screen. Finally the pipeline, ‘ | ‘, will cause the result of running the previous command to be used as the input for the following command. This to will be talked about later, mainly because I am tired and am going to end this post now.
I would like to start off by saying that I am running Ubuntu 8.04 on a 64-bit architecture. So some of the usual fixes for the 32bit version just don’t work, but I’ll ramble about those later on.
So if you are like most people you use the internet. And you might visit this YouTube site every now and then. Well Ubuntu (and probably other Linux distributions) doesn’t have native flash support. Thus you will not be able to watch videos and use various other sites. Luckily there is a rather painless solution, well there are several but I will only outline one.
First open up our good old friend the terminal. Now type
sudo apt-get install flashplungin-nonfree. Then enter your password when prompted. That’s it, more or less. You should now be able to view any flash content that’s out there on the web (well any that doesn’t require you to have version 10). After doing this I did however experience a small hick up, as I found out later wasn’t all that rare. Basically flash would stop working after a while. Pages would still load but the flash content would not. The solution was to delete the file libflashsupport.so, no idea why but it fixed the problem. In order to do this you must first find where it is located, which could require some digging around. Fortunately the terminal comes to our rescue once again (this seems to be a recurring theme). Just enter
locate libflashsupport.so, and it will list all the locations where this file is located, pretty neat. The locate command can also work with regular expressions (regx), but seeing as how I don’t know regx as of yet I won’t talk about it.
At this point I think I should further explain apt-get, seeing as how this is a pretty important package handler. The three “main” uses of this command are to install packages, remove packages, and update/remove ‘useless’ packages.
- Installing packages:
sudo apt-get install Pkg. Pretty self explanatory from the command. It installs the package Pkg to your system. You can list more than one package at a time so
sudo apt-get install Pkg1 Pkg2, will install Pkg1 and Pkg2.
- Remove packages:
sudo apt-get remove Pkg. The same as install except that it removes Pkg.
- Update/Remove useless:
sudo apt-get update, updates all packages to their most recent version. It is advised that an update is run before this so that
apt-getknows what packages to update. Also this command doesn’t remove any packages so your old versions will be left sitting around (but not installed). To remove the clutter run
sudo apt-get autoremove.
I just notticed that I have used sudo all over the place but you can mess around and figure out when you do and don’t need to use it.
This is my first “theme” blog so please bear with me a little. As you can guess it is about Linux (more specifically Ubuntu).
First I’ll explain the tabs at the top of the blog. The “lsb_release -a” is just a fancy way of saying “About”. Basically it is a general overview of the blog (similar to this post). In case you were wondering when you enter
lsb_release -a into the terminal it tells you what version of Ubuntu you are running.
Now for the “man” tab. It will act as a refernce to some useful (well in useful in some sense) terminal commands. It will contain links to post where the command is discussed. Also when in the terminal if you type
man command, it will tell you how to use the
command. And to quit this help page just hit the ‘Q‘ key.