Pages

Thursday, November 25, 2010

Tips tricks with the terminal in Ubuntu [Phần 1]

In ấn
Lệnh lpr là sử dụng để in tài liệu trong môi trường Linux do gói CUPS cung cấp. Đây là toàn bộ hướng dẫn. Bên dưới chỉ tóm tắt các lệnh phổ biến hay dùng.
  • Để xem danh sách các máy in hiện đang được quản lý bởi CUPS server, chúng ta: lpstat -p -d ## from cups manual
  • In một tài liệu với máy in ngầm định: lpr filename ## print file using default printer
  • lpr -P printername filename ## print file using the selected printer; see lpstat -p -d for list of printers
  • In tài liệu với số bản in (copy number): lpr -# num filename ## to print num times
  • openoffice -p filename.doc ## for doc files

Quản lý phiên làm việc
Để logout máy tính từ terminal, trong Ubuntu 10.10, bạn gõ lệnh:
gnome-session-save --logout
Để lock máy tính từ terminal, trong Ubuntu 10.10, gõ lệnh:
gnome-screensaver-command -l

Xem phiên bản (release) hệ điều hành Ubuntu
  • uname -r
    uname -a
  • cat /etc/*-release
    cat /etc/*version
  • lsb_release -a
Xem calendar
Xem link nay. Lệnh cơ bản: cal

Liệt kê các thư mục đang chia sẻ
Cách 1: Sử dụng GUI để xem, nhưng với cách này thì không hiển thị tất cả các thư mục chia sẻ (chưa giải thích được). Để mở cửa sổ này, lần lượt click vào menu System > Administration > Shared Folders. Xem hình bên dưới.

Nếu trường hợp bạn không thấy mục Shared Folders, có thể nó không được hiển thị trong menu Administration. Khi đó, bạn mở chỉnh sửa một chút là chức năng sẽ hiển thị trở lại. Các bước thực hiện: System > References > Main Menu, cửa sổ xuất hiện như hình bên dưới:

Tại cửa sổ, kéo thanh cuộn dọc (khung bên trái) để chọn Administration, đánh dấu check vào mục Shared Folders bên phải. Thế là bạn sẽ có chức năng này hiển thị trên menu Administration.
Cách 2: Dùng lệnh từ terminal
1. smbclient -L localhost
Mỗi lần chạy lệnh này là một lần cung cấp mật khẩu. Kết quả hiện thị tương tự như hình sau:

2. ls -l /var/lib/samba/usershares
Với lệnh này, tất cả các thư mục được chia sẻ thông qua dịch vụ Nautilus sẽ được hiển thị ra. Ví dụ, bạn có thư mục chia sẻ có tên là these. Thế thì, để lấy đường dẫn vật lý trên máy tính, chạy lệnh sau:

cat /var/lib/samba/usershares/billymusic | grep path

3. Tạo một script như sau:
shares=$(ls -l /var/lib/samba/usershares | awk '{print "/var/lib/samba/usershares/"$8}')
for i in $shares
do
cat $i | grep path
done

P/S: Đoạn scipt sau đây để hủy chia sẻ tất cả các thư mục:
#!/bin/sh
shares=$(ls -l /var/lib/samba/usershares | awk '{print $8}')
for i in $shares
do
net usershare delete $i
done


Mount một file iso
Tạo một thư mục để mount file iso, giả sử /mnt/disk. Thực hiện lệnh sau:
sudo mount -o loop -t iso9660 file.iso /mnt/disk


Tìm kiếm tập tinfind /usr/local/texlive -name "*.sty"

Tuesday, November 23, 2010

How to play *SWF file in Ubuntu 10.04

Gnash SWF player for Ubuntu 10.04
SWF(Shock Wave Flash) file format is widely used for displaying animation or vector graphics,with different degrees of interactivity and functions.Adobe products such as Flash,Flex Builder and Open source Ming Library is used to generate SWF files.If you are not able to play swf file on Ubuntu 10.04 then here is an simple solution of that.Gnash SWF player is recommended for playing SWF files on Ubuntu 10.04 Lucid Lynx.
Installing SWF player on Ubuntu 10.04
Install swf file player on ubuntu 10.04 LTS
1.Open Ubuntu Software Center(Applications->Ubuntu Software Center) or SPM(Synaptic Package Manager) from System->Administration->Synaptic Package Manager.
2.Search for Gnash and click on Install(in USC) OR mark for installation,then apply to install gnash(In SPM).
3.Installation,followed by downloading process will complete within few minutes…….
4.That’s all…Now Open the SWF files with GNASH SWF player to play……

(Source:  http://blog.sudobits.com/2010/06/05/how-to-play-swf-file-in-ubuntu-10-04/)

Một tham khảo khác: http://linuxtree.blogspot.com/2010/02/how-to-play-swf-filemacromedia-flash-on.html

Graph Clustering

This is a mathematical study of the methods to recognizing and grouping elements in a set of entities.

Tuesday, November 9, 2010

Biên dịch chương trình C/C++ tự động bằng việc tạo makefile

Đây là một bài viết khá hoàn chỉnh và lý thú. Qua bài này và một số tham khảo khác trên mạng, tạm copy cách làm như sau:

Preface

Compiling a program made of one source file is easy. Compiling one made of few sources is slightly annoying, but may be automated via a simple shell script. Anything larger than that would start to get on your nerves. This is where makefiles are helpful.
A makefile is a collection of instructions that should be used to compile your program. Once you modify some source files, and type the command "make" (or "gmake" if using GNU's make), your program will be recompiled using as few compilation commands as possible. Only the files you modified and those dependent upon them will be recompiled. Of-course, this is not done via usage of magic. You need to supply the rules for compiling various files and file types, and the list of dependencies between files (if file "A" was changed, then files "B", "C" and "D" also need to be re-compiled), but that only has to be done once.

The Structure Of A Makefile

A typical makefile contains lines of the following types:
  • Variable Definitions - these lines define values for variables. For example:
    
         CFLAGS = -g -Wall
         SRCS = main.c file1.c file2.c
         CC = gcc
         

  • Dependency Rules - these lines define under what conditions a given file (or a type of file) needs to be re-compiled, and how to compile it. For example:
    
         main.o: main.c
                 gcc -g -Wall -c main.c
         

    This rule means that "main.o" has to be recompiled whenever "main.c" is modified. The rule continues to tell us that in order to compile "main.o", the command "gcc -g -Wall -c main.c" needs to be executed.
    Note that each line in the commands list must begin with a TAB character. "make" is quite picky about the makefile's syntax.

  • Comments - Any line beginning with a "#" sign, or any line that contains only white-space.

Order Of Compilation

When a makefile is executed, we tell the make command to compile a specific target. A target is just some name that appears at the beginning of a rule. It can be a name of a file to create, or just a name that is used as a starting point (such a target is also called a "phony" target).
When make Is invoked, it first evaluates all variable assignments, from top to bottom, and when it encounters a rule "A" whose target matches the given target (or the first rule, if no target was supplied), it tries to evaluate this rule. First, it tries to recursively handle the dependencies that appear in the rule. If a given dependency has no matching rule, but there is a file in the disk with this name, the dependency is assumed to be up-to-date. After all the dependencies were checked, and any of them required handling, or refers to a file newer than the target, the command list for rule "A" is executed, one command at a time.
This might appear a little complex, but the example in the next section will make things clear.

Starting Small - A Single-Source Makefile Example

Lets first see a simple example of a makefile that is used to compile a program made of a single source file:


# top-level rule to create the program.
all: main

# compiling the source file.
main.o: main.c
        gcc -g -Wall -c main.c

# linking the program.
main: main.o
        gcc -g main.o -o main

# cleaning everything that can be automatically recreated with "make".
clean:
        /bin/rm -f main main.o
Few notes about this makefile:
  1. Not all rules have to be used in every invocation of make. The "clean" rule, for example, is not normally used when building the program, but it may be used to remove the object files created, to save disk space.
  2. A rule doesn't need to have any dependencies. This means if we tell make to handle its target, it will always execute its commands list, as in the "clean" rule above.
  3. A rule doesn't need to have any commands. For example, the "all" rule is just used to invoke other rules, but does not need any commands of its own. It is just convenient to make sure that if someone runs make without a target name, this rule will get executed, due to being the first rule encountered.
  4. We used the full path to the "rm" command, instead of just typing "rm", because many users have this command aliased to something else (for example, "rm" aliased to "rm -i"). By using a full path, we avoid any aliases.

Getting Bigger - A Multi-Source Makefile Example

In anything but the simplest programs, we usually have more than one source file. This is where using a makefile starts to pay off. Making a change to one file requires re-compilation of only the modified file, and then re-linking the program. Here is an example of such a makefile:


# top-level rule to compile the whole program.
all: prog

# program is made of several source files.
prog: main.o file1.o file2.o
        gcc main.o file1.o file2.o -o prog

# rule for file "main.o".
main.o: main.c file1.h file2.h
        gcc -g -Wall -c main.c

# rule for file "file1.o".
file1.o: file1.c file1.h
        gcc -g -Wall -c file1.c

# rule for file "file2.o".
file2.o: file2.c file2.h
        gcc -g -Wall -c file2.c

# rule for cleaning files generated during compilations.
clean:
        /bin/rm -f prog main.o file1.o file2.o
Few notes:
  • There is one rule here for each source file. This causes some redundancy, but later on we'll see how to get rid of it.
  • We add dependency on included files (file1.h, file2.h) where applicable. If one of these interface-definition files changes, the files that include it might need a re-compilation too. This is not always true, but it is better to make a redundant compilation, than to have object files that are not synchronized with the source code.

Using Compiler And Linker Flags

As one could see, there are many repetitive patterns in the rules for our makefile. For example, what if we wanted to change the flags for compilation, to use optimization (-O), instead of add debug info (-g)? we would need to go and change this flag for each rule. This might not look like much work with 3 source files, but it will be tedious when we have few tens of files, possibly spread over few directories.
The solution to this problem is using variables to store various flags, and even command names. This is especially useful when trying to compile the same source code with different compilers, or on different platforms, where even a basic command such as "rm" might reside in a different directory on each platform.
Lets see the same makefile, but this time with the introduction of variables:


# use "gcc" to compile source files.
CC = gcc
# the linker is also "gcc". It might be something else with other compilers.
LD = gcc
# Compiler flags go here.
CFLAGS = -g -Wall
# Linker flags go here. Currently there aren't any, but if we'll switch to
# code optimization, we might add "-s" here to strip debug info and symbols.
LDFLAGS =
# use this command to erase files.
RM = /bin/rm -f
# list of generated object files.
OBJS = main.o file1.o file2.o
# program executable file name.
PROG = prog

# top-level rule, to compile everything.
all: $(PROG)

# rule to link the program
$(PROG): $(OBJS)
        $(LD) $(LDFLAGS) $(OBJS) -o $(PROG)

# rule for file "main.o".
main.o: main.c file1.h file2.h
        $(CC) $(CFLAGS) -c main.c

# rule for file "file1.o".
file1.o: file1.c file1.h
        $(CC) $(CFLAGS) -c file1.c

# rule for file "file2.o".
file2.o: file2.c file2.h
        $(CC) $(CFLAGS) -c file2.c

# rule for cleaning re-compilable files.
clean:
        $(RM) $(PROG) $(OBJS)
Few notes:
  • We define many variables in this makefile. This will make it very easy to modify compile flags, compiler used, etc. It is good practice to define even things that might seem like they'll never change. In time - they will.
  • We still have a problem with the fact that we define one rule for each source file. If we'll want to change this rule's format, it will be rather tedious. The next section will show us how to avoid this problem.

A Rule For Everyone - Using "File Type" Rules

So, the next phase would be to eliminate the redundant rules, and try to use one rule for all source files. After all, they are all compiled in the same way. Here is a modified makefile:


# we'll skip all the variable definitions, just take them from the previous
# makefile
.
.
# linking rule remains the same as before.
$(PROG): $(OBJS)
        $(LD) $(LDFLAGS) $(OBJS) -o $(PROG)

# now comes a meta-rule for compiling any "C" source file.
%.o: %.c
        $(CC) $(CFLAGS) -c $<
We should explain two things about our meta-rule:
  1. The "%" character is a wildcard, that matches any part of a file's name. If we mention "%" several times in a rule, they all must match the same value, for a given rule invocation. Thus, our rule here means "A file with a '.o' suffix is dependent on a file with the same name, but a '.c' suffix".
  2. The "$<" string refers to the dependency list that was matched by the rule (in our case - the full name of the source file). There are other similar strings, such as "$@" which refers to the full target name, or "$*", that refers the part that was matched by the "%" character.

Automatic Creation Of Dependencies

One problem with the usage of implicit rules, is that we lost the full list of dependencies, that are unique to each file. This can be overcome by using extra rules for each file, that only contain dependencies, but no commands. This can be added manually, or be automated in one of various ways. Here is one example, using the "makedepend" Unix program.


# define the list of source files.
SRCS = main.c file1.c file2.c
.
.
# most of the makefile remains as it was before.
# at the bottom, we add these lines:

# rule for building dependency lists, and writing them to a file
# named ".depend".
depend:
        $(RM) .depend
        makedepend -f- -- $(CFLAGS) -- $(SRCS) > .depend

# now add a line to include the dependency list.
include .depend
Now, if we run "make depend", the "makedepend" program will scan the given source files, create a dependency list for each of them, and write appropriate rules to the file ".depend". Since this file is then included by the makefile, when we'll compile the program itself, these dependencies will be checked during program compilation.
There are many other ways to generate dependency lists. It is advised that programmers interested in this issue read about the compiler's "-M" flag, and read the manual page of "makedepend" carefully. Also note that gnu make's info pages suggest a different way of making dependency lists created automatically when compiling a program. The advantage is that the dependency list never gets out of date. The disadvantage is that many times it is being run for no reason, thus slowing down the compilation phase. Only experimenting will show you exactly when to use each approach.

Notes: Sorry the author of the article due to copyright.

Thursday, November 4, 2010

ASP.NET: Drop Down List Control

Thường thường chúng ta muốn điền dữ liệu cho các hộp danh sách (drop down list) khi form vừa nạp (môi trường web và desktop). Rõ ràng chúng ta chỉ muốn nạp (fill in) một lần duy nhất khi form vừa khởi động, và không cần fill nữa trong trường hợp form refresh. Đo đó, để tránh tình trạng cứ nạp nhiều lần mỗi khi form hởi động, chúng ta đặt đoạn mã nạp dữ liệu cho ô điều khiển Drop Down List trong điều kiện trang chưa PostBack. Cách làm như sau:
if (!Page.IsPostBack) {
....; // Đoạn mã lệnh fill in cho drop down list
}
Tương tự như vậy cho các ô điều khiển khác như ListBox,