二、正则表达式
1.作用
.元字符
.范围
.重复
三、测试语句,test命令
1.用于
主要用于判断文件、字符串、数值关系。
2.语法
A.测试文件是否存在
#!/bin/bash
if test -e $1 #$1是为输入第一个参数
then
echo "yes"
else
echo "no"
fi
测试结果:
gec@ubuntu:/mnt/hgfs/share$ ./test.sh 12.c
yes
gec@ubuntu:/mnt/hgfs/share$ ./test.sh 12322.c
no
B.测试字符串是否相等
#!/bin/bash
sa="ABC"
sb="ABC"
if test $sa = $sb # if [ $sa = $sb ]
then
echo "yes"
else
echo "no"
fi
输出结果:
gec@ubuntu:/mnt/hgfs/share$ ./test.sh
yes
C.测试数值是否相等
#!/bin/bash
a=11
b=22
if test $a -eq $b # if [ $a = $b ]
then
echo "yes"
else
echo "no"
fi
输出结果:
gec@ubuntu:/mnt/hgfs/share$ ./test.sh
no
四、逻辑控制语句
1.if
if test $a -eq $b # if [ $a = $b ]
then
echo "yes"
else
echo "no"
fi
2.for
#!/bin/bash
files=`ls`
for a in $files
do
if [ -f $a ]
then
wc -l $a
fi
done
输出结果:
gec@ubuntu:/mnt/hgfs/share$ ./test.sh
0 12.c
0 1abc.c
3 1.sh
0 abc.c
0 a.c
0 a.sh
0 a.txt
3 file
6 people.txt
20 phone.txt
6 test.c
12 test.sh
12 test.sh~
34 variable.sh
3.while
#!/bin/bash
declare -i n=0
while [ $n -le 10 ]
do #{
echo "$n"
n=$n+1
done #}
输出结果:
gec@ubuntu:/mnt/hgfs/share$ ./test.sh
0
1
2
3
4
5
6
7
8
9
10
4.case
#!/bin/bash
read VAR
case $VAR in
1) echo "one"
;;
2) echo "two"
;;
'a') echo "letter is a"
;;
*) echo "unknown"
esac
输出结果:
gec@ubuntu:/mnt/hgfs/share$ ./test.sh
1
one
gec@ubuntu:/mnt/hgfs/share$ ./test.sh
3
unknown
gec@ubuntu:/mnt/hgfs/share$ ./test.sh
2
two
gec@ubuntu:/mnt/hgfs/share$ ./test.sh
a
letter is a
五、Makefile
1.定义
Makefile最重要的作用管理编译的文件。
什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要懂。这就好像现在有这么多的HTML的编辑器,但如果你想成为一个专业人士,你还是要了解HTML的标识的含义。特别在Unix下的软件编译,你就不能不自己写makefile了,会不会写makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。
系统移植
.u-boot 有很多个*.c
.kernel 有很多个*.c,当前内核2200万行代码
.rootfs 有很多个*.c
2.格式
A.例子1
x86:
gcc *.c -o main
clean:
rm main
输出结果:
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ make
gcc *.c -o main
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ make x86
gcc *.c -o main
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ make clean
rm main
当我们输入make命令的时候,默认是执行makefile第一个标号。
B.例子2
x86:
gcc *.c -o main
arm:
arm-linux-gcc *.c -o main
clean:
rm main
输出结果:
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ make
gcc *.c -o main
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ file main
main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x7bf0135efa48bf3262e838e388435d3ed99f0bc7, not stripped
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ make arm
arm-linux-gcc *.c -o main
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ file main
main: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, not stripped
C.例子3
all:
$(CC) *.c -o main
clean:
rm main
输出结果:
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ make
cc *.c -o main
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ file main
main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x7bf0135efa48bf3262e838e388435d3ed99f0bc7, not stripped
D.例子4
CC=arm-linux-gcc
all:
$(CC) *.c -o main
clean:
rm main
输出结果:
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ make
arm-linux-gcc *.c -o main
gec@ubuntu:/mnt/hgfs/share/Makefile/1$ file main
main: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, not stripped
E.例子5
CC=arm-linux-gcc
OBJS=$(patsubst %.c,%.o,$(wildcard *.c))
all:$(OBJS)
$(CC) $(OBJS) -o main
clean:
rm *.o
一般我们可以使用“$(wildcard *.c)”来获取工作目录下的所有的.c文件列表。复杂一些用法;可以使用“$(patsubst %.c,%.o,$(wildcard *.c))”,首先使用“wildcard”函数获取工作目录下的.c文件列表;之后将列表中所有文件名的后缀.c编译输出为.o。