Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Saturday, December 15, 2012

Determine your BogoMIPS

Yeah, BogoMIPS are bogus, but it's a lot of fun to know how much you have in your computer. Usually you'll find this line to tell you how much you have:
dmesg | grep - 'bogomips'
(or something worse). This is a bad command line because if you happen to have an big uptime (today my uptime is 48 days, which is quite low), this command will not work as most of the kernel's logs have been flushed somewhere else. This is a bad command line because it's not fun at all!
I like dc a little bit, and here's how you can add a lot of numbers using dc:
gniourf@somewhere$ dc <<< '[+]sa[z2!>az2!>b]sb1 2 3 4 5 6 6 5 4 3 2 1lbxp'
42
gniourf@somewhere$ 
Cool eh? Please, read man dc to learn about registers and all that in dc.
To determine your BogoMIPS, you just can do this:
gniourf@somewhere$ dc <<< '[+]sa[z2!>az2!>b]sb'"$(sed -n '/bogomips/Is/.*://p' /proc/cpuinfo)"'lbxp'
10374.47
gniourf@somewhere$
If you want the average BogoMIPS per processor, do:
gniourf@somewhere$ dc <<< '2k[+]sa[z2!>az2!>b]sb'"$(sed -n '/bogomips/Is/.*://p' /proc/cpuinfo)"'zsclbxlc/p'
5187.23
gniourf@somewhere$
I wrote this note because I stumbled by accident on the most terrible command line you could think of. Just for posterity and as an example of something you should never ever do, here it is:
dumbo@moronity$ echo $(echo "print ((($( cat /proc/cpuinfo | grep bogomips | awk '{print $3}' | tr -s '\n' '+')0)/$(echo 4))+($( cat /proc/cpuinfo | grep bogomips | awk '{print $3}' | tr -s '\n' '+')0))/2" | python)
By respect to the author of this junk, I will not give you his name.


Exercise.
  1. Count how many pipes, subshells and process forks there are in the previous command line.
  2. Count how many Useless Uses of Cats there are.
  3. Conclusion? (Hint: look at the login)

Cheers!

Saturday, November 12, 2011

Never parse the output of ls, ps, etc... Use globs or find or something else!

It's all in the title!
So, you want to delete all files that end with .back, and (because you read a terrible advanced bash guide) you're tempted to do this:
gniourf@somewhere:~$ for i in `ls *.back`; do rm $i; done
The problem is that this will miserably fail whenever a filename contains a space. (Try it!). Also, the backticks are ugly (but it's not the worse thing here). Instead, do:
gniourf@somewhere:~$ for i in *.back; do rm "$i"; done
(also observe the quotes). You should realize that bash's globs are infinitely better than parsing the output of a program. If you want to use something like this in a script, maybe you should shopt -s nullglob or shopt -s failglob. For example, try:
gniourf@somewhere:~$ echo *hello # assuming that there's no file ending with hello
The output will be something like:
*hello
Because when a glob doesn't match, it is used verbatim. If you want a non-matching glob to expand to nothing, you must set nullglob on thus:
gniourf@somewhere:~$ shopt -s nullglob
Now,
gniourf@somewhere:~$ echo *hello
outputs nothing.
If you want a non-matching glob to generate an error, you must set failglob on thus:
gniourf@somehwere:~$ shopt -s failglob
Now,
gniourf@somewhere:~$ echo *hello
bash: no match: *hello


Cheers!