2007年7月7日 星期六
2007年7月4日 星期三
makefile example
ROOTDIR = ..
include $(ROOTDIR)/config/linux.mak
MODULE = XMLTool
MODULE_LIB = xmltool
ifdef BUILD_SO
ifeq ($(BUILD_SO), true)
LIBNAME = lib$(MODULE_LIB).so
CFLAGS += -fPIC
LFLAGS = -shared -Wl,-soname,$(LIBNAME)
else
LIBNAME = lib$(MODULE_LIB).a
endif
endif
DEPENDENT_MODULES = $(ROOTDIR)/Locker
CFLAGS += -I$(ROOTDIR)/$(MODULE)/include
CFLAGS += `xml2-config --cflags`
CFLAGS += -I$(ROOTDIR)/Locker/include
LDFLAGS += -L$(ROOTDIR)/$(MODULE)/lib -L$(ROOTDIR)/$(MODULE)/release/
LDFLAGS += -L$(DEPENDENT_MODULES)/release/
LDFLAGS += -lstdc++ -lpthread -luuid -llocker
LDFLAGS += `xml2-config --libs`
OBJS += XML.o
MAIN_OBJ =
EXE =
OBJS_SRC = $(addprefix src/,$(OBJS))
MAIN_OBJ_SRC = $(addprefix src/,$(MAIN_OBJ))
LIBNAME_RELEASE = $(addprefix release/,$(LIBNAME))
EXE_RELEASE = $(addprefix release/,$(EXE))
TEST_OBJ = test.o
TEST_EXE = test/test
TEST_OBJ_TST = $(addprefix test/,$(TEST_OBJ))
all: dependent_module $(LIBNAME_RELEASE) $(EXE_RELEASE) $(TEST_EXE)
@echo "Please export LD_LIBRARY_PATH..."
@echo "e.g. export LD_LIBRARY_PATH=`pwd`/release"
dependent_module:
@for modules in $(DEPENDENT_MODULES); do \
make -C $$modules; \
done;
$(LIBNAME_RELEASE): $(OBJS_SRC)
@if [ "$$BUILD_SO" == "true" ]; then \
$(LINK) $(LFLAGS) -o $(LIBNAME_RELEASE) $^ ;\
else \
$(LD) -r -o $(LIBNAME_RELEASE) $^ ;\
fi
$(EXE_RELEASE): $(LIBNAME_RELEASE) $(MAIN_OBJ_SRC)
$(C++) -o $@ $(MAIN_OBJ_SRC) $(LDFLAGS) -l$(MODULE_LIB)
$(TEST_EXE): $(LIBNAME_RELEASE) $(TEST_OBJ_TST)
$(CC) -o $@ $(TEST_OBJ_TST) $(LDFLAGS) -l$(MODULE_LIB)
.cpp.o:
$(C++) -c $< -o $(patsubst %.cpp,%.o,$<) $(CFLAGS)
.c.o:
$(CC) -c $< -o $(patsubst %.cpp,%.o,$<) $(CFLAGS)
clean:
$(RM) $(LIBNAME_RELEASE) $(EXE_RELEASE) $(OBJS_SRC) $(MAIN_OBJ_SRC)
$(RM) $(TEST_OBJ_TST) $(TEST_EXE) test/output*
makefile, go to each modules
all:
@for modules in $(MODULES); do \
make -C $$modules; \
cp -rf ./$$modules/release/* ./release; \
done;
clean:
@for modules in $(MODULES); do \
make clean -C $$modules; \
done;
set environment variable to the command
[will@localhost config]$ cat sendmail.sh
#!/bin/sh
LIST="a b c d e"
for list in $LIST; do\
cat /home/will/log.txt | LD_LIBRARY_PATH=/usr/local/lib /home/will/smtpsend $list > /dev/null 2>&1; \
done;
my indent coding style
# Will.20070509: linux coding style
dos2unix $*
indent -nbad -bap -nbc -bbo -br -bli2 -bls -ncdb -nce -cp1 -cs -di2 -ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -psl -nsc -nsob -l100 $*
[will@localhost config]$
use shared library during runtime
[will@localhost DynamicLibrary]$ cat dl.test.c
#include <dlfcn.h>
#include <stdio.h>
main ()
{
void *libc;
void (*printf_call) ();
if (libc = dlopen ("/lib/libc.so.6", RTLD_LAZY))
{
printf_call = dlsym (libc, "printf");
(*printf_call) ("hello, world\n");
}
dlclose (libc);
}
[will@localhost DynamicLibrary]$
Make Dynamic Shared Library
gcc -shared -Wl,-soname,libxxx.so -o libxxx.so *.o
shell script : while loop
[will@localhost ShellScript]$ cat while.sh
#!/bin/sh
s=0
i=0
while [ "$i" != "10" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "The result of '1+2+3+...+10' is ==> $s"
[will@localhost ShellScript]$
shell script : test file type
[will@localhost ShellScript]$ cat testfile.sh
#!/bin/sh
read -p "Input a filename : " filename
test -z $filename && echo "You MUST input a filename." && exit 0
test ! -e $filename && echo "The filename $filename DO NOT exist" && exit 0
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="r"
test -w $filename && perm=$perm"w"
test -x $filename && perm=$perm"x"
echo "The filename: $filename is a $filetype"
echo "And the permission are : $perm"
[will@localhost ShellScript]$
shell script : awk and sort
[will@localhost ShellScript]$ cat sort.sh
#!/bin/sh
ls -l *.sh| awk '{print $8" "$9}'|sort
[will@localhost ShellScript]$
shell script : read line
[will@localhost ShellScript]$ cat read.sh
#!/bin/sh
read -p "Please input your first name: " firstname
read -p "Please input your last name: " lastname
echo -e "Your full name is : "$firstname" "$lastname"\r\n"
[will@localhost ShellScript]$
shell script : execute with parameter
[will@localhost ShellScript]$ cat parameter.sh
#!/bin/sh
echo "The script name is ==> $0"
[ -n "$1" ] && echo "The 1st paramter is ==> $1" || exit 0
[ -n "$2" ] && echo "The 2nd paramter is ==> $2" || exit 0
[ -n "$3" ] && echo "The 3th paramter is ==> $3" || exit 0
echo "integer compare..."
[ "$1" -gt "$2" ] && echo $1" > "$2
[ "$1" -lt "$2" ] && echo $1" < "$2
[ "$1" -eq "$2" ] && echo $1" = "$2
echo "string compare..."
[ "$1" = "$2" ] && echo $1" = "$2
[ "$1" != "$2" ] && echo $1" != "$2
[will@localhost ShellScript]$
shell script : do math
[will@localhost ShellScript]$ cat number.sh
#!/bin/sh
a=3
b=5
ab=$a*$b
echo $ab
ab=$(($a*$b))
echo $ab
[will@localhost ShellScript]$
shell script : function
[will@localhost ShellScript]$ cat function.sh
#!/bin/shfunction printit(){
echo "Your choice is $1"
}case $1 in
"one")
printit 1
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage {one|two|three}"
;;
esac
[will@localhost ShellScript]$
shell script : string for loop
[will@localhost ShellScript]$ cat for.string.sh
#!/bin/sh
for animal in elephant dinosaur kangaroo
do
echo "There are ""$animal"" in the zoo."
done
[will@localhost ShellScript]$
shell script : for loop
[will@localhost ShellScript]$ cat for.sh
#!/bin/sh
s=0
for (( i=1; i<=10; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+10' is ==> $s"
[will@localhost ShellScript]$
shell script : variable save command result
[will@localhost ShellScript]$ cat date.sh
#!/bin/bash
ddd=`/bin/date`
echo -e "It is \""$ddd"\" now."
[will@localhost ShellScript]$
shell script : condition without if
[will@localhost ShellScript]$ cat cond.sh
#!/bin/sh
read -p "Please input (Y/N): " yn
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "Oh, Yes!" && exit 0
[ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, No!" && exit 0
echo "I don't know what your choice is." && exit 0
[will@localhost ShellScript]$
shell script : if condition
[will@localhost ShellScript]$ cat cond.if.sh
#!/bin/sh
read -p "Please input (Y/N): " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "Oh, Yes!"
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh, No!"
else
echo "I don't know what your choice is."
fi
shell script case
[will@localhost ShellScript]$ cat case.sh
#!/bin/sh
case $1 in
"hello")
echo "Hello, how are you ?"
;;
"")
echo "You MUST input parameters, ex> $0 someword"
;;
*)
echo "Usage $0 {hello}"
;;
esac
[will@localhost ShellScript]$
select stdin example
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int
main(void) {
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don¡¦t rely on the value of tv now! */
if (retval == -1)
perror("select()");
else if (retval)
printf("Data is available now.\n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds.\n");
return 0;
}