WindowsBatch与LinuxShell比较[变量符号和关键字]
1)batch file






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




-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来定义变量,=两边不可以使用空格。
-变量间用;隔开。
-使用%%来使用变量的值。
2) shell file




小结:
-变量直接定义,且=两边不能有空格。
-变量间用:隔开。
-使用$来使用变量的值。
三 特殊变量
小结:
-可以使用shift来使用超过10个变量。
-windows的batchfiles中%~dp0%表示当前文件的目录。
四 变量的特殊用法
变量的替换:
1)batch file








第一种形式仅仅替换第一个匹配的项目,第二个用 STRING 替换所有匹配 PATTERN 的项目。

变量求子串:
1)batch file











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





五 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












2)shell file














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
九 完!