Sometimes it's all about the aesthetics.

Showing posts with label UNIX. Show all posts
Showing posts with label UNIX. Show all posts

4/02/2010

Hello GIT

git clone ssh://laptop.local/Users/mjhsieh/amber11

8/14/2009

awk stuff

AWK can also be used to generate lazy code.

$ awk '/Block inner limits/{print $4,$5}' b37.out | awk '\
? NR==1        {printf "if      ( h*i+gox >=%6.1f .and. h*i+gox <=%6.1f .and. &\n",$1,$2} \
? NR>1&&NR%3==1{printf "else if ( h*i+gox >=%6.1f .and. h*i+gox <=%6.1f .and. &\n",$1,$2}\
? NR%3==2      {printf "          h*j+goy >=%6.1f .and. h*j+goy <=%6.1f .and. &\n",$1,$2}\
? NR%3==0      {printf "          h*k+goz >=%6.1f .and. h*k+goz <=%6.1f ) then\n"$1,$2}\
? NR%3==0      {printf "   write(%d,\47(4f)\47) mytmp,h*i+gox,h*j+goy,h*k+goz\n",8000+NR/3}'

abs() in awk command

It has been very annoying that there is no intrinsic absolute function in awk, but it's still easy to do it by ourself:

$ paste tmp1/energy.txt tmp2/energy.txt tmp3/energy.txt tmp4/energy.txt \
? | awk 'function abs(x){return (((x < 0.0) ? -x : x) + 0.0)}\
? {printf "%s %25.12f %25.12f %25.12f %25.12f\n", $1,\
? abs($2-$2),abs($4-$2),abs($6-$2),abs($8-$2)}'

7/05/2009

Package things in Leopard? Meh.

Right now there are at least two type of official package registering method in Mac OS X Leopard. The old style of package registering involves a bundled directory with literally no good way to manage it. The new style of package registry is a sqlite3 database, which is potentially a nice one. However according to the manual of pkgutil:

     --forget package-id
                 Discard all receipt data about package-id, but do not touch
                 the installed files.  DO NOT use this command from an
                 installer package script to fix broken package design.
SO YOU ARE NOT SUPPOSED TO FIX WHAT APPLE SHOULD HAVE DONE ALREADY.

Alright, I can still do a query tool, right?

Here it is.

Notes:

  1. BASH is not enough. Perl and PerlObjCBridge were use read plist data.
  2. It seems to be not allowed to read the database file without administration privilege, and the framework seems to be private.
  3. If you know how to get the installation time from the old fashion Archive.bom/Info.plist combination, please comment. I'd love to add that into the tool.

6/29/2009

How to f*ck your CJK input in Terminal.app.

prerequisite

You being able to type CJK in the shell window under Terminal.app.
  1. Launch Terminal.app
  2. Create a bash script file, say test.bash, with this content:
    #!/bin/bash
      while true 
      do 
        tput sc
        tput cup 0 60
        echo -en `date +"%H:%M:%S %F"`
        tput rc
        sleep 1
      done
  3. Launch the script in the background:
    $ bash test.bash&
  4. Type-in some candidate CJK words without committing it into the shell buffer.
  5. Now you see your candidate buffer disappearing and showing somewhere you didn't expect.
Later I will put some screenshots to illustrate the problem.

Analysis

This happens when programs issue screen location instructions, and this causes an event that having the input methods dropping the input buffer all together. Different behaviors are observed on Mac OS X 10.4's Terminal.app, the location of the input buffer still shows up in the wrong place, but the buffer did not disappear!

4/23/2009

Forward: How to use screen and irssi

I am glad that this has been written already! [Guide to using Screen and irssi]

note: useful applescript closing microsoft word

$ osascript -e 'tell application "Microsoft Word" to close documents saving yes'

12/03/2008

Completing Nally's Terminal Emulation


This is linux's top running with Nally's terminal emulation display. So far only top/vim/man are really tested.

10/18/2008

Useless, not so cool rsync trick

rsync -e 'ssh -x host1 ssh' -avx --delete --bwlimit=1024 --exclude '*.spotlightIndex' host2:Documents/Papers/ Documents/Papers/

This let you synchronize files if you can't connect host2 directly and don't want to transfer it twice. However beware of the broadband problem since you might trigger DoS protection on host1... my solution is to add '--bwlimit=1024' ...

NETDEV WATCHDOG: eth0: transmit timed out
tg3: eth0: transmit timed out, resetting
tg3: tg3_stop_block timed out, ofs=2c00 enable_bit=2
tg3: tg3_stop_block timed out, ofs=1400 enable_bit=2
tg3: tg3_stop_block timed out, ofs=c00 enable_bit=2
tg3: tg3_stop_block timed out, ofs=4800 enable_bit=2
tg3: eth0: Link is down.
tg3: eth0: Link is up at 100 Mbps, full duplex.
tg3: eth0: Flow control is off for TX and off for RX.
...
...

4/24/2008

Placebo to squeeze more usable physical memory

Well, you can just allocate a big chunk of memory and release it to push some idle process to the virtual memory (swap).

program bigmem
   implicit none
   double precision, allocatable :: thebigmem(:)
   integer ierr

   ierr = 0
   allocate ( thebigmem(1:195660000), stat=ierr )
   if (ierr/=0) then; print *,"error   allocating"; call exit(1); else; continue ; endif
   thebigmem = 0
   deallocate ( thebigmem, stat=ierr)
   if (ierr/=0) then; print *,"error deallocating"; call exit(1); else; continue ; endif
   print *,"success"
end program
But it's still like a placebo.

2/04/2008

dscacheutil -flushcache

dscacheutil -flushcache
This leopard command help you to reset corrupted DNS record that Safari refuses to forget.
lookupd -flushcache
Its equivalent command in tiger.

1/29/2008

Nice Troll

12:18 < abc> xyz: hey man, i love freebsd as much as the next guy, but its time to face reality
12:18 < abc> xyz: there's also the issues of copyright indemnification
12:19 < abc> xyz: its a proven fact that linux torvalds borrowed AT&T code when he wrote FreeBSD
12:19 < abc> it was proven in court!
12:19 < abc> and you're a corporation running freebsd servers, you need support. who are you going to call? GNU?
12:20 < abc> i need support for windows servers... i call MS on the phone, give them my credit card number, and they fix my problem right there... BAM!
Following figure is totally irrelevant.
niceboatld0.jpg

1/22/2008

A history file that crashes your tcsh in leopard

[localhost:~] johndoe% tcsh --version
tcsh 6.14.00 (Astron) 2005-03-25 (powerpc-apple-darwin) options wide,nls,dl,al,kan,sm,rh,color,filec
[localhost:~] johndoe%
[localhost:~] johndoe% bash
localhost:~ johndoe$ cd
localhost:~ johndoe$ mkdir backup.tcshtest
localhost:~ johndoe$ mv .history .login* .cshrc* .tcsh* backup.tcshtest/
localhost:~ johndoe$ curl http://mjhsieh.googlepages.com/history-crash -o .history
localhost:~ johndoe$ bash -c 'LANG=en_US.UTF-8 tcsh'
Segmentation fault
localhost:~ johndoe$ bash -c 'LANG=C tcsh'
[localhost:~] johndoe%
[localhost:~] johndoe% exit
localhost:~ johndoe$ rm .history
localhost:~ johndoe$ bash -c 'LANG=en_US.UTF-8 tcsh'
[localhost:~] johndoe%
[localhost:~] johndoe% exit
localhost:~ johndoe$ mv backup.tcshtest/.??* .
localhost:~ johndoe$ exit
[localhost:~] johndoe%
    Reference:
  1. https://bugs.launchpad.net/ubuntu/+source/tcsh/+bug/86683

12/11/2007

Understanding DGETRF/SGETRF interface

I was working on the inverse matrix the other day, it can be done through DGETRI of LAPACK. The DGETRI requires DGETRF to do LU decomposition first in order to calculate the inverse matrix. Then I found DGETRF interesting to start with since it has been awhile for me to touch linear algebra stuff. I wrote a "hello world" code for it. I was stuck on the issue to construct permutation matrix from IPIV, basically IPIV is not like vecP in Mathematica. This is how I understand it, I am not confident about the P matrix, this requires some LAPACK friends' help. On Mac OS X, just do g95 -framework Accelerate hello.f90 -o hellolapack and run the program by ./hellolapack , sorry I am too lazy to install a software lapack. :p

program hello
   integer INFO
   integer IPIV(3)
   real*8 A(3,3),B(3,3),C(3,3),D(3,3)
   real*8 P(3,3),L(3,3),U(3,3)
   real*8 myVEC(3)
   real*8 WORK(64*3)
   A(1,1)=1d0
   A(1,2)=2d0
   A(1,3)=2d0
   A(2,1)=2d0
   A(2,2)=7d0
   A(2,3)=7d0
   A(3,1)=2d0
   A(3,2)=7d0
   A(3,3)=9d0
   P=0d0
   P(1,1)=1d0
   P(2,2)=1d0
   P(3,3)=1d0
   ! dgetrf(M,N,A,LDA,IPIV,INFO)
   ! input
   ! M          The number of rows of the matrix A.  M >= 0. Integer
   ! N          The number of columns of the matrix A.  N >= 0. Integer
   ! A          The DP array
   ! output
   ! A          The DP array
   ! LDA        The leading dimension of the array A.  LDA >= max(1,M).
   ! IPIV       INTEGER array, dimension (min(M,N))
   !            The pivot indices; for 1 <= i <= min(M,N), row i of the
   !            matrix was interchanged with row IPIV(i).
   print *,"A"
   write (*,'(F8.3,F8.3,F8.3)') A(1,1:3)
   write (*,'(F8.3,F8.3,F8.3)') A(2,1:3)
   write (*,'(F8.3,F8.3,F8.3)') A(3,1:3)
   B=A
   call dgetrf(3,3,A,3,IPIV,INFO)
   if (INFO.ne.0) stop
   !IPIV(1)= 3 implies that the first and third rows were interchanged
   !when factoring the first column; IPIV(2)= 3 implies that the second
   !and third rows were interchanged when factoring the second column.
   !In this case, IPIV(3) must be 3 because there are only three rows.
   if (IPIV(1).eq.3) then
      myVEC=P(1,1:3)
      P(1,1:3)=P(3,1:3)
      P(3,1:3)=myVEC
   elseif(IPIV(1).eq.2) then
      myVEC=P(1,1:3)
      P(1,1:3)=P(2,1:3)
      P(2,1:3)=myVEC
   endif
   if (IPIV(2).eq.3) then
      myVEC=P(2,1:3)
      P(2,1:3)=P(3,1:3)
      P(3,1:3)=myVEC
   elseif(IPIV(2).eq.1) then
      myVEC=P(2,1:3)
      P(2,1:3)=P(1,1:3)
      P(1,1:3)=myVEC
   endif
   print *,"P"
   write (*,'(F8.3,F8.3,F8.3)') P(1,1:3)
   write (*,'(F8.3,F8.3,F8.3)') P(2,1:3)
   write (*,'(F8.3,F8.3,F8.3)') P(3,1:3)
   print *,"L"
   L(1,1)=1d0
   L(1,2:3)=0d0
   L(2,1)=A(2,1)
   L(2,2)=1d0
   L(2,3)=0d0
   L(3,1:2)=A(3,1:2)
   L(3,3)=1d0
   write (*,'(F8.3,F8.3,F8.3)') L(1,1:3)
   write (*,'(F8.3,F8.3,F8.3)') L(2,1:3)
   write (*,'(F8.3,F8.3,F8.3)') L(3,1:3)
   print *,"U"
   U(1,1:3)=A(1,1:3)
   U(2,1)=0d0
   U(2,2:3)=A(2,2:3)
   U(3,1:2)=0d0
   U(3,3)=A(3,3)
   write (*,'(F8.3,F8.3,F8.3)') U(1,1:3)
   write (*,'(F8.3,F8.3,F8.3)') U(2,1:3)
   write (*,'(F8.3,F8.3,F8.3)') U(3,1:3)
   call dgemm("N","N",3,3,3,1d0,L,3,U,3,0d0,C,3)
   print *,"L.U"
   write (*,'(F8.3,F8.3,F8.3)') C(1,1:3)
   write (*,'(F8.3,F8.3,F8.3)') C(2,1:3)
   write (*,'(F8.3,F8.3,F8.3)') C(3,1:3)
   call dgemm("N","N",3,3,3,1d0,P,3,L,3,0d0,C,3)
   call dgemm("N","N",3,3,3,1d0,C,3,U,3,0d0,D,3)
   print *,"P.L.U"
   write (*,'(F8.3,F8.3,F8.3)') D(1,1:3)
   write (*,'(F8.3,F8.3,F8.3)') D(2,1:3)
   write (*,'(F8.3,F8.3,F8.3)') D(3,1:3)

   call dgetri(3,A,3,IPIV,WORK,64*3,INFO)
   if (INFO.ne.0) stop
   call dgemm("N","N",3,3,3,1d0,B,3,A,3,0d0,C,3)
   print *,"A.A^-1"
   write (*,'(F8.3,F8.3,F8.3)') C(1,1:3)
   write (*,'(F8.3,F8.3,F8.3)') C(2,1:3)
   write (*,'(F8.3,F8.3,F8.3)') C(3,1:3)
end program hello

12/08/2007

sh/ksh/bash redirecting the stdout and stderr

This seems to belong to "Wizard Level I/O"......orz
Recently g95 croaks some warning on Mac OS X platform like:

% g95 test.f90 
/var/tmp//ccXFPLrz.s:59:indirect jmp without `*' 
... 
It seems to be a known issue and the compiler still builds working binaries (assembly code? not sure). It is reasonable to get a wrapper script that filters out the warning. It turns out to be not as easy as I thought:
#!/bin/sh
myg95="/opt/g95-install/bin/i386-apple-darwin*g95"
exec 3>&1
$myg95 $* 2>&1 >&3 | grep -v "indirect jmp without " 1>&2
exec 3>&-
This duplicates stdout to another file descriptor (3), then runs the g95 command with stderr redirected to stdout and stdout redirected to the saved descriptor (3). The result is piped into filter command to get rid of irreverent errors. The output of the pipeline is redirected back to stderr, so that stdout and stderr of the script as a whole are what it is expected.

10/21/2007

Shell Script in An OSX Bundle And More

Dr. Ping Yeh brought up a question about how to put a shell script in the Dock of Mac OS X. My original thought about this question is to solve it with a shell script with a file extension/type *.command and it can be run by double clicking it. Open the *.command files and the Terminal.app will launch the shell to run it. But the problem becomes the limitation of the left side of the Dock, a item has to be an "application" to sit in the left side of the Dock. (At the time we didn't work it thoroughly and forgot the right side the Dock... The thing is, you can just put the *.command file in the right side of the separator and click to launch it.)
filedock

Just put it in the right side of the Dock.

You have to wrap your shell script into an application if you want it to stay in the left side of the Dock. Luckily we can follow the procedure in this document by Artem Fishman (via google search).
Basically the bundle structure looks like this:
MyBundle.app
MyBundle.app/Contents
MyBundle.app/Contents/Info.plist
MyBundle.app/Contents/MacOS
MyBundle.app/Contents/MacOS/MyBundle
MyBundle.app/Contents/MacOS/startup.command
MyBundle.app/Contents/Resources
MyBundle.app/Contents/Resources/MyIcons.icns
(The name "MyIcons" is defined in Info.plist file.)
MyBundle

MyBundle.app in my Dock.

However that document only helps you run the script silently. It's not enough if you want to be able to read the output of the shell script or even interact with the shell script. A better way to do it is just simply writing your main script in a command file inside the bundle and "open" the command like this:
#!/bin/sh
#File:  MyBundle.app/Contents/MacOS/MyBundle
mypath=`dirname "$0"`
open $mypath/starup.command
With this command file:
#!/bin/sh
#File:  MyBundle.app/Contents/MacOS/startup.command
echo "this is a test"
/bin/sleep 5
This will give a terminal window with the output of "this is a test" and 5 seconds delay. Now you have a working application that can be put on the dock and also give you an opportunity to read the output!
Completed Command

After the command is completed, the terminal leaves a dummy window.


vmd

VMD in action, you can see there is a console window utilizing Terminal.app.

Unfortunately this new application doesn't close the terminal window automatically. You got a "Completed Command" window after the application finished and it requires you to close it manually. This had been my major quest since the first time I knew about the *.command files. I always want to improve a program called VMD, a program that shells-out a console window but leaves that window half dead after finished all the task. This time I decided to finish this quest. I started with closing the window in the front or idle window with Applescript but the side-effects are bad --- either logically bad or bugs on Terminal.app . Later I have to abandon the idea of using "open" command asking Terminal.app to open the script.
Yay! Eventually I did it.
#!/bin/sh
#File:  MyBundle.app/Contents/MacOS/MyBundle
mypath=`dirname "$0"`
osascript << EOT
tell application "System Events" to set terminalOn to (exists process "Terminal")
tell application "Terminal"
   if (terminalOn) then
        activate
        do script "\"$mypath/startup.command\"; exit"
   else
        do script "\"$mypath/startup.command\"; exit" in front window
   end if
end tell
EOT
exit 0
Time to fix the VMD.
(Update: This demo bundle is now available for download.)
(Update: It turns out that if you already have Terminal.app running, clicking the command files on right side of the dock might not be working.)
(Update: In order to make the trick work, you still need to set your terminal.app's shell preference to "close only if the shell exited cleanly".)
(Update: For processing arguments and drop actions, use the DropScript by Wilfredo Sánchez, updated source can be found through SVN repository. Also checkout his introduction.)
(Update: Some shell scripts are suitable for contextual menu items. Check the website out.)

10/20/2007

Asus EeePC

Via Knight Feng, 咿咿咿批西 入手!

processor : 0
vendor_id : GenuineIntel
cpu family : 6
model  : 13
model name : Intel(R) Celeron(R) M processor          900MHz
stepping : 6
cpu MHz  : 900.000
cache size : 512 KB
fpu  : yes
fpu_exception : yes
cpuid level : 2
wp  : yes
flags  : fpu vme de pse tsc msr mce cx8 apic sep mtrr pge mca cmov pat clflush dts acpi mmx fxsr sse sse2 ss tm pbe
bogomips : 1261.26
clflush size : 64

MemTotal:       508328 kB

00:00.0 Host bridge: Intel Corporation Mobile 915GM/PM/GMS/910GML Express Processor to DRAM Controller (rev 04)
00:02.0 VGA compatible controller: Intel Corporation Mobile 915GM/GMS/910GML Express Graphics Controller (rev 04)
00:02.1 Display controller: Intel Corporation Mobile 915GM/GMS/910GML Express Graphics Controller (rev 04)
00:1b.0 Audio device: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) High Definition Audio Controller (rev 04)
00:1c.0 PCI bridge: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) PCI Express Port 1 (rev 04)
00:1c.1 PCI bridge: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) PCI Express Port 2 (rev 04)
00:1c.2 PCI bridge: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) PCI Express Port 3 (rev 04)
00:1d.0 USB Controller: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB UHCI #1 (rev 04)
00:1d.1 USB Controller: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB UHCI #2 (rev 04)
00:1d.2 USB Controller: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB UHCI #3 (rev 04)
00:1d.3 USB Controller: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB UHCI #4 (rev 04)
00:1d.7 USB Controller: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB2 EHCI Controller (rev 04)
00:1e.0 PCI bridge: Intel Corporation 82801 Mobile PCI Bridge (rev d4)
00:1f.0 ISA bridge: Intel Corporation 82801FBM (ICH6M) LPC Interface Bridge (rev 04)
00:1f.2 IDE interface: Intel Corporation 82801FBM (ICH6M) SATA Controller (rev 04)
00:1f.3 SMBus: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) SMBus Controller (rev 04)
01:00.0 Ethernet controller: Atheros Communications, Inc. AR5007EG 802.11 b/g Wireless PCI Express Adapter (rev 01)
03:00.0 Ethernet controller: Atheros Technology Corp. L2 100 Mbit Ethernet Adapter (rev a0)
Bus 005 Device 003: ID eb1a:2761 eMPIA Technology, Inc. 
Bus 005 Device 002: ID 0cf2:6225 ENE Technology, Inc. 
Bus 005 Device 001: ID 0000:0000  
Bus 004 Device 001: ID 0000:0000  
Bus 003 Device 001: ID 0000:0000  
Bus 002 Device 001: ID 0000:0000  
Bus 001 Device 001: ID 0000:0000

[   15.618272] eeepc_hotk: module license 'unspecified' taints kernel.
[   15.618901] Asus EEEPC HOTKEY
[   15.619223] [eeepc hotk] Hotkey init flags 0x41.
[   15.620795] [eeepc hotk] Get control methods supported : 0x101711

10/16/2007

Fun Toy under Terminal Emulator

Just got termatrix compiled in my powerbook, it looks like this toy can be used as a terminal emulator benchmark.
termatrix
As I expected, xterm eats the least amount of CPU resource.

10/08/2007

Porting ASTROLOG to Mac OS X.

The other days I was trying to port ASTROLOG to Mac OS X, the only official option for Mac OS X is thru X11 display. However you still need to modify the Makefile to make it work.

Before you port/compile the program by yourselves, you might want to try "Baby Astrolog"

If recompiling and X11 is what you want (since X11 version does have more functions). Try to use my patch to the Makefile. This patch is based on the UNIX source v5.4 of ASTROLOG:

The patch.

Also check momizi's astrolog story

Simple SRT subtitle time shift with awk.

Call me an one-line awk command freak. (sorry, it only processes integer seconds.)

awk -v shiftsec=2 '{\
    if(/:..:.*:..:/){\
        split($1,s1,","); split($3,s2,",");\
        split(s1[1],s1t,":"); split(s2[1],s2t,":");\
        time1=((s1t[1]*60+s1t[2])*60+s1t[3])+shiftsec;\
        time2=((s2t[1]*60+s2t[2])*60+s2t[3])+shiftsec;\
        ss1=time1%60;mm1=((time1-ss1)/60)%60;hh1=(time1-ss1-mm1*60)/60/60;\
        ss2=time2%60;mm2=((time2-ss2)/60)%60;hh2=(time2-ss2-mm2*60)/60/60;\
        printf("%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n",\
               hh1,mm1,ss1,s1[2],hh2,mm2,ss2,s2[2]);\
      }else{\
        print $0\
      }\
    }' oldsrtfile.srt > newsrtfile.srt
I sorta wish I had used shell script at the beginning.