if [ condition ]
then
command
...
else
command
...
fi
使用 test command
if test condition
then
command
...
else
command
...
fi
if 比較運算子
字串比較運算子
[ … ]
test command
用法(回傳 TRUE)
if [ string1 = string2 ]
if test string1 = string2
string1與 string2相同
if [ string1 != string2 ]
if test string1 != string2
string1與 string2不相同
if [ string ]
if test string
string 不為空值
if [ -n string ]
if test -n string
string 不為空值
if [ -z string ]
if test -z string
string 為空值
整數比較運算子
[ … ]
test command
用法(回傳 True)
if [ int1 -eq int2 ]
if test int1 -eq int2
int1等於 int2
if [ int1 -ne int2 ]
if test int1 -ne int2
int1不等於 int2
if [ int1 -gt int2 ]
if test int1 -gt int2
int1大於 int2
if [ int1 -ge int2 ]
if test int1 -ge int2
int1大於等於 int2
if [ int1 -lt int2 ]
if test int1 -lt int2
int1小於 int2
if [ int1 -le int2 ]
if test int1 -le int2
int1小於等於 int2
檔案運算子
[ … ]
test command
用法(回傳 True)
if [ -d file ]
if test -d file
file是個目錄
if [ -e file ]
if test -e file
file存在
if [ -f file ]
if test -f file
file是個檔案
if [ -r file ]
if test -r file
file可被讀取
if [ -s file ]
if test -s file
file內容非空
if [ -w file ]
if test -w file
file可被寫入
if [ -x file ]
if test -x file
file可被執行
if [ -L file ]
if test -x file
file是個符號連結
if [ -b file ]
if test -b file
file is a block special file
if [ -c file ]
if test -c file
file is a character special file
if [ -g file ]
if test -g file
file’s set group ID flag is set
if [ -h file ]
if test -h file
file is a symbolic link. This operator is retained for compatibility with previous versions of this program. Do not rely on its existence; use -L instead.
if [ -k file ]
if test -k file
file’s sticky bit is set
if [ -p file ]
if test -p file
file is a named pipe (FIFO)
if [ -u file ]
if test -u file
file’s set user ID flag is set
if [ -O file ]
if test -O file
file’s owner matches the effective user id of this process
if [ -G file ]
if test -G file
file’s group matches the effective group id of this process
intshift_and(int a) { int b = a >> 12; return b & 0xfff; }
編譯此程式,並在過程中使用 GDB 來偵錯。
$ gdb --args ./llvm-build/bin/clang -O2 -c -o shift_and.o shift_and.c
GNU gdb (GDB) Fedora 7.12.1-48.fc25
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./llvm-build/bin/clang...done.
(gdb)
(gdb) b AArch64ISelDAGToDAG.cpp:2707
Breakpoint 1 at 0x1940ecd: file /home/users/jim/llvm/llvm-6.0.0.src/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp, line 2707.
(gdb) run
Starting program: /home/users/jim/llvm/llvm-build/bin/clang -O2 -c -o shift_and.o shift_and.c
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.24-10.fc25.x86_64
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Detaching after vfork from child process 16302.
[Inferior 1 (process 16298) exited normally]
Missing separate debuginfos, use: dnf debuginfo-install libgcc-6.4.1-1.fc25.x86_64 libstdc++-6.4.1-1.fc25.x86_64 ncurses-libs-6.0-6.20160709.fc25.x86_64 zlib-1.2.8-10.fc24.x86_64
解決方法即為在執行程式之前,先設定好 set follow-fork-mode child,可看到順利地停在 AArch64ISelDAGToDAG.cpp:2707了。
$ gdb --args ./llvm-build/bin/clang -O2 -c -o shift_and.o shift_and.c
GNU gdb (GDB) Fedora 7.12.1-48.fc25
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./llvm-build/bin/clang...done.
(gdb) b AArch64ISelDAGToDAG.cpp:2707
Breakpoint 1 at 0x1940ecd: file /home/users/jim/llvm/llvm-6.0.0.src/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp, line 2707.
(gdb) set follow-fork-mode child
(gdb) run
Starting program: /home/users/jim/llvm/llvm-build/bin/clang -O2 -c -o shift_and.o shift_and.c
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.24-10.fc25.x86_64
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New process 16732]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
process 16732 is executing new program: /home/users/jim/llvm/llvm-build/bin/clang-6.0
Missing separate debuginfos, use: dnf debuginfo-install libgcc-6.4.1-1.fc25.x86_64 libstdc++-6.4.1-1.fc25.x86_64 ncurses-libs-6.0-6.20160709.fc25.x86_64 zlib-1.2.8-10.fc24.x86_64
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[Switching to Thread 0x7ffff7fc5600 (LWP 16732)]
Thread 2.1 "clang-6.0" hit Breakpoint 1, (anonymous namespace)::AArch64DAGToDAGISel::Select (this=0x9fe0190, Node=0xa0923a0)
at /home/users/jim/llvm/llvm-6.0.0.src/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:2707
2707 if (tryBitfieldExtractOp(Node))
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.24-10.fc25.x86_64 libgcc-6.4.1-1.fc25.x86_64 libstdc++-6.4.1-1.fc25.x86_64 ncurses-libs-6.0-6.20160709.fc25.x86_64 zlib-1.2.8-10.fc24.x86_64
(gdb)