2.2、shell和makefile基础知识(2021-4-15)

2.2.1.shell介绍

2.2.1.1、shell是操作系统的终端命令行

在这里插入图片描述

2.2.1.2、shell是一类编程语言

在这里插入图片描述

2.2.1.3、常用shell语言:sh、bash、csh、ksh、perl、python等

在这里插入图片描述

2.2.1.4、shell脚本的运行机制:解释运行

在这里插入图片描述


2.2.2.动手写第一个shell

2.2.2.1、编辑器、编译器、运行方法(脚本的3种执行方法)

在这里插入图片描述

2.2.2.2、hello world程序和解释

#!/bin/sh
#    #开头的是注释

echo "hello world."
touch a.c

在这里插入图片描述

2.2.2.3、shell并不神秘

在这里插入图片描述


2.2.3.shell编程学习1

2.2.3.1、shell中使用linux命令

在这里插入图片描述

#!/bin/sh
# 

# 在当前目录下创建a.txt
touch a.txt

#在当前目录下创建文件夹dir,dir下创建b.txt
mkdir dir
cd dir
touch b.txt
cd ..

2.2.3.2、shell中的变量定义和引用

在这里插入图片描述

#变量的定义和使用
string="hello world"
string="new string"
echo $string

2.2.3.3、shell中无引用、单引号和双引号的区别

在这里插入图片描述

#无引用、单因号、双引号
echo new string
echo 'new st\"ring'
echo "new st\"ring"

var="\$a\$"
echo $var


2.2.4.shell编程学习2

2.2.4.1、shell中调用linux命令

在这里插入图片描述

#!/bin/sh
#comment

PWD=`pwd`
echo $PWD

MYPATH="`pwd`/include"
MYPATH2='`pwd`/include'
echo "MYPATH = $MYPATH"
echo "MYPATH2 = $MYPATH2"

2.2.4.2、shell中的选择分支结构

在这里插入图片描述

#!/bin/sh
#comment

#判断文件a.txt是否存在,如果不存在则创建
#if [ -f a.txt ]; then
#       echo "yes"
#else
#       echo "no"
#       touch a.txt
#fi


#判断字符串是否相等,注意,此处=前后要有空格
#if [ "abc" = "abd" ];then
#       echo "equal"
#else
#       echo "no equal"
#fi


#判断数字是否相等,大雨,小于,大于等于,小于等于
#if [ 25 -eq 30 ];then
#       echo "equal"

#判断字符串是否为空
#str="a"
#if [ -z $str ];then
#       echo "wei kong"
#else
#       echo "bu wei kong"
#fi


#逻辑或-o
#if [ 12 -eq 12 -o 13 -eq 15 ];then
#       echo "yes"
#else
#       echo "no"
#fi


#简写的if表达式
#此时str没定义,所以str不为0,前者为假,打印非空
[ -z $str ] || echo "fei kong"


2.2.5.shell中的循环结构

2.2.5.1、for循环

在这里插入图片描述

#!/bin/sh
# 循环结构

#for循环
#for i in 1 2 3 4 5
for i in `ls`
do
        echo $i
done

2.2.5.2、while循环

在这里插入图片描述

#!/bin/sh
# while循环
i=1
j=11
while [ $i -lt $j ]; do
       echo $i
       i=$(($i + 1))

#done

2.2.5.3、echo的创建和追加输入文件

在这里插入图片描述

# echo指令创建爱你文件和追加输入文件
# 注意:一个大于号是创建文件;两个大于号是追加内容
echo "#include<stdio.h>
int main( int argc, char *argv[] )
{
        return 0;
}">a.txt
# eho指令向已经存在的文件中追加输入内容
echo "追加的内容">>a.txt


2.2.6.shell中其他值得关注的知识点

2.2.6.1、case语句

在这里插入图片描述

#!/bin/sh
# 注释

# case语言演示
var=1
case $var in
       1) echo "1";;
       2) echo "2";;
#esac

2.2.6.2、调用shell程序的传参

在这里插入图片描述

# 演示使用$# $0 $1等传参
echo $# $0 $1 $2 $3

2.2.6.3、while循环和case语言和传参结合

在这里插入图片描述

# 演示shift指令和$# $1等的关系
echo $# $1
shift
echo $# $1


2.2.7.Makefile基础回顾

//led.bin:目标。led.o:依赖
led.bin: led.o 
	arm-linux-ld -Ttext 0x0 -o led.elf $^
	arm-linux-objcopy -O binary led.elf led.bin
	arm-linux-objdump -D led.elf > led_elf.dis
	gcc mkv210_image.c -o mkx210
	./mkx210 led.bin 210.bin
	
//	%.o:代表所有以.o结尾的文件
//	%.S:代表所有以.S结尾的文件
%.o : %.S
	arm-linux-gcc -o $@ $< -c
	
//	%.c:代表所有以.c结尾的文件
%.o : %.c
	arm-linux-gcc -o $@ $< -c 

//伪目标。执行这个目标不是为了得到某个文件或东西,而是单纯为了执行这个目标下面的命令。
.PHONY clean
clean:
	rm *.o *.elf *.bin *.dis mkx210 -f

2.2.7.1、Makefile的作用和意义

在这里插入图片描述

2.2.7.2、目标、依赖、命令

在这里插入图片描述

2.2.7.3、通配符%和Makefile自动推导(规则)

在这里插入图片描述

2.2.7.4、Makefile中定义和使用变量

在这里插入图片描述

2.2.7.5、伪目标(.PHONY)

在这里插入图片描述

2.2.7.6、Makefile的文件名

在这里插入图片描述

2.2.7.7、Makfile中引用其他Makefile(include指令)

在这里插入图片描述


2.2.8.Mafile补充学习1

2.2.8.1、Makefile中的注释用

在这里插入图片描述

2.2.8.2、命令前面的@用来静默执行

在这里插入图片描述

#演示Makefile
all:
#       echo "hello world"
        @echo "hello world"

2.2.8.3、Makefile中几种变量赋值运算符

在这里插入图片描述

var="abcd"
var?="efgh"
all:
        echo $(var)
A=abc
B=$(A)def
#B:=$(A)def
A=gh
all:
        echo $B

2.2.8.4、Makefile的环境变量

在这里插入图片描述


2.2.9.Makefile补充学习2

2.2.9.1、Makefile中使用通配符

在这里插入图片描述

all:1.c 2.c 12.c test.c 1.h
        echo *.c
        echo ?.c
        echo [12].c

2.2.9.2、Makefile的自动变量

在这里插入图片描述

all:1.c 2.c 12.c test.c 1.h
        echo $@
        echo $<
        echo $^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值