这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » Linux shell sed awk

共3条 1/1 1 跳转至

Linux shell sed awk

高工
2019-03-01 12:50:21     打赏

1、我们的平台编译kernel的时候总是要输入项目名称,很麻烦

################################################

## Compile kernel and pack it into boot.img   ##

################################################

#!/bin/sh


PROJECT=$1

echo $PROJECT

if [ "$1" = "" ];then

    echo "Use: makeMtk.ini file for the mkkernel project_name"

#exit  修改 sen -n "1p" makeMtk.ini获取makeMtk.ini的第一行数据

#awk '{print $3}'获取第三个参数 

#read LINE 把之前传进来的参数读取出来

    sed -n "1p" makeMtk.ini|awk '{print $3}'|while read LINE

    do

    PROJECT=$LINE

    ./makeMtk $PROJECT new kernel

    ./makeMtk $PROJECT remake kernel

    echo ./mediatek/build/tools/images/mkbootimg --kernel ./out/Download/flash/kernel_$PROJECT.bin --ramdisk ./out/target/product/$PROJECT/ramdisk.img -o ./out/target/product/$PROJECT/boot.img

    ./mediatek/build/tools/images/mkbootimg --kernel ./out/Download/flash/kernel_$PROJECT.bin --ramdisk ./out/target/product/$PROJECT/ramdisk.img -o ./out/target/product/$PROJECT/boot.img

    done

    #编译完成后退出    

    exit

else

    echo "Use: $1 for the project name"

    PROJECT=$1

fi

echo ./makeMtk $PROJECT new kernel

./makeMtk $PROJECT remake kernel

echo ./mediatek/build/tools/images/mkbootimg --kernel ./out/Download/flash/kernel_$PROJECT.bin --ramdisk ./out/target/product/$PROJECT/ramdisk.img -o ./out/target/product/$PROJECT/boot.img

./mediatek/build/tools/images/mkbootimg --kernel ./out/Download/flash/kernel_$PROJECT.bin --ramdisk ./out/target/product/$PROJECT/ramdisk.img -o ./out/target/product/$PROJECT/boot.img

代码里面我们每次要编译内核的时候都要输入./mkkernel mid713l_lp_lvds mid713l_lp_lvds 是项目名称,我觉得很麻烦。


2、sed读取文件的某一行

读取文件的某一行可以看看sed 这个命令 

sed -n “1p” mkkernel 显示文件的第一行 

sed -n “1,4p” mkkernel 显示文件第一行到第四行


weiqifa@weiqifa-Inspiron-3847:~/weiqifa/new_tm100/tm100$ sed -n "1p" mkkernel 

################################################

weiqifa@weiqifa-Inspiron-3847:~/weiqifa/new_tm100/tm100$ sed -n "1,4p" mkkernel 

################################################

## Compile kernel and pack it into boot.img   ##

################################################

#!/bin/sh

weiqifa@weiqifa-Inspiron-3847:~/weiqifa/new_tm100/tm100$ 

1

2

3

4

5

6

7

8

3、 awk截取某一列

awk ‘{print $3}’ 把管道前面的第三列显示出来


weiqifa@weiqifa-Inspiron-3847:~/weiqifa/new_tm100/tm100$ cat makeMtk.ini

project = mid713l_lp_lvds

build_mode = eng

weiqifa@weiqifa-Inspiron-3847:~/weiqifa/new_tm100/tm100$ cat makeMtk.ini |awk '{print $3}'

mid713l_lp_lvds

eng

weiqifa@weiqifa-Inspiron-3847:~/weiqifa/new_tm100/tm100$ 

1

2

3

4

5

6

7

8

4、while read LINE ;do done 读取文件

do done会重新起一个子shell 这是为什么我不能把$LINE传下去的原因


sed -n "1p" makeMtk.ini|awk '{print $3}'|while read LINE

do

PROJECT=$LINE

./makeMtk $PROJECT new kernel

./makeMtk $PROJECT remake kernel

echo ./mediatek/build/tools/images/mkbootimg --kernel ./out/Download/flash/kernel_$PROJECT.bin --ramdisk ./out/target/product/$PROJECT/ramdisk.img -o ./out/target/product/$PROJECT/boot.img

./mediatek/build/tools/images/mkbootimg --kernel ./out/Download/flash/kernel_$PROJECT.bin --ramdisk ./out/target/product/$PROJECT/ramdisk.img -o ./out/target/product/$PROJECT/boot.img

done   

1

2

3

4

5

6

7

8

5、用set -x set +x调试上面的说法

################################################

## Compile kernel and pack it into boot.img   ##

################################################

#!/bin/sh

set -x

if [ "$1" = "" ];then

        echo "Use: makeMtk.ini file for the mkkernel project_name"

#exit  修改 sen -n "1p" makeMtk.ini获取makeMtk.ini的第一行数据

#awk '{print $3}'获取第三个参数 

#read LINE 把之前传进来的参数读取出来

        sed -n "1p" makeMtk.ini|awk '{print $3}'|while read LINE

        do

        PROJECT=$LINE

        echo $PROJECT

        break

        done

        echo $PROJECT

        #编译完成后退出 

        #exit

else

        echo "Use: $1 for the project name"

        PROJECT=$1

fi

echo ./makeMtk $PROJECT new kernel

set +x

#./makeMtk $PROJECT remake kernel

#echo ./mediatek/build/tools/images/mkbootimg --kernel ./out/Download/flash/kernel_$PROJECT.bin --ramdisk ./out/target/product/$PROJECT/ramdisk.img -o ./out/target/product/$PROJECT/boot.img

#./mediatek/build/tools/images/mkbootimg --kernel ./out/Download/flash/kernel_$PROJECT.bin --ramdisk ./out/target/product/$PROJECT/ramdisk.img -o ./out/target/product/$PROJECT/boot.img                                                                                                                                                                                                                                                  


输出结果如下


weiqifa@weiqifa-Inspiron-3847:~/weiqifa/new_tm100/tm100$ ./wmkkernel 

++ '[' '' = '' ']'

++ echo 'Use: makeMtk.ini file for the mkkernel project_name'

Use: makeMtk.ini file for the mkkernel project_name

++ sed -n 1p makeMtk.ini

++ read LINE

++ awk '{print $3}'

++ PROJECT=mid713l_lp_lvds

++ echo mid713l_lp_lvds

mid713l_lp_lvds

++ break

++ echo


++ echo ./makeMtk new kernel

./makeMtk new kernel

++ set +x

weiqifa@weiqifa-Inspiron-3847:~/weiqifa/new_tm100/tm100$ vim wmkkernel 




管理员
2019-03-04 09:31:29     打赏
2楼

谢谢楼主分享


院士
2019-03-06 13:35:55     打赏
3楼

记下来,再看看


共3条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]