博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell编程基础
阅读量:4103 次
发布时间:2019-05-25

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

(1)它必须以如下行开始(必须放在文件的第一行):

      # !/bin/sh
符号#!用来告诉系统执行该脚本的程序,本例使用/bin/sh。编辑结束并保存后,如果要执行该脚本,必须先使其可执行:
      chmod +x filename
此后在该脚本所在目录下,输入 ./filename 即可执行该脚本。
(2)变量赋值和引用。Shell编程中,使用变量无需事先声明,需要给变量赋值时,可以这么写:  变量名=值 。
要取用一个变量的值,只需在变量名前面加一个$ ( 注意: 给变量赋值的时候,不能在"="两边留空格 )
      # 对字符串变量赋值:
      a="hello world" 
      # 打印字符串变量a的值:
      echo "A is:" $a
执行结果:A is: hello world

     如果是变量不用输出到终端,直接在sh文件内部调用,使用单括号即可:

       PRODUCT='real6410'

调用到的地方使用:

      mkdir -p out/target/product/$PRODUCT/obj/lib

(3)if 语句。"if"表达式如果条件为真,则执行then后的部分:
       if ....; then
       ....
       elif ....; then
       ....
       else
       ....
       fi
通常用" [ ] "来表示条件测试,注意这里的空格很重要,要确保方括号前后的空格。例如:
[ -f "somefile" ] :判断是否是一个文件
[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限
[ -n "$var" ] :判断$var变量是否有值
[ "$a" = "$b" ] :判断$a和$b是否相等

(4)循环。

while ...; do
   ....
done
只要测试表达式条件为真,则while循环将一直运行。关键字"break"用来跳出循环,而关键字”continue”则可以跳过一个循环的余下部分,直接跳到下一次循环中。

(5)函数功能

       在sh中可以定义某段类似于函数的集成语句段,在名字前使用function申明。方法如:

function real6410_prebuild()

{
      mkdir -p out/target/product/$PRODUCT/obj/lib
      mkdir -p out/target/product/$PRODUCT/system/lib
     cp vendor/realarm/real6410/*.so out/target/product/$PRODUCT/obj/lib
     cp vendor/realarm/real6410/*.so out/target/product/$PRODUCT/system/lib
}

之后,在其他地方直接使用real6410_prebuild就行了。

(6)sync

同步文件句柄, 刷新所有未写入硬盘的数据。

(7)实例,可执行脚本update的内容如下。

[cpp]
  1. #!/bin/bash 
  2. if [ "$1" = "" ]; then 
  3.     echo "Please input resolution," 
  4.     echo "Such as: qvga, wqvga, wvga, hvga" 
  5.     exit 
  6. fi 
  7. p=$1 
  8. ./tool/bmp_to_raw ./temp0.raw ./$p/"${p}_uboot".bmp 
  9. ./tool/bmp_to_raw ./temp1.raw ./$p/"${p}_battery00".bmp 
  10. ./tool/bmp_to_raw ./temp2.raw ./$p/"${p}_battery02".bmp 
  11. ./tool/bmp_to_raw ./temp3.raw ./$p/"${p}_battery04".bmp 
  12. ./tool/bmp_to_raw ./temp4.raw ./$p/"${p}_battery06".bmp 
  13. ./tool/bmp_to_raw ./temp5.raw ./$p/"${p}_battery08".bmp 
  14. ./tool/bmp_to_raw ./temp6.raw ./$p/"${p}_low_battery1".bmp 
  15. ./tool/bmp_to_raw ./temp7.raw ./$p/"${p}_low_battery2".bmp 
  16. ./tool/bmp_to_raw ./temp8.raw ./$p/"${p}_batteryfull".bmp 
  17. ./tool/bmp_to_raw ./temp9.raw ./$p/"${p}_charger_ov".bmp 
  18. ./tool/bmp_to_raw ./boot_logo ./$p/"${p}_kernel".bmp 
  19. ./tool/zpipe -l 9 ./"${p}.raw" temp0.raw temp1.raw temp2.raw temp3.raw temp4.raw temp5.raw temp6.raw temp7.raw temp8.raw temp9.raw 
  20. rm -rf ./temp0.raw ./temp1.raw ./temp2.raw ./temp3.raw ./temp4.raw ./temp5.raw ./temp6.raw ./temp7.raw ./temp8.raw ./temp9.raw ./bootlogo.raw 
  21. echo "conversion finished" 
#!/bin/bashif [ "$1" = "" ]; then	echo "Please input resolution,"	echo "Such as: qvga, wqvga, wvga, hvga"	exitfip=$1./tool/bmp_to_raw ./temp0.raw ./$p/"${p}_uboot".bmp./tool/bmp_to_raw ./temp1.raw ./$p/"${p}_battery00".bmp./tool/bmp_to_raw ./temp2.raw ./$p/"${p}_battery02".bmp./tool/bmp_to_raw ./temp3.raw ./$p/"${p}_battery04".bmp./tool/bmp_to_raw ./temp4.raw ./$p/"${p}_battery06".bmp./tool/bmp_to_raw ./temp5.raw ./$p/"${p}_battery08".bmp./tool/bmp_to_raw ./temp6.raw ./$p/"${p}_low_battery1".bmp./tool/bmp_to_raw ./temp7.raw ./$p/"${p}_low_battery2".bmp./tool/bmp_to_raw ./temp8.raw ./$p/"${p}_batteryfull".bmp./tool/bmp_to_raw ./temp9.raw ./$p/"${p}_charger_ov".bmp./tool/bmp_to_raw ./boot_logo ./$p/"${p}_kernel".bmp./tool/zpipe -l 9 ./"${p}.raw" temp0.raw temp1.raw temp2.raw temp3.raw temp4.raw temp5.raw temp6.raw temp7.raw temp8.raw temp9.rawrm -rf ./temp0.raw ./temp1.raw ./temp2.raw ./temp3.raw ./temp4.raw ./temp5.raw ./temp6.raw ./temp7.raw ./temp8.raw ./temp9.raw ./bootlogo.rawecho "conversion finished"

      说明:$1表示第一个参数;以上命令依次执行。

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

你可能感兴趣的文章
c++输入文件流ifstream用法详解
查看>>
c++输出文件流ofstream用法详解
查看>>
字符编码:ASCII,Unicode 和 UTF-8
查看>>
QT跨MinGW和MSVC两种编译器的解决办法
查看>>
firewalld的基本使用
查看>>
Linux下SVN客户端使用教程
查看>>
i2c-tools
查看>>
Linux分区方案
查看>>
nc 命令详解
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(五):OpenFeign请求结果处理及重试控制
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
OpenFeign学习(七):Spring Cloud OpenFeign的使用
查看>>
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>
Ribbon 学习(三):RestTemplate 请求负载流程解析
查看>>
深入理解HashMap
查看>>