博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
移植 bash
阅读量:4186 次
发布时间:2019-05-26

本文共 3497 字,大约阅读时间需要 11 分钟。

移植 bash
一般情况下,在ARM开发板中都会有sh,这是随busybox携带的脚本工具。如果想将shell脚本在ARM目标机中运行,则需要把脚本第一行的
#!/bin/bash
改为
#!/bin/sh
。但这样虽然可以执行脚本,但是脚本中的许多语法是不支持的,例如:
1、判断: if [ -d /usr ] ; if [ -b /dev/sda1 ]
2、数值运算:sum=$[$val1+$val2]
3、逻辑运算:and=$[ $val1 & $val2 ]
解决方法就是将bash移植到目标板。

0 移植环境:                                                                                
主机:Intel(R) Xeon(R) CPU           X5650  @ 2.67GHz
Linux version 3.2.0-32-generic (
) (gcc version 4.6.3 (Ubuntu/L
inaro 4.6.3-1ubuntu5)
目标平台:Amlogic Meson8(S802)
android 4.4 (Linux version 3.10.33)

1 下载bash源码

地址:


2 解压

将下载的bash压缩包解压,命令:

$ mkdir /home/hui.zhang/w/tmp/shell/                 # 创建bash工作目录$ cp bash-4.3.tar.gz /home/hui.zhang/w/tmp/shell/    # 复制安装包$ cd /home/hui.zhang/w/tmp/shell/                    # 进入/home/veryarm/bash目录$ tar zxvf bash-4.3.tar.gz                           # 解压

3、配置

编译之前,要进行配置,命令:

$ mkdir install                                 # 创建安装目录$ cd bash-4.3# 进入目录$ export PATH=$PATH:/opt/gcc-linaro-arm-linux-gnueabihf/bin# 添加交叉编译器路径到PATH环境变量中,最好用与编译内核用的交叉编译器版本一致$ ./configure CC=arm-linux-gnueabihf-gcc --prefix=/home/hui.zhang/w/tmp/shell/install  --host=arm-linux --target=arm-linux-gnueabihf --enable-static-link --enable-history --without-bash-malloc  --cache-file=arm-linux.cache

CC:指定交叉编译器工具
--prefix:指定安装目录
--host:运行在的主机环境
--enable-static-link:静态链接
更多配置项,见$./configure --help

4 编译

编译并安装:

$ make & make install

5 复制到目标板

装成功后在安装路径/home/hui.zhang/w/tmp/shell/istall下生成两个目录 bin 和 share,将bin中的 bash 可执行文件复制至开发板 /bin 中,并修改执行权限:

root@k200:/ #cp bash /bin root@k200:/ #chmod +x /bin/bash

 

6 测试

在开发板中运行bash

root@k200:/ #bash bash-4.3#

在开发版中运行相关脚本(hello world):

root@k200:/ # cat /data/test.sh                                                #!/system/bin/bashecho hello world!root@k200:/ # chmod 755 /data/test.sh root@k200:/ # /data/test.sh                                                    hello world!root@k200:/ # 

恭喜,移植成功!

7 问题记录:

(1)配置报错

$ ./configure CC=arm-linux-gnueabihf-gcc --prefix=/home/hui.zhang/w/tmp/shell/install  --host=arm-linux --target=arm-linux-gnueabihf --enable-static-link --enable-history --without-bash-malloc

checking whether getpgrp requires zero arguments... yes
checking whether setvbuf arguments are reversed... configure: error: cannot run test program while cross compiling
解决:
交叉编译的时候测试程序是不可以在HOST上运行就会出现: error: cannot run test program while cross compiling 类似的错误,可以使用CACHEFILE解决这个问题,
查看错误位置:configure: 
9983 echo "$as_me:$LINENO: checking whether setvbuf arguments are reversed" >&5
 9984 echo $ECHO_N "checking whether setvbuf arguments are reversed... $ECHO_C" >&6
 9985 if test "${ac_cv_func_setvbuf_reversed+set}" = set; then
 9986   echo $ECHO_N "(cached) $ECHO_C" >&6
 9987 else
 9988   if test "$cross_compiling" = yes; then
 9989   { { echo "$as_me:$LINENO: error: cannot run test program while cross compiling" >&5
 9990 echo "$as_me: error: cannot run test program while cross compiling" >&2;}
 9991    { (exit 1); exit 1; }; }
 9992 else
 9993   cat >conftest.$ac_ext <<_ACEOF

checking whether getpgrp requires zero arguments... yes
checking whether setvbuf arguments are reversed... configure: error: cannot run test program while cross compiling
解决:echo ac_cv_func_setvbuf_reversed=yes>arm-linux.cache
$ ./configure CC=arm-linux-gnueabihf-gcc --prefix=/home/hui.zhang/w/tmp/shell/install  --host=arm-linux --target=arm-linux-gnueabihf --enable-static-link --enable-history --without-bash-malloc  --cache-file=arm-linux.cache

(2)段错误。前面问题搞定后,最后测试时报错:

root@k200:/ #bash segment fault

由于bash3.1之前的版本源文件包2M多,后续的版本都将近8M,猜想3.1会精简一些,第一次移植的是bash3.1,段错误应该是这个版本源码本身问题。更换为bash-4.3.tar.gz,果然运行正常。


参考文档:

转载地址:http://nwcoi.baihongyu.com/

你可能感兴趣的文章
Qt中文件读取的几种方式
查看>>
pyqt实现界面化编程
查看>>
qt写DLL文件并调用和出现的问题分析
查看>>
工厂模式(Factory)-设计模式(一)
查看>>
建造者模式(Builder)-设计模式(三)
查看>>
Qt 怎么给QWidget添加滚动条
查看>>
双十一冲刺业绩,完不成杀运营祭天?程序员:你们也有今天
查看>>
搜狗输入法到底算不算恶意挟持百度搜索流量?五个测试告诉你答案
查看>>
百度成为美国领先的人工智能联盟的第一个中国成员
查看>>
程序员资讯:QR代码在公共交通中得到越来越多的采用
查看>>
当了将近十年的程序员,为什么从来没见过程序员带孩子
查看>>
程序员面试中最容易碰到的五个套路!应届生最容易上当
查看>>
三种不同的程序员,你属于哪一种?如果要裁员,你会让谁走?
查看>>
干货神总结,程序员面试技巧
查看>>
深度解析BAT三家互联网公司,为什么腾讯产品第一,百度技术第一,阿里运营第一?
查看>>
程序员发贴求助:剪短头发能缓解脱发吗?网友:我觉得秃头挺好的
查看>>
史上最难程序员的面试题!谷歌、百度、微软、阿里必答题
查看>>
为什么会出现“程序员千万不要学算法”这种言论?
查看>>
程序员如何做到快速升职?这几点你都做到了吗?
查看>>
第五届世界互联网大会重点介绍工业互联网
查看>>