shell编程2(makefile)


二、正则表达式

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。






七、静态模式 静态模式可以更加容易地定义多目标的规则,可以让我们的规则变得更加的有弹性和灵活。我们还是先来看一下语法: ;: ;: ; ; ... targets定义了一系列的目标文件,可以有通配符。是目标的一个集合。 target-parrtern是指明了targets的模式,也就是的目标集模式。 prereq-parrterns是目标的依赖模式,它对target-parrtern形成的模式再进行一次依赖目标的定义。 这样描述这三个东西,可能还是没有说清楚,还是举个例子来说明一下吧。如果我们的;定义成“%.o”,意 思是我们的;集合中都是以“.o”结尾的,而如果我们的;定义成“%. c”,意思是对;所形成的目标集进行二次定义,其计算方法是,取;模式中的“%”(也就是去掉了[.o]这个结尾),并为其加上[.c]这个结尾,形成的新集合。 所以,我们的“目标模式”或是“依赖模式”中都应该有“%”这个字符,如果你的文件名中有“%”那么你可以使用反斜杠“\”进行转义,来标明真实的“%”字符。 看一个例子: objects = foo.o bar.o all: $(objects) $(objects): %.o: %.c $(CC) -c $(CFLAGS) $< -o $@ 上面的例子中,指明了我们的目标从$object中获取,“%.o”表明要所有以“.o”结尾的目标,也就是“foo.o bar.o”,也就是变量$object集合的模式,而依赖模式“%.c”则取模式“%.o”的“%”,也就是“foo bar”,并为其加下“.c”的后缀,于是,我们的依赖目标就是“foo.c bar.c”。而命令中的“$<”和“$@”则是自动化变量,“$<”表示所有的依赖目标集(也就是“foo.c bar.c”),“$@”表示目标集(也就是“foo.o bar.o”)。于是,上面的规则展开后等价于下面的规则: foo.o : foo.c $(CC) -c $(CFLAGS) foo.c -o foo.o bar.o : bar.c $(CC) -c $(CFLAGS) bar.c -o bar.o 试想,如果我们的“%.o”有几百个,那种我们只要用这种很简单的“静态模式规则”就可以写完一堆规则,实在是太有效率了。“静态模式规则”的用法很灵活,如果用得好,那会一个很强大的功能。再看一个例子: files = foo.elc bar.o lose.o $(filter %.o,$(files)): %.o: %.c $(CC) -c $(CFLAGS) $< -o $@ $(filter %.elc,$(files)): %.elc: %.el emacs -f batch-byte-compile $< $(filter %.o,$(files))表示调用Makefile的filter函数,过滤“$filter”集,只要其中模式为“%.o”的内容。其的它内容,我就不用多说了吧。这个例字展示了Makefile中更大的弹性。 八、自动生成依赖性 在Makefile中,我们的依赖关系可能会需要包含一系列的头文件,比如,如果我们的main.c中有一句“#include "defs.h"”,那么我们的依赖关系应该是: main.o : main.c defs.h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酣楼驻海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值