[awk] Command line calculator using awk
This is an update on our bash command line calculator posted a few days ago — except for the fact that this time we’ll use awk to do the calculation instead of bc. As I mentioned in that post, you may use python or ruby (irb) to do the same thing, but these tricks may be useful if you don’t have ruby or python installed (bc and awk, in general, come by default in any Unix or GNU/Linux distro).
First, create (or rewrite if you used our last trick) function “?” as follows and put it in your ~/.bashrc file:
? () { awk "BEGIN{ print $* }" ;}and make sure to reload your ~/.bashrc file (do the similar thing if you're using any other shell). [NOTE: ZSH does not like ``?'' as a function, so you might consider replacing it with something reasonable, e.g., ``compute'']
Now, if you want to calculate an expression, do it, for example, as
$ ? "2*3+4.0*(9.9+8.1)"and don’t forget the quotes.
.
The advantage of this over bc is that you can use more arithmetic and trigonometric functions (link):
atan2(y,x) Arctan of y/x between -pi and pi. cos(x) Cosine function, x in radians. exp(x) Exponential function. int(x) Returns x truncated towards zero. log(x) Natural logarithm. rand() Returns a random number between zero and one. sin(x) Sine function, x in radians. sqrt(x) Returns square root of x.
.
You may include variables as well in the function definition itself:
? () { awk "BEGIN{ pi = 4.0*atan2(1.0,1.0); degree = pi/180.0; print $* }" ;}where we have defined the variable pi and degree (such that tan(pi/4.0) = 1.0 and pi radians is equivalent to 180 degrees) to be used later, e.g.
$ ? "cos(pi)"
$ ? "cos(90*degree)"and I’m sure that you’ll get -1 and 0 (within the machine precision), respectively, as the answer!
(You may find some more interesting calculator related tricks posted in this blog scattered in different pages)
Credit: here and here (via LifeHacker).
UPDATE: I just realized that I can use the calc package (besides bc, awk, python and irb) to do the command line math wizardry more efficiently (and let’s take that as the end to this command line calculator series!). It has a larger set of built-in functions. You may grab the source code from the maintainer’s website and follow my instructions to install it on your system [although binaries are also readily available, e.g. apcalc package for Ubuntu 9.10 (Karmic Koala)].
[vim] Save and use vim sessions
Save your vim session while working on a lot of files opened in different tabs or screens by using
:mksession saved_session.vimin one of the vim windows. Note that it does not have to have “.vim” extension.
After that you can do pretty much anything with your computer (close all windows, reboot, etc.) as long as you do not deleted the file saved above. You can restore the session while you are in vim by using:
:source saved_session.vimor if you want to start vim with the saved session, use this
$ vim -S saved_session.vimin the command line.
Application: (Directly copied from here)
The obvious way to use sessions is when working on different projects. Suppose you store you session files in the directory "~/.vim". You are currently working on the "secret" project and have to switch to the "boring" project: :wall :mksession! ~/.vim/secret.vim :source ~/.vim/boring.vim This first uses ":wall" to write all modified files. Then the current session is saved, using ":mksession!". This overwrites the previous session. The next time you load the secret session you can continue where you were at this point. And finally you load the new "boring" session.
[linux] How to install a package from the source code
Installing a package (AKA software) from source code is often considered to be the secret cult of the worshipers of the Geek God. But, you know, it’s not that difficult. Let’s see how we can do it.
First, some preliminaries. Why should I build it form the source code? Well, source codes are more or less platform independent. If you have a suitable compiler and necessary libraries, you may install it on any platform of your choice. Source code files are available in ASCII plain text format (not binaries) — so you can study it and understand what it does and whether/ how it may affect your system. Reading the description of a software distributed as a binary does not even come close to that — you really don’t know what that piece of software does until you actually run it. Needless to add, you may include your changes to the open-source code to suit your needs as well in accordance with the relevant license.
Secondly, where can I find the source code? The source code for a open-source package is often distributed in compressed format (i.e., with an extension “tar.gz”, “tgz” or “tar.bz2” — that’s why it’s also called tarball). You may download the tarball from a reliable source, e.g. homepage of the package developer, Free Software Directory, Open Source Software directory, linux.org, Ubuntu archive, Darwin ports, Fink and Open Source Apple website. Last three are specifically intended for OSX running Darwin. Wikipedia also has a nice list of Free and Open Source softwares.
Now, let’s do the installation step-by step. Create a directory (say, ~/packages) where you want to save the packages, navigate to that directory, save the tarball (say, newpack.tar.gz) there and uncompress it:
$ tar zxvf newpack.tar.gzReplace “zxvf” by “jxvf” if it has a “tar.bz2” extension instead of “tar.gz” or “tgz”.
Then navigate to the uncompressed directory (usually newpack, but be sure of that first using the ls command) using
$ cd newpackand read the instruction files (e.g., INSTRUCTIONS, INSTALL, README), if any, for special instructions using your favorite text editor. Otherwise, the normal procedure for installation is the following series of commands: (wait for each one to finish before you issue the next command!)
$ ./configure
$ make
$ sudo make install You need to enter the superuser password in the last step as it copies the executable in the system’s executable directory (e.g., /bin, /sbin/). That’s it!
However, you may want to clean up the mess (especially the object files) created by the installation process using:
$ make cleanIn a spree of cleaning up, however, don’t delete the file named Makefile: you need this if you want to uninstall the package (the above command won’t delete it).
In order to uninstall the package, navigate to the same directory (~/packages/newpack/), and issue the following command:
$ sudo make uninstallYou need to enter the superuser password for this.
Reference: here.
[cli] Find out the public IP in the command line
You may find out your IP using commands like ifconfig, but if your computer is behind a firewall, that is not the IP that the world sees. Here’s a quick how-to to find out what your external IP is.
First, let’s create an alias: (link)
alias getip="wget -q -O - http://whatismyip.com/automation/n09230945.asp"Next time when you need the public IP of your computer, just type getip in the command line and hit enter.
The above command gets the IP from http://whatismyip.com/automation/n09230945.asp (just copy and paste this URL to your location bar and hit enter, and see what it does!). The “-q” is to suppress verbose information (quiet mode) and “-O -” causes the output to be written to STDOUT.
You may use dyndns.org too to find out the IP, but in this case you may need some trimming: substitute the text within the code above by the following:
wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1
A few words about various flags above:
The output of the first command (everything before the first pipe) is
<code><html><head><title>Current IP Check</title></head><body>Current IP Address: xxx.xxx.xx.xx</body></html>
(the actual IP address is masked by x).
The “-o /dev/null“ part redirects the STDERR of wget to /dev/null. The “-d :” option in first “cut” tells it to use colon (:) to be used as the delimiter and “-f 2” causes it to print second of the delimited fields. Similarly, the flags of the second “cut” cause it to use “<” as the delimiter for the piped output from the first “cut” and choose the first of the delimited fields.
UPDATE: Another one using curl [link]. You may again change the quoted text within the alias above by the following:
curl --connect-timeout 3 http://www.whatismyip.org/
[unix] Change default login shell
I just stumbled upon a few ways to change the default login shell to bash:
The easiest one is
$ chsh -s /bin/bash username
On some machines you may have to use ypcsh instead of chsh. The full path of the shell executable must be specified.
But if you have root access you may have the option to use either of the following two as well:
$ usermod -s bash username- Change it by editing the entry for the user in
/etc/passwdfile, manually.
If none of the above works, here’s a workaround!
N.B. Make sure that the desired shell is listed in /etc/shells file.

1 comment