Chinaunix首页 | 论坛 | 博客

OS

  • 博客访问: 2228861
  • 博文数量: 691
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2660
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-05 12:49
个人简介

不浮躁

文章分类

全部博文(691)

文章存档

2019年(1)

2017年(12)

2016年(99)

2015年(207)

2014年(372)

分类: LINUX

2014-12-07 17:29:50

1.1 Shell脚本从文件中读取参数传给驱动模块

问题描述:insmod驱动模块需要几个参数,不过这几个参数需要从配置文件中读取,为了开机时自动化处理还需要把这些操作都放在一个脚本中。

下面是一个helloworld驱动的demo,这个demo需要两个参数: char * people int num

1.2 hello.c

  1. root@ubuntu:~/test/shreadf# cat hello.c
  2. #include
  3. #include
  4. MODULE_LICENSE("Dual BSD/GPL");
  5. static char * people= "wang";
  6. static int num= 1;
  7. static int hello_init(void)
  8. {
  9.     printk(KERN_ALERT"num=%d people=%s\n", num, people);
  10.     return 0;
  11. }
  12. static void hello_exit(void)
  13. {
  14.     printk(KERN_ALERT"Hello world exit\n");
  15. }
  16. module_init(hello_init);
  17. module_exit(hello_exit);
  18. module_param(num, int, S_IRUGO);
  19. module_param(people, charp, S_IRUGO);
  20. EXPORT_SYMBOL(num);
  21. EXPORT_SYMBOL(people);


1.3 Makefile


  1. root@ubuntu:~/test/shreadf# cat Makefile
  2. ifneq ($(KERNELRELEASE),)
  3.     obj-m:=hello.o
  4. else
  5.     KDIR =/lib/modules/$(shell uname -r)/build
  6.     PWD:=$(shell pwd)
  7. default:
  8. $(MAKE) -C $(KDIR) M=$(PWD) modules


这个驱动需要的参数在 text.txt中,

root@ubuntu:~/test/shreadf# cat test.txt

1234

abcdefg

第一行是参数num, 第二行是参数people

1.4 测试

编写好后测试一下驱动有没有问题:

root@ubuntu:~/test/shreadf# insmod hello.ko num=1 people=" zhangsan"

root@ubuntu:~/test/shreadf# dmesg | tail

[34398.230034] num=1 people=zhangsan

shell脚本中读取参数用sed 就好了。

但是在给int 型的num传参数时直接用 num=$num时会报错,但是加expr就成功了。
虽然这个脚本可以用了,但是还不是特别理解,个人感觉这个表达表就跟
shell中做四则运算时都要加expr一样,这个地方为了类型匹配也需要加expr

1.5 脚本

  1. root@ubuntu:~/test/shreadf# cat shread.sh
  2. #!/bin/sh
  3. num=$( sed -n '1p' test.txt )
  4. people=$( sed -n '2p' test.txt )
  5. echo "num=$num"
  6. echo "people=$people"
  7. #insmod hello.ko num=1 people=" zhangsan"
  8. insmod hello.ko num=`expr $num` people=$people




阅读(711) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~