2008年4月3日 星期四

Using script to Write Notes - (我用 Script 寫筆記)

I am a firmware engineer and I often write notes to record my trial and experience on installation of software, tools, and some other things! These notes help me a lot because I can redo any thing after a long time!!

Recently, I tried the Ubuntu Hardy with version Alpha 5 and got some problem with my amd64 machine. I have to re-install it several times! The installation of Ubuntu is quick, but it takes me a lot of time to tune it to fit the my used environment! I have to install a lot of other software and re-write the configuration files!!

If I have write down the steps by script, I can save a lot of my time after each installation!!

The second experience is: When I try the building of Glibc, I keep on write down the notes. And suddenly I think: Since it is step by step, why don't I use the script to note it instead of simply text?

For a software / firmware notes, such as software and program build, the notes style is something like:
---------------
Description
....
Commands
....
Description
...
Commnads
...
---------------
You describe something, and then show up your command or program!!


if what you note down can not be realized step by step, the information must be obsolete or outdated!!! So I have to revise my old notes frequently!! If that is true, why do I not to make my note runnable? This can save my time a lot for the second round!!!

This is not a new idea!

I am not very sure, but I did know there are some tries and maybe some useful tool available. For example, to embedded the notes in the program, such as C or others!! You write the remark by some tags or format such that you can extract your notes by utilities!!

Or the XML that can do almost anything!!

After consideration, there are some choices:
1. Search for some ready tools
2. Write my notes with the commands with some special tags so that I can use utilities to extract and run
3. Write more remark and use more "echo" in the Bash Script

For item 1, First, I have no time to learn if the grammar or tags are complicated. Second,it maybe hard to trim it for my used usage. and third and also the major reason: I don't know any ready tools and I don't search the Web. If anyone have good suggestion can tell me!!

For item 2, I think it is workable! But I think it would be more complicated to parse the runnable commands out of a plain text!! And it is not flexible or it would be more complicated if you want flexibility!!

For item 3, it is good, but it is not intuitive!! Although it is closest to our notes, but for flexibility sake, we often use the function call, indirect reference, and many flow control that make your remark trivial!!

1 Easy to note down
Not too much tags or grammar!!
2 No need more utilities or tools
As simple as good!!
3 Flexibility
Sometimes I want to just run parts of my script for some tests!! And I can turn on/off the notes when execution, or I simply want to know the usable steps to do it and don't really run

I choose the Bash Script because I often work on it and I used to it. Although I am not familiar with script programming, this is the chance that I can learn it. And it has the following benefit:

1 Its function is easy
2 Its file management is easy
3 It is easy for it to execute program
Thanks to the Unix Design, it can call almost any program to help the job

To do it, first I create two simple function:
-------------------
n() {
if [ "$NOTE" != "n" ];then
echo "$*"
fi

p() {
if [ "$VERBOSE" != ];then
echo "executing $*"
fi
if [ "$GO" = "y" ];then
eval "$*"
fi
}
------------------------------
and then you can use them in your following script!!
------------------------------

n your notes
p ls
------------------------------
So that I can write down your note and command nearly as easy as my notes in plain text!!

You can see your notes on the standard output, and you can also see the execution command and result! I use some variables to switch the notes, execution command line, and the real execution on / off

To improve the flexibility, I use the program style used by the scripts in /etc/init.d/*. Which has the following pattern:
-----------------------
#!bin/sh
..
..
..
case $1)
start)
...
stop)
...
restart)
...
*)
;;
esac

exit
--------
It uses the CASE execute different work in one script! And I can use it o divide your stream-like flow into separate segment so that I can test them separately!!

Follow this idea, I use the compilation of glibc on PC as an experiment!! If you are using the Ubuntu Hardy and have the internet conncetion, just download the two attached files my_methods and glibc_build in the same directory and run:

./glibc_build go=y

and it would download the glibc-2.17 source package and build the source automatically. Please notes: it doesn't use the dpkg-related utilities!!

you can show the basic options by

./glibc_build show=basic

you can the basic information by

./glibc_build act=preface

you can show the building steps

./glibc_build

and it would show your notes and your steps, but not run:

########### Do Preface
Platform: Ubuntu Hardy i386 20080401
The Glibc version-glibc-2.7.tar.gz
Based on the book: The Definitive Guild To GCC, 2nd, Chapter 12


########### Do Download
$----- apt-get source glibc -----'
$----- cd glibc-2.7 -----'


########### Do Unpack
Note: We use the GNU tar so that we can use the following commands, if not, please reference the book!!
The official downloaded version is glibc-2.7.tar.gz


########### Do Intaling Tools
Install the required tools
$----- aptitude install -y autoconf automake -----'
Install the gawk to prevent the erors:mawk: scripts/gen-sorted.awk: line19: regular expression make failed
No rule to make target xxx/Versions.all, needed by ..../abi-version.h
If you do not use the gawk, it would use the mawk and casuse this error
You will needs to re-run configuration if you face this error in make
$----- sudo aptitude install -y gawk -----'


########### Do Configure
Configuraing the glibc-2.7.tar.gz
The best way to build the Glibc is to create a separate directory for buiild
The glibc is default to be installed in /usr/local
The Glibc search and load search path is determined by
LD_LIBRARY environment varialbes or configuration file /etc/ld.so.conf
Set the installation directory by --prefix= option
run the configure --help to get more configuration options
Optimize for kernel --enable-kernel=, --enable-kernel=current>


Creating a directory
$----- mkdir /home/black/workarea/glibc-2.7/tt/glibc-build -----'
$----- mkdir /home/black/workarea/glibc-2.7/ttglibc-installed -----'
$----- cd /home/black/workarea/glibc-2.7/tt/glibc-build -----'
$----- /home/black/workarea/glibc-2.7/tt/glibc-2.7/configure --enable-add-ons --prefix=/home/black/workarea/glibc-2.7/ttglibc-installed -----'


########### Do Make
Compiling the glibc-2.7.tar.gz
Fix the __stack_chk_fail_local fail by adding the -fno-stack-protector
However it would overlap the -O2 options so that glibc would complain
#error: glibc cannot be maked without optimization
So I add the -O2 together and succeeded
$----- cd /home/black/workarea/glibc-2.7/tt/glibc-build -----'
$----- LANG=C LC_ALL=C make -j2 CFLAGS+='-O2 -fno-stack-protector' -----'


########### Do Check
do the check
$----- cd /home/black/workarea/glibc-2.7/tt/glibc-build -----'

You can look a piece of my code:

do_configure() {
n "$SEP Do Configure"
n Configuraing the $PKG
n The best way to build the Glibc is to create a separate directory for buiild
n The glibc is default to be installed in /usr/local
n The Glibc search and load search path is determined by
n LD_LIBRARY environment varialbes or configuration file /etc/ld.so.conf
n Set the installation directory by --prefix=\ option
n run the configure --help to get more configuration options
n Optimize for kernel --enable-kernel=\, --enable-kernel=current\>
n "\n"
n Creating a directory
BUILD_DIR=$(pwd)/glibc-build
INSTALL_DIR=$(pwd)glibc-installed
PKGDIR=$(pwd)/glibc-$VER
p mkdir $BUILD_DIR
p mkdir $INSTALL_DIR
p cd $BUILD_DIR
p $PKGDIR/configure --enable-add-ons --prefix=$INSTALL_DIR
}

do_make() {
n "$SEP Do Make"
n Compiling the $PKG
n Fix the __stack_chk_fail_local fail by adding the -fno-stack-protector
n However it would overlap the -O2 options so that glibc would complain
n "\t\t"\#error: glibc cannot be maked without optimization
n So I add the -O2 together and succeeded
.......
.......

case "$ACT" in
preface)
do_preface
;;
download)
do_download
;;
tools)
do_tools
;;
usage)
do_usage
;;
patch)
do_patch
;;
restart)
do_restart
;;
precheck)
do_precheck
;;
addon)
do_addon
;;
unpack)
do_unpack
;;
addon)
do_addon
;;
configure)
do_configure
;;
make)
do_make
;;
check)
do_check
;;
install)
do_install
;;
clean)
do_clean
;;
summary)
do_summary
;;
*)
do_preface
do_download
p cd $PKGNAME-$VER
do_unpack
do_tools
do_configure
do_make
do_check
do_summary
;;
esac
exit 0

I use the function to group each step, and write the note and script inside them. Of course I want more robust and flexibility so many variables are added and many skill used. If you just want it run in your own limited situation, you can just write it serially and you can modify it if you have free time!!

佇 Linux 來看GPX 檔案

最近定定有戶外活動。使用𤆬路機 (GPS) 來記錄行過的路線。普通我記錄路線,攏是用手機仔抑是專門个𤆬路機,罕得用電腦來看。 毋過,"仙人拍鼓有時錯,跤步踏差啥人無"。有一擺我無細膩,袂記得共一擺活動的路線收煞起來,閣直接開始記錄下一擺的活動,按呢共幾落...