zoukankan      html  css  js  c++  java
  • UNIX shell 学习笔记 一 : 几个shell的规则语法对比

    1. 查看系统有哪些可用的shell

      cat /etc/shell

    2. 每种shell都有一个特殊内置变量来存上一条命令的退出状态,例:

      C/TC shell  $status

      % cp fx fy

      % echo $status   # 显示cp的退出状态,0成功,1失败

      Bourne,Bash,Korn Shells  $?

      $ cp fx fy

      $ echo $?        # 显示cp的退出状态,0成功,1失败

    C shell, TC shell编程语法与结构

      1. shbang行  

        是脚本第一行,通知内核使用哪种shell解释脚本,如

    C /TCshell korn shell Bourne shell bash
    #!/bin/csh  #!/bin/ksh #!/bin/sh #!/bin/bash

      2. 注释   符号# 注释其后的一行

      3. 通配符

        特殊字符,被称作是shell的元字符或通配符,如*、?、[]用于文件名扩展, <、>、>>、<&、|用于重定向和管道,如果要直接使用这些字符必须用反斜杠或引号进行引用,如

          echo "how old are you?"      # C, TC , Bourne, korn, bash shell

          echo ok !          #C shell, TC shell

      4. 显示输出  echo命令,korn还提供了内置命令print

      5. 局部变量

    C /TCshell korn shell Bourne shell bash
    set variable = value    

    variable = value 

    typeset   variable=value

    variable=value

    variable=value

    declare var=val

      

      6. 全局变量

    C/TC shell korn shell Bourne shell bash
    setenv VARIABLE value     export VARIABLE =value  

    VAR=value   

    export VAR   

    export VARIABLE =value

    declare -x VAR=value

      

      7. 提出变量值

        用符号$, 如 $variable

      8. 读取用户输入

      CTC shell BourneBash shell korn
    符号或命令 $<

    read命令

      从用户输入中读取一行并将它赋给该命令右侧

    的一个或多个变量  

    ex echo "input number .. "

    set num = $<

    说明:读取用户输入并给变量赋值

     echo "what is your name?"

     read name

     read n1 n2

    read name?"your name?"

    print -n "your name?"

    read name

    read n1 n2

       

      9. 参数

      CTC shell BourneBashkorn shell
    符号或命令 scriptname arg1 arg2      

    scriptname arg1 arg2

    ex

    $1 表示第一个参数arg1

    $* 表示所有参数

    $argv[1]第一个参数

    $argv[*]所有参数

    $#argv 表示参数个数。

    $1 第一个参数 

    $* 所有位置参数

    $# 参数个数 

      

      10. 数组

      C,TC shell Bourne shell
    define

    数组是一系列的词表,由()括起来,空格隔开,下标索引从1开始,

    内置命令shift将数组中左边第一个词移开   

    set内置命令后跟一系列词,每个词可通过位置来访问,

    最多允许使用9个位置。下标索引从1开始,内置命令

    shift将数组中左边第一个词移开   

    ex

    set word_list = (a b c)

    shift word_list   # remove 'a' from word_list

    echo $word_list[1]     # display 'b'

    echo $word_list or echo $word_list[*]   # display all elements of word_list

    set a b c

    echo $1 #显示第一个参数 'a'

    shift  # remove 'a' from word_list

    $* 所有位置参数

         korn shell:
      位置参数创建的数组元素下标从1开始,set -A创建的数组元素下标从0开始,如
        set apples pears peaches      #位置参数
        print $1 $2 $3   # $1 is apples
        set -A arr w1 w2 w3 ...    #数组
        print ${arr[0]}    #打印w1
      Bash:
      与korn shell类似,如
        set apples pears peaches      #位置参数
        print $1 $2 $3   # $1 is apples
        declare -a arr=( w1 w2 w3 ...  )  #数组
         echo ${arr[0]}    #打印w1

      11. 命令替换

        反引号`可以将其包含的字符解释成命令,如 

          set now = "today is `date`"   # date将作为命令执行并将结果插入到原处

        kornash shell 还除了可用反引号替换命令,还支持另一种语法,如

          today = " today is `date`"

          today = "today is $(date)"

      12. 算术运算

      C,TC shell Bourne shell
    define

    保存算术运算结果的变量必须以一个@符号加一个空格开头

    不支持算术运算,通过linux/unix命令计算

    ex

    @ nvalue = 4+1

    n=`expr 5+5`

    echo $n

      

      korn shell:

        typeset -i var   #声明整数

        num=5+5      #声明整数后可进行计算

        ((m = 4+4))    #(())语法表示算术运算,即let命令

      bash shell:

        typeset -i var   #声明整数,与ksh兼容

        declare -i var

        num=5+5      #声明整数后可进行计算

        ((m = 4+4))    #(())语法表示算术运算,即let命令

      13. 运算符

      C/TC /korn/bash shell Bourne shell  
    等式运算符

    ==

    (korn: '=' for str,'=='for numric)

    =字符串

    -eq 数字

    等式判断
      !=

    != 字符串

    -ne 数字

    不等
    关系运算符 > -gt 大于
      >= -ge 大于等于
      < -lt 小于
      <= -le 小于等于
    逻辑运算符 && -a
      || -o
      ! !

      

    14. 条件运算

      C/TC shell:

    #C/TC shell
    if (expr) then
        //todo
    endif
    ----------------------------------------------------------------------------------------------
    if (expr) then
        //todo
    else if (expr) then
        //todo
    else
        //todo
    endif
    -------------------------------------------------------------------------------------------------
    switch ("$color")
      case blue:
        echo $color is blue
        breaksw
      case green:
        echo $color is green
        breaksw
      default:
        echo no color
    endsw
    View Code

      Bourne shell:

    #Bourne shell
    if expr    # expr form : command, [expression]
    then
        //todo
    fi
    ---------------------------------------------------------------------------
    if expr    # expr form : command, [expression]
    then
        //todo
    elif expr
    then
        //todo
    else
        //todo
    fi
    ---------------------------------------------------------------------------
    case "$color" in
      blue)
        echo $color is blue
        ;;
      green)
        echo $color is green
        ;;
      *) echo no color #default case
    esac
    View Code

      kornash shell:

    kornash 
    if   expr   # expr :command, [[string expr]], ((numeric expr))  
    then               
        // to do            
    fi          
    ----------------------------------------------------------------------
    if exp                
    then
        //to do
    elif exp
    then
        //to do
    else
       //do do
    fi
    -----------------------------------------------------------------------
     case usage is the same as bourne shell
    View Code

    15. 循环

      C/TC shell :

      while后跟一个用圆括号括起来的表达式,一个语句段,以end结束. foreach后跟一个变量,一个用圆括号括起来的词表,一个语句段,最后以关键字end结束。foreach遍历词表,对每个词处理后将其移开,所有词被移开后,循环结束。

    #C/TC shell
    while (exp)
        //todo
    end
    ------------------------------------------
    foreach color(red green blue)
        echo $color
    end
    View Code

      Bourne shell:

    #Bourne shell     while, until, for
    while exp   #exp : command, [expr]
    do
        //todo
    done
    -------------------------------------------------
    until exp  #exp : command, [expr]
    do
        //todo
    done
    -------------------------------------------------
    for
    var in a b c
    do
        //todo
    done
    View Code

      kornash shell:

      while, until, for , select 4个循环,前三个与前面的一样,就不说了。select循环提供一条提示信息和多个选项的菜单,用户从中选择一项,这个输入将被存在内置的特定变量REPLY中。

    kornash shell:
    while exp # exp 可选形式 cmd, [[str expr]], ((num expr))
    do
        //todo
    done
    
    until exp  #exp 可选形式 cmd, [[str expr]], ((num expr))
    do
        //todo
    done
    
    for name in tom dic harry  #traverse list and remove the first item
    do
        //todo
    done
    
    select var in wordlist
    do
        //todo
    done
    View Code

    16. 函数定义

      korn shell:

    #korn shell
    func() {                #from bourne shell
        //todo
    }
    ---------------------------------------
    function func {       #korn version
        //todo
        echo current directory is  `pwd`
    }

    17. 文件测试  

    C/TC shell

      Bourne shell

     Kornshell

     

    -r 当前用户可以读该文件

    -w 当前用户可写该文件

    -x 当前用户可执行该文件

    -e 文件存在

    -o 该文件属于当前用户

    -z 该文件长度为0

    -d 该文件是一个目录  

    -f 该文件是一个普通文件

    -r 当前用户可以读该文件

    -w 当前用户可写该文件

    -x 当前用户可执行该文件

    -s 该文件大小非0

    -d 该文件是一个目录  

    -f 该文件是一个普通文件

    -d 该文件是一个目录

    -r 当前用户可以读该文件

    -a 该文件存在且不是目录

    -s 该文件大小非0

    -w 当前用户可写该文件

    -x 当前用户可执行该文件

  • 相关阅读:
    oracle手工生成AWR报告方法
    oracle init.ora常用配置详解
    Statspack的使用
    控制用户的访问之权限、角色【weber出品必属精品】
    初识数据字典【weber出品必属精品】
    EMCA常用命令 【weber整理必出精品】
    全世界最详细的图形化VMware中linux环境下oracle安装(三)【weber出品必属精品】
    vi 快捷键【转】【weber整理必出精品】
    数据库对象(视图,序列,索引,同义词)【weber出品必属精品】
    解决linux下oracle进入sqlplus环境中后退键显示^H、上下键无效与ctrl+l无法清屏等问题【weber出品必属精品】
  • 原文地址:https://www.cnblogs.com/lovemo1314/p/3496051.html
Copyright © 2011-2022 走看看