嵌入式linux对shell脚本的基本掌握

嵌入式技术

1335人已加入

描述


对于嵌入式linux开发者而言,shell脚本的要求基本上能看懂就可以。不用像运维人员那样写出非常强悍的shell脚本
shell:
在计算机科学中,Shell俗称壳(用来区别于核)
是指“提供使用者使用界面”的软件(命令解析器)
它类似于DOS下的command和后来的cmd.exe
它接收用户命令,然后调用相应的应用程序
同时它又是一种程序设计语言
作为命令语言
它交互式解释和执行用户输入的命令或者自动地解释和执行预先设定好的一连串的命令
作为程序设计语言,它定义了各种变量和参数
并提供了许多在高级语言中才具有的控制结构,包括循环和分支

[cpp] view plain copy
 
  • bash / sh / ksh / csh(Unix/linux 系统)  
  • Bourne shell (包括 sh,ksh,and bash)  
  • Bourne shell ( sh)  
  • Korn shell ( ksh)  
  • Bourne Again shell ( bash)  
  • POSIX shell ( sh)  
  • C shell (包括 csh and tcsh)  
  • C shell ( csh)  
  • TENEX/TOPS C shell ( tcsh)  

  • shell脚本:
    windows 批处理脚本.bat   cmd
    linux   shell脚本 .sh/空 shell
    Shell Script,Shell脚本与Windows/Dos下的批处理相似
    也就是用各类命令预先放入到一个文件中
    方便一次性执行的一个程序文件
    主要是方便管理员进行设置或者管理用的
    但是它比Windows下的批处理更强大
    比用其他编程程序编辑的程序效率更高
    它使用了Linux/Unix下的命令
    下面就列举一些shell基本用法和基本语句
    sh格式要求:
    sh文件格式开头:#!/bin/sh  #!特殊格式要求指定用哪种shell
    在bin目录下执行
    [cpp] view plain copy
     
  • [root@localhost bin]# ls *sh*  
  • bash  cgsnapshot  csh  dash  sh  tcsh  
  • 可以查看系统所具有的解析器类别
    shell脚本可以定义变量
    格式:变量名=变量内容
    引用:$+变量名

    例如下面的例子:
    [cpp] view plain copy
     
  • #!/bin/sh  
  • ZC=123456  
  • echo $ZC  

  • 运行sh
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh   
  • 123456  

  • 消除变量的含义将变量用单引号括住即可,看下面的例子:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • ZC=shell  
  • LJJ=haha  
  • echo '$ZC+$LJJ'  

  • 执行sh
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh   
  • $ZC+$LJJ  

  • 删除变量使用unset+变量名  看一下下面这个例子:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • ZC=shell  
  • LJJ=haha  
  • echo $ZC+$LJJ  
  • unset ZC  
  • unset LJJ  
  • echo $ZC+$LJJ  


  • 执行sh
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh   
  • shell+haha  
  • +  
  • 打印结果中第二次打印变量已经被释放掉,没有打印


    看下面的例子:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • NUM1=3  
  • NUM2=5  
  • echo $(($NUM1+$NUM2))  
  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh   
  • 8  

  • 读取标准输入命令read linux中标准输入默认为键盘输入,代码如下:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • echo "请输入第一个数:"  
  • read NUM1  
  • echo "请输入第二个数:"  
  • read NUM2  
  • echo "两数相加的结果:"  
  • echo $(($NUM1+$NUM2))  
  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh   
  • 请输入第一个数:  
  • 23  
  • 请输入第二个数:  
  • 45  
  • 两数相加的结果:  
  • 68  

  • 加法还可以这样写:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • echo $(($1+$2))  


  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh 23 65  
  • 88  

  • 这么神奇!!那么这个神奇的$究竟是怎样的神通广大的呢?看下面
    [cpp] view plain copy
     
  • $1,$2…$n 传入的参数, $0 表示 shell 程序名称 每一项相当于 main 函数中 argv[i].  
  • $#传递到脚本的参数列表,或表示参数个数 等价于 main 函数中的 argc-1.  
  • $@传入脚本的全部参数 argv[1] ---- argv[n-1].  
  • $* 显示脚本全部参数  
  • $? 前个命令执行情况 0 成功 1 失败  
  • $$ 脚本运行的当前进程号  
  • $! 运行脚本最后一个命令  

  • 综合演示一下:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • echo $(($1+$2))  
  • echo "脚本命令执行情况:" $?  
  • echo “"传入参数为:"$@  
  • echo “"脚本执行进程号:"$$  

  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh 56 87  
  • 143  

  • 脚本命令执行情况: 0
    “传入参数为:56 87
    “脚本执行进程号:28632


    判断命令 test 用法如下:
    [cpp] view plain copy
     
  • test 表达式 1 –a 表达式 2 两个表达式都为真  
  • test 表达式 1 –o 表达式 2 两个表达式有一个为真  
  • test –n 字符串 字符串的长度非零  
  • –z 字符串长度为零 ==字符串相等 != 字符串不等  
  • test 整数 1 –eq 整数 2 整数相等  
  • -ge 大于等于 –gt 大于 –le 小于等于 –lt 小于 –ne 不等于  
  • test File1 –ef File2 两个文件具有同样的设备号和 i 结点号  
  • test File1 –nt File2 文件 1 比文件 2 新  
  • test File1 –ot File2 文件 1 比文件 2 旧  
  • test –b File 文件存在并且是块设备文件  
  • test –c File 文件存在并且是字符设备文件  
  • test –d File 文件存在并且是目录  
  • test –e File 文件存在  
  • test –f File 文件存在并且是普通文件  
  • test –g File 文件存在并且是设置了组 ID  
  • test –G File 文件存在并且属于有效组 ID  
  • test –h File 文件存在并且是一个符号链接(同-L)  
  • test –k File 文件存在并且设置了 sticky 位  
  • test –L File 文件存在并且是一个符号链接(同-h)  
  • test –o File 文件存在并且属于有效用户 ID  
  • test –p File 文件存在并且是一个命名管道  
  • test –r File 文件存在并且可读  
  • test –s File 文件存在并且是一个套接字  
  • test –t File 文件描述符是在一个终端打开的  
  • test –u File 文件存在并且设置了它的 set-user-id 位  
  • test –w File 文件存在并且可写  
  • test –x File 文件存在并且可执行  

  • 举一个小例子简单说明一下简单的用法:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • test $1 -eq $2  
  • echo $?  


  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh 1 3  
  • 1  
  • [root@localhost shell]# ./test.sh 1 1  
  • 0  
  • 通常判断语句test 与 if语句配合使用


    shell脚本中的if语句结构如下
    if [ 条件 ]
    then
      [相关任务1]
    else
      [相关任务2]
    fi
    如果条件为真执行then后面语句,如果条件为假执行else后面的语句,不要忘记if语句结束时加上fi
    简单的小例子示意一下if用法:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • if test $1 -eq $2   
  • then  
  • echo "两个数相等"  
  • else  
  • echo "两个数不相等"  
  • fi  

  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh 1 1  
  • 两个数相等  
  • [root@localhost shell]# ./test.sh 23 5  
  • 两个数不相等  

  • shell中的while语句结构如下
    while(条件)
    do
      执行指令
    done
    举一个小例子说明一下while的用法:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • c=0;  
  • while [ $c -lt 3 ]  
  • do  
  • echo "Value c is $c"  
  • c=`expr $c + 1`  
  • done  


  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh   
  • Value c is 0  
  • Value c is 1  
  • Value c is 2  


  • while条件为c<3 执行循环条件为 c+1;
    在shell脚本中执行算术运算通常用expr空格+运算体  做后用TAB键上方的点括起来使用。在进行乘法运算时需要用\屏蔽其特殊含义
    用于计算的命令还有let 用法如下:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • c=100  
  • let c=$c+1  
  • echo $c  


  • shell中的for语句结构如下:
    for 变量 in 列表
    do
      语句···
    done
    简单的小例子说明其用法
    [cpp] view plain copy
     
  • #!/bin/bash  
  • for int in 1 2 3 4  
  • do  
  •   echo $int  
  • done  

  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh   
  • 1  
  • 2  
  • 3  
  • 4  


  • case分支语句相当于c语言中的switch case语句,case结构如下:
    case 变量 in
    “变量类型A”)执行语句;;
    “变量类型B”)执行语句;;
    “变量类型C”)执行语句;;
    `````
    esac
    下面写一个小例程说明简单用法:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • grade=$1  
  • case $grade in  
  • "A") echo "Very Good!";;  
  • "B") echo "Good!";;  
  • "C") echo "Come On!";;  
  • *)   
  • echo "You Must Try!"  
  • echo "Sorry!";;  
  • esac  


  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh A  
  • Very Good!  
  • [root@localhost shell]# ./test.sh B  
  • Good!  
  • [root@localhost shell]# ./test.sh C  
  • Come On!  
  • [root@localhost shell]# ./test.sh F  
  • You Must Try!  
  • Sorry!  

  • shell脚本中数组定义如下
    变量名 =(元素1 元素2······元素n)
    下面的小例程简单说明一下数组的用法:
    [cpp] view plain copy
     
  • #!/bin/bash  
  • int=(1 2 3 4 5)  
  • s=${int[2]}  
  • echo $s  

  • 执行sh:
    [cpp] view plain copy
     
  • [root@localhost shell]# ./test.sh   
  • 3  

  • 基本的shell指示就介绍到这里

    打开APP阅读更多精彩内容
    声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
    • 相关推荐

    全部0条评论

    快来发表一下你的评论吧 !

    ×
    20
    完善资料,
    赚取积分