【shell】从文件中读取参数传给驱动模块

来源:互联网 发布:申根签证截止日期 知乎 编辑:程序博客网 时间:2024/05/16 17:41

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 -'1p' test.txt )
  4. people=$( sed -'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

0 0
原创粉丝点击