Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

Tuesday, July 27, 2010

a script to replace extention of files

Suppose the file extension is "foo".


#!/bin/bash

for f in `find . -name "*.foo" -type f`
do
newname=`echo "$f" | sed "s/\.foo$//"`
mv $f $newname
echo "mv $f to $newname"
done

exit 0

Wednesday, May 20, 2009

Calculate cpu usage in MCycles/sec

Here is the script to calculate cpu usage in MCycles per second.
The cpu speed is 222MHz as an example, processing data rate is 44100, each block has 8192 samples, and the total calls to foo is 532.

#!/bin/bash

rate=44100
blksize=8192
cpu=222
call=532

# the following val is got from gprof as percentage cpu time, 30.11%.
pcpu_foo=30.11

duration=`echo "scale=4; $blksize * $call / $rate" | bc`
echo "duration: " $duration

secs_foo=`echo "scale=4; $duration * $pcpu_foo /100" | bc`
echo "secs_foo " $secs_foo

blksecs_foo=`echo "scale=4; $secs_foo/$call"|bc`
echo "blocksecs_foo" $blksecs_foo

cpu_foo=`echo "scale=4; $cpu * $rate / $blksize * $blksecs_foo"|bc`
echo "cpu_foo " $cpu_foo " MCycle/sec"

Monday, June 30, 2008

use shell variables in sed


input=abc.wav
i=0
while test $i != 10
do
echo "run case $i
tag=`expr output-foo-$i`
output=`echo $input | sed -e "s/^\(.*\)\.wav$/\1-$tag\.wav"`
./foo.bin -v -e -E $i -o $output $input
done


Here instead of

sed -e 's/^\(.*\)\.wav$/\1-$tag\.wav'


Use


sed -e "s/^\(.*\)\.wav$/\1-$tag\.wav"

Monday, June 16, 2008

setup dhcp, nfs on FC5

setup dhcp server
I need to setup dhcp server under my router.
Here is the configuration file (/etc/dhcpd.conf)


ddns-updates on;
subnet 192.168.2.0 netmask 255.255.255.0 {
option routers 192.168.2.1;
option subnet-mask 255.255.255.0;
option domain-name "local.net";
option domain-name-servers ns.local.net;
host trgt {
hardware ethernet 08:00:28:01:15:F7;
fixed-address 192.168.2.110;
option root-path "/opt/eldk/ppc_8xx";
option host-name "trgt";
next-server 192.168.2.151;
filename "tftpboot/pImage";
}
}
ddns-update-style ad-hoc;

Here "trgt" is a linux device which has fixed ip address, and need boot image through NFS from linux host with ip "192.168.2.151".

Start dhcp-server with: `/etc/rc.d/init.d/dhcpd start`

Setup nfs

Edit /etc/exports

/tftp 192.168.2.110(rw,no_root_squash,sync)


Edit /etc/hosts.allow

portmap: 10.10.10.20
lockd: 10.10.10.20
mountd: 10.10.10.20
rquotad: 10.10.10.20
statd: 10.10.10.20


And edit /etc/hosts.deny

portmap: ALL
lockd: ALL
mountd: ALL
statd: ALL



run: `exportfs -ra`




scripts on start nfs

/etc/init.d/portmap start
/etc/init.d/nfslock start
/etc/init.d/nfs start
/etc/init.d/netnfs start


Check nfs status with: `/sbin/service nfs status`

rpc.mountd (pid 4181) is running...
nfsd (pid 4183 4182 4181 4180) is running
rpc.rquotead (pid 4170) is running


You may need to "/sbin/service iptables stop" sometimes if you can the ip but can not mount while getting "no route and host" message.

Thursday, May 22, 2008

gnu make on win32 shell

1. The version of gnu make that works on win32 is 3.80, while both 3.78 and 3.81 would not work.

2. win32 shell script. The following is a sample which uses cvs to check out foo source code


@echo off

set PRJ_DIR=%PRJ_ROOT%\foo-%PRJ_TSTAMP%
set PRJ_NAME=foo
if "%1"=="-D" goto check_out_tstamp;

echo "check out HEAD of %PRJ_NAME%"
cvs co -d %PRJ_DIR% -P %PRJ_NAME%

goto end;

: check_out_tstamp
echo "check out with time stamp"
cvs co -D %PRJ_TIME% "-d" %PRJ_DIR% -P %PRJ_NAME%

:end

Monday, April 21, 2008

sed programming - commands combination

Sed is a programming language, to me it basically contains two things, one is regexp, the other is the control commands such as "g G H h" which manipulate the hold buffer and pattern buffer. Sed only contains these two buffers, so it is simple. No more.

Although Sed itself is simple, programming with Sed can be complicated. This is like playing chess, to know the rule of each play does not mean you know how to play. You need to know at least some combinations.

Combination of "G h d". Here is an example "1!G;h;$!d" which REVERSE a list of items, typically 3 states control, at start it is "h d", then the second state which can be looped, which is "G h d", and the third state is "G h". Explanation is quite not hard to understand, the hold buffer was used to save the result, so for the first item, it hold it and delete current pattern, for the second, add "\n' on current buffer and append current buffer with the content in hold buffer, save current buffer to hold buffer, so the second item will be ahead of the first item in the hold buffer. it do the same for third, fourth, etc. until the last item, is just "G h", with out "d" means it did not delete current buffer, so at last the current buffer contains the reverse list of items. (My opinion, the last 'h' is not necessary since hold buffer is only for assistant use)

"x;x" combination. A second example:

/^<\example>/ { h; }
x
/^< example>/ { x; p; d;}
x
/^< example>/ {h;}

The five line script find the content of a XML "example" tag. It need a little time to figure out how it works, this script is not using b and n command, remember it and use it in some cases. A altered way may be easy to understand, here the hold buffer contains the content of the example tag.

/^< example>/ {
h
: example-loop
n
/^<\/example>/! {
H
b example-loop
}
x
p
d
}


Here is some of my thought,
1. "n" is alway used "p", thus with "sed -n". And if you use "p", then "p" is always connect with with "d", you don't do "b end" since "d" itself restart a new cycle, -- read a next line to current buffer.
2. using branch is easy to understand the flow, but in some case, the "x;x" command combination can also be considered.

Other thoughts:
3. eol : Use "sed -e 's/\r//' your_file" to convert line ending "dos to unix".

Wednesday, January 9, 2008

version controls with cvs

0. I have a simple script to set my working CVSROOT. So when I work on it. I just run the script.

. cvs.sh

Here is cvs.sh script.(which is just 2 lines)

#! /bin/sh
export CVSROOT=":pserver:user:192.168.10.7:2401/home/repository"


1. Create a branch with tag.

cvs co -P -d foo FOO
cvs tag -b z_markc_unknown

To create a branch, you first need to check out the source code. here FOO is the module name and foo is the checkout directory.

2. Work on a branch.

cvs co -P -d foo -r z_markc_unknown FOO


3. Merge from a branch

cd foo
cvs update -j z_markc_1

It will merge the branch to where you currently work on (can be main trunk or a branch)

4. Commit your changes

cvs update
cvs diff Makefile
cvs commit Makefile

When you merge the code from a branch, it does not commit yet, so you need to commit it.

5. To delete a branch tag. (THIS SHOULD BE VERY CAREFUL!)

cd foo
cvs tag -dB z_markc_1

Monday, December 3, 2007

Turing's Very First Example (with sed)

I have fun with sed and Turing Machine. It is pleasure to digg into sed, to find out the powerful features by sed. To me, sed is not just a tool for string find and replacement, but also Turing-Complete, that means sed is programable, the best way to prove it is to construct a Turing Machine. What I choose here is Turing's very first example by Alan Turing, so I can learn Turing Theory.

The following is my coding with seds, I implement the 4 state (b c e f) table, and use "_" to identify the head position, "$" to identify the end of tap cells and "*" for blank symbols on the tap.

# Turing's very first example

# check input
/^_\?[\*]*$/! b error

# start from first
s/^\*/_/
s/\([_\*]\)$/\1\$/

# b blank P0,R c
: b
h
s/^/b /
p
g

/_/ {
# write 0
s/_/0/

# move right
s/0\*/0_/

/0\$$/ b H

b c
}
b error

: c
h
s/^/c /
p
g

/_/ {
# print blank
s/_/ /
# right
s/ \*/ _/

/ \$$/ b H
b e
}
b error

: e
h
s/^/e /
p
g

/_/ {
# print one
s/_/1/
# turn right
s/1\*/1_/

/1\$$/ b H
b f
}
b error

: f

h
s/^/f /
p
g

/_/ {
# print blank
s/_/ /
# right

s/ \*/ _/

/ \$$/ b H
b b
}
b error



: error
s/^/error with turing machine/

q 1

: H
s/^/= /
s/\$$//
q 0


And the output:

# echo "********" | sed -f Turing_first.sed
b _*******$
c 0_******$
e 0 _*****$
f 0 1_****$
b 0 1 _***$
c 0 1 0_**$
e 0 1 0 _*$
f 0 1 0 1_$
= 0 1 0 1

Thursday, November 29, 2007

Seven steps to write a functioning Makefile

In my opinion, writing a Makefile is not simple, it sometimes can be more complicated if you want to your code compiled with different compilers, different running environments or targets, and different builds to support diffent features. A Makefile can accept shell or sed scripts, it makes the building process more advance. Today, I just want to cover the basic part of Makefile, or the parts I am using for a Makefile.

First define directory variables, you specifies this to tell `make' where is your project?

PROJECT_ROOT_DIR = /devel/src/foo


Second, specify the common compiler settings, most of my time is build for embedded target, so here is the code:

export ARCH :=arm
export CROSS_COMPILE := arm-linux-

#
# Include make variables (CC, etc...)
#
ASM := $(CROSS_COMPILE)as
LD := $(CROSS_COMPILE)ld
CC := $(CROSS_COMPILE)gcc
CXX := $(CROSS_COMPILE)g++
AR := $(CROSS_COMPILE)ar


3rd, specifies the source code compileing rules, how you compile c++ or c code. The rules will make the later life easier


%.o : %.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o : %.c
$(CC) $(CFLAGS) -c -o $@ $<


Fourth, is more specific compiling setting, you define CFLAGS, CXXFLAGS, or LDFLAGS, so you specify what header directory to include, you specify what library to link, you also specify "-g" for debug, and how you want the compiler post warning message etc.

CFLAGS += -I$(PROJECT_ROOT_DIR)/include
CFLAGS += -I$(INCLS) -D_FOO_=1 -g


Fiveth, now you can provide the basic feature you want in the Makefile. Here is the sample:

compile: libfoo foo_bin

You want the user use "make compile" for build foo.

Sixth, specifiy all source code you build in seperate directories.

SRCS_FOO_DIR = $(PROJECT_ROOT_DIR)/src
SRCS_FOO = $(SRCS_FOO_DIR)/util_arm.s \
$(SRCS_FOO_DIR)/foo_info.c \
$(SRCS_FOO_DIR)/foo_main.c $(FOO_SRC_DIR)/foo_help.c
OBJS_FOO = $(patsubst %.s,%.o, $(patsubst %.c,%.o, $(SRCS_FOO)))
$(OBJS_FOO) : INCLS=-I$(SRCS_FOO_DIR)

SRCS_GST_DIR = $(PROJECT_ROOT_DIR)/gst
SRCS_GST = $(SRCS_GST_DIR)/gst.c
OBJS_GST = $(SRCS_GST:.c=.o)
$(OBJS_GST) : INCLS=-I$(SRCS_GST_DIR)


Seventh, now weave them together.


libfoo : $(OBJS_FOO) $(OBJS_GST)
$(CC) $(CFLAGS) -shared -o $@.so $^


For now, the Makefile is done, to add "clean" feature, is quite simple based the content from here.

Tuesday, November 13, 2007

script - create package

Today I write a shell script that is used to create project package, so I can backup easily. There are two features I want the script supports, one is have a date stamp automatically generated, second in the source code directory I want to filter only c source code, for instance just have c and h file.

Here is the script:

export PROJECT_NAME=foo
export PROJECT_PATH=/devel/project
export PROJECT_SRC_PATH=$PROJECT_PATH/src
export DATESTAMP=`date +%d%b%Y`

cd $PROJECT_SRC_PATH
tar czf $PROJECT_NAME-$DATESTAMP.tar.gz ./Makefile.arm-linux ./plugin/*.[ch] ./application/*.c

echo "DONE!"