Batch pk Shell - WindowsBatch与LinuxShell比较 [变量符号和关键字]

 

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

 

一 简单实例
1)batch fileshell

@echo off

rem output helloworld
::  output helloworld
Echo Hello World!

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

2)shell filewindows

#!/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 filebash

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

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

2) shell filepost

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

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

三 特殊变量

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



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

%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 filespa

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


变量求子串:
1)batch file.net

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

2) shell file3d

${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 evaluation2) 字符串比较:<                     Less than >                     Greater than <=                    Less than or equal to >=                    Greater than or equal to ==                    Equal to !=                    Not equal to &&                    Logical and ||                    Logical or3) 整数比较:-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九 完!

相关文章
相关标签/搜索