Tag Archives: bash

Telling bash to step aside for zsh

So, let’s say that you want to change your shell to zsh, but fall back to bash if it isn’t available on whatever system you’re using. This is useful if you use something like NIS or LDAP with home directory NFS, since you’ll be sshing around and bringing your .bashrc with you everywhere. The solution is pretty simple – just add this to the bottom of your .bashrc:

hash zsh 2>&- && exec zsh

Update: This breaks X-windows. Need to figure out why…
Update: My esteemed colleague informed me that I should check for interactive shell, which is a dead give-away for X:

if [ ! -z $PS1 ]; then
  hash zsh 2>&- && exec zsh -l

1,000,000th Fibonacci Number One-Liner in C

This is possibly the best one-liner I’ve ever written:

gcc -x c -o /tmp/out - -lgmp <<< '#include <stdlib.h> 
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <gmp.h>
void omg_i_love_leonardo_of_pisa(uint32_t num, mpz_t * result) { mpz_t retval, last, tmp; mpz_init(retval);
 mpz_init(last); mpz_init(tmp); uint32_t i = 1; if(num == 0) return; mpz_set_ui(retval, 1U); 
mpz_set_ui(last, 0U); for(; i < num; i++) { mpz_set(tmp, retval); mpz_add(retval, retval, last); 
mpz_set(last, tmp); } mpz_set(*result, retval); } int main() { uint32_t num; mpz_t fibo; mpz_init(fibo);
omg_i_love_leonardo_of_pisa(1000001, &fibo); mpz_out_str(stdout, 10, fibo); printf("\n"); return 1; }
' && time /tmp/out

It compiles a C program given from STDIN, puts it in /tmp/out, and runs it with time to find the time it takes to run. It generates the 1,000,000th Fibonacci number. Try it!

Update May 21, 2011

I changed the algorithm to do a matrix multiplication trick. The only problem is it goes over the number you ask for currently. I'm going to fix this with memoization soon.

gcc -x c -o /tmp/out - -lgmp <<< '#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmp.h>
void print_state(mpz_t* fm2, mpz_t* fm1, mpz_t* f, uint32_t n){gmp_printf("fib(%d) = %Zd\n", n, f);}
#define NEXT_FIB() mpz_set(oldfm1, fm1);mpz_set(oldf, f);mpz_mul(f, f, f);mpz_mul(tmp, fm1, fm1);\
mpz_add(f, f, tmp);mpz_mul(fm1, oldf, fm1);mpz_mul(tmp, oldfm1, fm2);mpz_add(fm1, fm1, tmp); \
mpz_set(tmp, fm2);mpz_mul(fm2, oldfm1, oldfm1);mpz_mul(tmp, tmp, tmp);mpz_add(fm2, fm2, tmp);\
n += i;i *= 2;
int main(){mpz_t fm2, fm1, f;uint32_t n = 2;uint32_t i = 1;mpz_inits(fm2, fm1, f, NULL);mpz_set_si(fm2,
0);mpz_set_si(fm1, 1);mpz_set_si(f, 1);mpz_t oldf, oldfm1, tmp;mpz_inits(oldf, oldfm1, tmp, NULL);
uint32_t g = 1000000;while(n<g){NEXT_FIB();}print_state(&fm2, &fm1, &f, n);return 0;}' && time /tmp/out

This outputs almost immediately on my Intel Atom:

fib(1048577) = 19202837189514814.................

real	0m0.840s
user	0m0.280s
sys	0m0.010s

The code is here. Feel free to fork and improve!

Update August 30, 2013

I ended up looking at this again, and I improved it immensely. Apparently, gmp has built-in fibo functions (!!):

int main()
  int n = 1000000;
  mpz_t fm2;
  mpz_inits(fm2, NULL);
  mpz_fib_ui(fm2, n);
  gmp_printf("fib(%d) = %Zd\n", n, fm2);
  return 1;

This produces a number MUCH faster than the above implementation. It also makes a nicer oneliner:

gcc -x c -o /tmp/out - -lgmp <<< '#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmp.h>
int main() { int n = 1000000; mpz_t fm2; mpz_inits(fm2, NULL); mpz_fib_ui(fm2, n); gmp_printf(\"fib(%d) = %Zd\n\", n, fm2); return 1; }" && time /tmp/out

real    0m0.057s
user    0m0.040s
sys     0m0.008s

Putting Non-YouTube Videos in MxTube

So, you have your Jailbroken iPod Touch or iPhone with 2.0 firmware running OpenSSH, and you have MxTube 1.5 or better, of course. What if you want one of those pesky videos that YouTube deletes all the time, like Paris Hilton For President? Or maybe you want a full movie on your iPod without having to sync to iTunes since you’re stuck in Linux Land. Well, here’s how:

Download the video

This can be done lots of ways, but somehow obtain a version of the video that ffmpeg can read. It can be just about any format. Let’s assume that we get a file called paris.flv.

Convert the video

ffmpeg -i paris.flv -b 500000 -s 176x144 -ac 1 -ab 64000 paris-high.mp4

This will make a nice movie for us to play. Let’s put it on the iPod:

scp paris-high.mp4 root@ipod:/var/mobile/Media/MxTube/

Make the Thumbnail

We need to grab a frame from the middle of the movie. Let’s use mplayer for that!

mplayer -vo jpeg -frames 1 -ss 30 paris.flv

This will make a file called 00000001.jpg. Let’s put that in the right spot:

scp 00000001.jpg root@ipod:/var/mobile/Media/MxTube/paris.thm

Add the video to the Library

Edit /var/mobile/Library/MxTube/VideoLibrary.plist on the iPod. Make a new dict entry in the list like so:

                <string>Paris for President</string>

Just fill in the right filenames for the thumbnail and movie like you used above. Also, change the author to yourself or the original director, and the duration accordingly. Keep the id something random. Also, you can change the display title as the last option there.

Other Pro Tips

Keep in mind that if you want to be able to delete the movie from the interface, you need to change the ownership on all the files to mobile:mobile using chown.


Well, it totally works, with sound even.

Paris is at the Bottom

Here's Paris in Full Glory

Good luck!

Putting Images on the iPod Touch from Linux


I wanted to put images on my iPod Touch without using iTunes since, as most of us know, there is no good way to use it from Linux. It turns out there is a magic directory on the iPod Touch where it saves images from Safari. I simply looked at how it saved them, and applied it on my box here at home.


  • Jailbroken iPod Touch (I used WinPWN), with OpenSSH installed and working (the root password is alpine)
  • Firmware 2.0 (Though, 1.x may work, I just haven’t tried it)
  • A Linux box with a bash shell
  • Cool images
  • convert and mogrify from ImageMagick installed


These instructions might work for the iPhone as well. YMMV

All of your images must be in the format IMG_XXXX.YYY where XXXX is a number < 9999, and YYY is either JPG or THM (THM is a thumbnail). To rename our files, I use a simple trick I outlined in my last post:

EII=4; for i in *.jpg; do ls $i; \
NEWNAME=IMG_00`printf "%02d" $EII`.JPG; \
echo Renaming $i to $NEWNAME; \
mv $i $NEWNAME; EII=`expr $EII + 1`; done

That will rename all the JPEG files in order from 4 to, in my case, 62. Now, I have to make the thumbnails:

for i in `ls *.JPG | cut -d '.' -f 1`; do \
convert $i.JPG -resize 75x75! $i.THM; \

The 75×75! part makes sure they are exactly those dimensions. You end up with something like the following:

IMG_0004.JPG  IMG_0016.JPG  IMG_0028.JPG  IMG_0040.JPG  IMG_0052.JPG
IMG_0004.THM  IMG_0016.THM  IMG_0028.THM  IMG_0040.THM  IMG_0052.THM
IMG_0005.JPG  IMG_0017.JPG  IMG_0029.JPG  IMG_0041.JPG  IMG_0053.JPG

Now, note that the iPod can only display images under 100KB (to my knowledge). If your high-res image is too large, it will just display the magnified 75×75, which is really ugly. So, make sure all your images fit that description, and if they don’t, mogrify -resize them until they do (or take other measures as necessary). I used this:

mogrify -resize 400x *.JPG

All my images turned out to be between 30 and 90 KB. This also keeps the aspect ratio, unlike the 75×75!.

Now, SSH into your iPod (if you can’t do this yet, google it). You should have the following file:


Open this file in vi, and observe the plist>dict>integer part of the hierarchy:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

So, I put 62 in there since the last image I have is numbered 0062. It’s a very simple idea. Now, just load them onto the iPod:

scp IMG_00* root@ipod:/var/mobile/Media/DCIM/100APPLE/

Let’s have a look from the iPod console!

HanksTouch:/var/mobile/Media/DCIM/100APPLE root# ls
IMG_0002.JPG  IMG_0014.THM  IMG_0027.JPG  IMG_0039.THM  IMG_0052.JPG
IMG_0002.THM  IMG_0015.JPG  IMG_0027.THM  IMG_0040.JPG  IMG_0052.THM

Beautiful. Now, to test it…

Renaming Files Sequentially with BASH

I made a cool one-liner in bash today that renames files sequentially. Here’s an example that takes all the JPEGs in a directory and renames them in order:

EII=4; for i in *.jpg; do ls $i; \
NEWNAME=IMG_00`printf "%02d" $EII`.JPG; \
echo Renaming $i to $NEWNAME; \
mv $i $NEWNAME; EII=`expr $EII + 1`; done

Pretty cool, huh? This is in relation to my next post. Keep watch…