Linux shell 脚本编写

知识点回顾:
1、Unix&Linux,GUN,Ubuntu,Red Hat,Fedora。
2、Linux用户管理。
—Root用户,普通用户,用户组。
—useradd,passwd,usermod,finger,sudo(su),groupadd,groups,whoami,who。
3、文件和目录操作。
—文件名和文件拓展名
—层次的文件结构
—In linux,Everything is file,if something is not file,it is ?(process)
—文件类型:普通文件,符号链接文件,目录文件,设备文件等。
—Touch,vim,cat,ls,pwd,cd,cp,rm,mkdir,rmdir,file。
—Chmod,umask。
—改变文件属主:chown。
—改变文件用户组:chgrp。
—其他的一些命令:more,less,tail,head,find。
4、一个编辑器vi(vim)的使用
—两种模式:编辑模式,命令模式。
—Vi基本编辑功能:x, dd, i, c.
—推出vi:q, q!, wq.

参考资料
1、鸟哥的linux私房菜:
http://linux.vbird.org/linux_basic/0320bash.php#bash
2、IBM shell编程系列:
http://www.ibm.com/developerworks/cn/linux/shell/
3、Linux Shell Scripting(详细英文文档) :
http://bash.cyberciti.biz/guide/Main_Page

Shell简介
1、Shell是什么?
Shell是系统的用户界面,提供了用户和内核进行互操作的一种接口。它接受用户的输入,并将命令传送到内核中去执行。此外,shell也能被系统中其他有效的linux实用程序和应用程序调用。
这里写图片描述

2、学习shell有何用途??
—自动化工具:自动化软件测试等
—文本处理:sed,awk
—批处理
—Unix界有一句名言:“一行shell脚本胜过万行C程序”
—求职面试的需求:有兴趣去看看阿里,百度,腾讯等公司的招聘要求。只要是面向系统的几乎全部需要熟悉linux操作系统,熟悉shell编程。

博主这边主要是在Android系统编译打包时要用到这个shell脚本文件,基本上都是用的这个shell脚本,去操作系统文件导入,压缩,copy,move,makedir,权限修改等,所以我们很有必要去了解这个shell程序的编写

这里写图片描述
build.makeimage代码片段:

if [[ ! $SUDING_BT_VERSION -ne 1 ]];then
    echo "suding bt"
    #bin
    cp -vf $AUTOCHIPS_DIR/packages/externalsysapk/SUDINGBT/bin/*  $OUTPUT_SYS/bin
    #app
    cp -vf $AUTOCHIPS_DIR/packages/externalsysapk/SUDINGBT/apk/*  $OUTPUT_SYS/app

    #bt tools
    cp -vrf $AUTOCHIPS_DIR/packages/externalsysapk/SUDINGBT/tools/*  $OUTPUT_SYS

    #for bt app save the bt power state file
    if [ ! -f $OUTPUT_DIR/data/power_status ];then
        touch $OUTPUT_DIR/data/power_status
        chmod 666 $OUTPUT_DIR/data/power_status
    fi

    #rm mtk apk
    rm -rf $OUTPUT_SYS/app/AutoBT.apk
    rm -rf $OUTPUT_SYS/app/AutoBTService.apk
else
    echo " mtk bt"
    rm -rf $OUTPUT_SYS/bin/blueletd
    rm -rf $OUTPUT_SYS/bin/bt_test
    rm -rf $OUTPUT_SYS/lib/libbluelet.so
    rm -rf $OUTPUT_SYS/lib/libbt_platform.so
    rm -rf $OUTPUT_SYS/bin/serial_rate_to_file
    rm -rf $OUTPUT_SYS/lib/libserialport_bt.so
    rm -rf $OUTPUT_SYS/app/AutoBtService.apk
    rm -rf $OUTPUT_SYS/app/AutoBtClient.apk
    rm -rf $OUTPUT_SYS/app/AutoBtMusic.apk
fi

Shell分类:各个shell中内置命令不相同

Bourne shell(/bin/sh) 
C shell(/bin/csh)
Korn shell(/bin/ksh)
Bourne again shell(/bin/bash)
Tenex C shell(tcsh)

Shell中的变量和变量的设定及使用

1、shell本地变量:本地变量只在本shell中起作用。
—变量无需声明
—变量调用:在变量前加 ,echo HOME
—unset删除变脸赋值
—有条件命令替换:

${variable: -value}
${variable:=value}
${variable: +value}
${variable:?message}

2、环境变量
—查看环境变量:env
—常见环境变量作用(实验作业)
—export将一个局部变量提供给shell的其他命令使用
—修改环境变量
3、特定变量参数

$#:传递给脚本的参数的个数
$*:显示所有向脚本传递的参数
$$:脚本运行的id号
$!:后台运行的第一个进程的id号
$?:最后一个命令执行情况,0表示没有错误
$@:所有参数,每次参数使用””括起来

Shell输入输出重定向
重定向是什么?将shell命令的标准输入,输出,错误重定向到指定的文件中。

这里写图片描述
stdin(0表示):<, <<
stdout(1表示):>, >>
stderr(2表示):2>, 2>>
/dev/null:黑洞

Pipe
Pipe是什么?将一个进程的标准输出连接到另外进程的标准输入。
这里写图片描述
Pipe简单应用
—cut:从文件中的每一行剪切一定量的字符,送到stdout。
—grep
—sort,wc,uniq
—tee –a [file],将管道中的数据写入[file]中,同时将数据写入stdin。
这里写图片描述

Shell 编写
1、打造自己的shell编程环境
—Shell功能
—Shell中的变量和变量设定,使用
—个人环境变量配置
—Shell输入输出重定向
—Shell中pipe
2、第一个shell程序Hello Shell
—Shell程序结构
—执行shell程序
3.Shell的输入输出
4.Shell控制结构
if – else
do while
for
5.Shell中的函数
6.Shell脚本调试

下面编写shell脚本,了解程序结构
这里写图片描述
改变文件属性:sudo chmod +x helloshell.sh
执行该文件: ./helloshell.sh或者是sh ./helloshell.sh

这里写图片描述

标准输出:echo,注意转移字符,\t, \n, \”等。
这里写图片描述
标准输入: read,cat
这里写图片描述

shell控制结构

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

shell中的函数

这里写图片描述

这里写图片描述

其实这个shell 编程语法表现的很随意,shell程序80%的时间基本上都是用于调试的。

It bombs with a "syntax error" message, or
It runs, but does not work as expected (logic error)
It runs, works as expected, but has nasty side effects (logic bomb)

那么该如何调试?
1、在任何可能出错的地方使用echo打印变量的值。
这里写图片描述
2、使用tee命令在关键点检查数据流
3、设置shell运行时参数
sh -n your_script:返回所有的语法错误
sh -x your_script:显示所有变量的值
4、使用图形化的调试工具
http://bashdb.sourceforge.net/
这里写图片描述
5、使用assert实现

#!/bin/bash
# filename: assert.sh

assert ()                 #  If condition false,
{                         #+ exit from script with error message.
  E_PARAM_ERR=98
  E_ASSERT_FAILED=99

  if [ -z "$2" ]          # Not enough parameters passed.
  then
    return $E_PARAM_ERR   # No damage done.
  fi

  lineno=$2

  if [ ! $1 ]
  then
    echo "Assertion failed:  \"$1\""
    echo "File \"$0\", line $lineno"
    exit $E_ASSERT_FAILED
  # else
  #   return
  #   and continue executing script.
fi
}


a=5
b=4
condition="$a -lt $b"     # Error message and exit from script.
                          #  Try setting "condition" to something else,
                          #+ and see what happens.
# assert failure
assert "$condition" $LINENO

demo点击下载

另附相关参考资料及教程说明:
http://www.linuxidc.com/Linux/2013-02/79468.htm
使用env命令查看系统中的换将变量,并解释变量对应的含义。
http://linux.vbird.org/linux_basic/0320bash.php
Shell下的gui(libnotify-bin)编程
http://blog.mpathirage.com/2009/09/12/using-libnotify-in-ubuntu-9-04/

猜你喜欢

转载自blog.csdn.net/u013171283/article/details/79204476