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.