shell脚本补充和Makefile+GDB

shell的函数

格式1:

function 函数名()

{

函数体

}

调用:

函数名

ls -a

格式2:

函数名()

{

函数体

}

调用:

函数名

ls -a

格式3:

function 函数名

{

函数体

}

调用:

函数名

ls -a

练习:

定义四个无参无返函数实现+-*/

有无参函数

shell的参数是通过位置变量获取的,位置变量默认是局部变量

位置变量:获取终端输入的字符串

格式:

function 函数名()

{

$0 --->文件名

$1 --->参数名1

$2 --->参数名2

}

练习:

在终端输入表达式,定义一个函数传参,计算乘积

有无返回值函数

格式1:返回全局变量

function 函数名()

{

a=100

函数体

}

函数名 参数1 参数2

直接使用全局变量

格式2:使用return返回

function 函数名()

{

函数体

return 表达式

}

函数名 参数1 参数2

$? //获取上一行指令的值

return后面的表达式的值只能是0-255之间的数

如果return后面的表达式的值不在0-255之间,则发生越界,类似C语言

return后面的表达式不允许是字符串

格式3:使用echo实现返回

function 函数名()

{

函数体

echo hello world

}

变量=`函数名 参数`

变量=$(函数名 参数)

练习:

在终端输入用户名

定义有参有返函数,如果用户存在,则返回uid(用户ID),gid(组ID)

定义有参有返函数,如果用户存在,请回答用户是否加密,并返回用户所使用的编译器

 

 

Makefile(重点)

什么是Makefile

Makefile是一个工程项目管理的一个项目文件,文件名为Makefile

注意:如果makefile(小写)和Makefile(大写)同时存在,则默认执行makefile(小写)

作用:当文件未发生修改时,Makefile不对该文件进行编译等操作,节省时间,加快执行速度

Makefile是根据时间戳去判断文件是否发生修改

Makefile的单行注释:#(同shell)

Makefile的分布编译

汇编:把源文件转化为二进制文件

gcc -c ***.c -o ***.o

链接:把二进制文件转化为可执行文件

gcc ***.o -o 可执行文件

Makefile的组成

Makefile是由规则组成的,规则是由目标,依赖,shell指令组成

Makefile默认第一个目标为all

规则格式:

目标:依赖

shell指令(shell指令前必须有个Tab)

注意:

目标:必须存在

依赖:可有可无,多个依赖之间使用空格隔开

shell指令:可有可无

认识make

Make类似gcc是一个编译器

格式:

make 目标

注意:

目标:可以省略不写,则默认执行第一个目标

如果不存在make则需要安装

sudo apt install make

Makefile完整实现流程

Makefile的变量以及赋值

格式:

变量=初始化的值

引用:

$变量

$(变量)

赋值

= //递归赋值,延时赋值,赋值为最后一个结果

:= //立刻赋值,在哪赋值在哪展开

?= //条件赋值,如果变量以及赋值则不发生赋值,

+= //追加赋值,数据之间默认使用空格隔开

she

自动类型变量

$@ //表示目标

$^ //表示所有依赖

$< //表示第一个依赖

*通配符:匹配一个或多个字符

%模式匹配

Makefile的内置函数

wildcard

功能:匹配当前目录下的某种文件

格式:

$(wildcard *.o)

patsubst

功能:把一种文件类型替换为另一种

格式:

$(patsubst 被替换的格式,要替换的格式,替换的字符串)

$(patsubst %.c,%.o,.c 2.c 3.c)

伪目标

功能:如果当前目录下存在文件名和目标一致时,make会默认执行文件,忽略目标

格式:

.PHONY:clean

GDB

什么是GDB

功能:bug调试器

查看是否具备gdb

gdb --version

此情况表示有gdb

否则要安装

sudo apt install gdb

gdb调试二进制文件

gdb program

program:为二进制文件(a.out)

创建一个c环境

gcc -g ***.c --->默认生成一个a.out文件

-g:生成一个gdb可识别文件

1.打断点
      break [file:]function
          Set a breakpoint at function (in file).
    b 行号
    b 函数名
2.执行程序                    

      run [arglist]
          Start your program (with arglist, if specified).

      bt  Backtrace: display the program stack.
3.打印表达式
      print expr
          Display the value of an expression.
4.继续执行
      c   Continue running your program (after stopping, e.g. at a breakpoint).
5.执行下一步
      next
          Execute next program line (after stopping); step over any function calls in
          the line.
6.查看在哪一行发生了什么
      edit [file:]function
          look at the program line where it is presently stopped.
7.打印文件默认打印10行
      list [file:]function
          type the text of the program in the vicinity of where it is presently
          stopped.
8.进入函数调用
      step
          Execute next program line (after stopping); step into any function calls in
          the line.

      help [name]
          Show information about GDB command name, or general information about using
          GDB.
9.退出
      quit
          Exit from GDB.
10,info 指令
11,删除
    d 编号
12.gdb可以写shell指令
    shell 指令
    1.打断点
      break [file:]function
          Set a breakpoint at function (in file).
    b 行号
    b 函数名
2.执行程序                    

      run [arglist]
          Start your program (with arglist, if specified).

      bt  Backtrace: display the program stack.
3.打印表达式
      print expr
          Display the value of an expression.
4.继续执行
      c   Continue running your program (after stopping, e.g. at a breakpoint).
5.执行下一步
      next
          Execute next program line (after stopping); step over any function calls in
          the line.
6.查看在哪一行发生了什么
      edit [file:]function
          look at the program line where it is presently stopped.
7.打印文件默认打印10行
      list [file:]function
          type the text of the program in the vicinity of where it is presently
          stopped.
8.进入函数调用
      step
          Execute next program line (after stopping); step into any function calls in
          the line.

      help [name]
          Show information about GDB command name, or general information about using
          GDB.
9.退出
      quit
          Exit from GDB.
10,info 指令
11,删除
    d 编号
12.gdb可以写shell指令
    shell 指令
13.设置日志
    set loging on

gdb调试core文件

  1. gdb program core

program:为二进制文件(a.out)

core:当出现重大错误(段错误)产生镜像文件,保存现场

  1. 创建一个c环境
  2. gcc -g ***.c --->生成一个gdb可识别的二进制文件

*如果没有core文件

方式一:

ulimit -a --->查看文件是否受限制

如果受限

ulimit -c unlimited //设置为为限制

方式二:

在终端运行以下语句
    sudo bash -c "echo core > /proc/sys/kernel/core_pattern "
    在重新执行即可

gdb调试正在执行执行文件

gdb program 1234

gdb -p 1234

program:为二进制文件(a.out)

  1. 创建一个c环境
  2. gcc -g ***.c --->生成一个gdb可识别的二进制文件

./a.out

  1. gdb a.out -p 进程号

查看进程号:ps -ef | grep "a.out"

  1. 如果调试失败

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值