zoukankan      html  css  js  c++  java
  • Batch pk Shell

     

    原文地址:WindowsBatch与LinuxShell比较[变量符号和关键字]

     

    一 简单实例
    1)batch file

    @echo off

    rem output helloworld
    ::  output helloworld
    Echo Hello World!

    小结:
    - batch file一般以bat或cmd为后缀。
    - 第一行为@echo off表示关闭运行时batch file本身输入,只输出运行的结果。
    - rem和::表示注释。

    2)shell file

    #!/bin/sh

    # output helloworld
    echo helloworld!

    小结:
    -shell file一般以sh,ksh,bash等结尾。
    -第一行为#!/bin/sh用来用那种shell解释程序来解释本shell脚本,因为shell有多种,常见的有sh,ksh,tsh,bash等。
    -#用来在shell中表示注释。
    -shell file执行前需要修改权限为可执行,例如:chmod a+x shellfile.sh。

    二 变量
    1)batch file

    Set Name = Value
    Set path = Name;%PATH%
    Echo %path%

    小结:
    -用set来定义变量,=两边不可以使用空格。
    -变量间用;隔开。
    -使用%%来使用变量的值。

    2) shell file

    Name=Value
    PATH=Name:$PATH
    Echo $PATH

    小结:
    -变量直接定义,且=两边不能有空格。
    -变量间用:隔开。
    -使用$来使用变量的值。

    三 特殊变量

    小结:
    -可以使用shift来使用超过10个变量。
    -windows的batchfiles中%~dp0%表示当前文件的目录。



    四 变量的特殊用法
    变量的替换:
    1)batch file

    %VariableName:ReplacementString=OriginalString%
    set a=belcome to CMD borld!
    set temp=%a:b=w%
    echo %temp%
    pause
    将显示 welcome to CMD world! 即用w替换了变量a中的b。

    2)shell file

    ${VAR/PATTERN/STRING} or ${VAR//PATTERN/STRING} 语法。
      第一种形式仅仅替换第一个匹配的项目,第二个用 STRING 替换所有匹配 PATTERN 的项目。


    变量求子串:
    1)batch file

    复制代码
    %VariableName:~StartPosition,Length%
    set a=superhero 
    set temp=%a:~0,-3% 
    echo %temp% 
    pause 
    将显示superh 即显示了变量a的第0位和第-3位中间包含的所有字符。
    复制代码

    2) shell file

    ${varname:offset:length} Purpose: Returning parts of a string (substrings or slices).
    STRING="thisisaverylongname" 
    echo ${STRING:6:5} 


    shell file中的其他的特殊用法:

    a. 变量=${参数-word}:如果设置了参数,则用参数的值置换变量的值,否则用word置换。即这种变量的值等于某一个参数的值,如果该参数没有设置,则变量就等于word的值。
    b. 变量=${参数=word}:如果设置了参数,则用参数的值置换变量的值,否则把变量设置成word,然后再用word替换参数的值。注意,位置参数不能用于这种方式,因为在Shell程序中不能为位置参数赋值。
    c. 变量=${参数?word}:如果设置了参数,则用参数的值置换变量的值,否则就显示word并从Shell中退出,如果省略了word,则显示标准信息。这种变量要求一定等于某一个参数的值。如果该参数没有设置,就显示一个信息,然后退出,因此这种方式常用于出错指示。
    d. 变量=${参数+word}:如果设置了参数,则用word置换变量,否则不进行置换。


    五 Call/start/source/sh
    1)batch file中call/start
    call, 父bat中的vars可以在子bat中访问,且子bat的修改可以返回到父bat中,或者如果子bat中新定义的vars也可以带回到父中。(因为最后子bat和父bat在执行时被合并为同一个bat)。
    Start,父bat中的vars可以在子bat中访问,但是子bat修改不会被反映到父bat中,且子中定义的变量不会被带到父中。(子bat和父bat是独立的进程)。

    2) shell file中source/sh/.
    Source同.命令,与batch file中的call相同,父shell中的vars可以在子shell中访问,且子shell的修改可以返回到父shell中,或者如果子shell中新定义的vars也可以带回到父中。(因为最后子shell和父shell在执行时被合并为同一个shell)。
    Sh,同batch file的start,但是有区别,父shell中的vars不能被在子中访问,且子中的修改不会被反映到父shell中,子中定义的变量不能被带到父中。如果将父中的vars使用export导入子中,则在子中可见,但是修改仍不能被带回父中。(子shell和父shell是独立的进程)。

    六 特殊符号

    七 错误代码
    1) batch file
    -errorlevel用来上次命令的返回值,如果为0表示成功。
    2) shell file
    -$?用来表示上次命令的返回值,如果为0表示成功。
    3)2> file 表示将错误重定向到file,2>&1 表示将错误输出重定向到与标准输出相同。0表示标准输入,1表示标准输入,2表示错误输出。

    八 表达式计算
    1)batch file

    复制代码
    SET /A variable = Expression
    set /a var=5+2 
    set /a var=55*34 
    set /a var=55/34
    set /a var=55%%34
    set /a var= (8+(9/3+7))*3
    但set /a vat=55.1*34是错误的,因为批处理不支持浮点运算。

    SET /A x = 1 
    SET /A y = 2 
    SET /A z = x + y + 3 
    ECHO z
    复制代码


    2)shell file

    复制代码
    a=2
    c=5
    let b=$a*$c
    echo $b

    $((i++))

    $((3 > 2)) 
    $(( (3 > 2) || (4 <= 1) )) 

    declare -i val3=12 val4=5
    declare -i result2
    result2=val3*val4
    echo $result2
    复制代码

    小结:
    Shell file中:
    1) 常用运算符号:
    ++ Increment by one (prefix and postfix) 
    — Decrement by one (prefix and postfix) 
    + Plus 
    - Minus 
    * Multiplication 
    / Division (with truncation) 
    % Remainder 
    ** Exponentiation[10] 
    << Bit-shift left 
    >> Bit-shift right 
    & Bitwise and 
    | Bitwise or 
    ~ Bitwise not 
    ! Logical not 
    ^ Bitwise exclusive or
    , Sequential evaluation
    2) 字符串比较:
    <                     Less than 
    >                     Greater than 
    <=                    Less than or equal to 
    >=                    Greater than or equal to 
    ==                    Equal to 
    !=                    Not equal to 
    &&                    Logical and 
    ||                    Logical or
    3) 整数比较:
    -lt               Less than
    -gt               Greater than
    -le               Less than or equal to
    -ge               Greater than or equal to
    -eq               Equal to
    -ne              Not equal to
    九 完!

  • 相关阅读:
    python自动化测试-使用第三方python库技术实现
    JMeter目录结构
    JMeter Http请求之content-type用法
    JMeter生成性能报表-Windows环境和Linux环境
    JMeter4.0 IF Controller
    c++ 初始化列表和构造函数初始化区别
    关于C++ 中 thread 的拷贝构造函数
    函数的参数类型 指针和指针的引用的区别
    window10 vs2013 SIFTGPU
    Qt使用双缓冲绘图时报错:pure virtual method called
  • 原文地址:https://www.cnblogs.com/frankcui/p/11598935.html
Copyright © 2011-2022 走看看