原文地址:WindowsBatch与LinuxShell比较[变量符号和关键字]html
一 简单实例
1)batch fileshell
小结:
- batch file通常以bat或cmd为后缀。
- 第一行为@echo off表示关闭运行时batch file自己输入,只输出运行的结果。
- rem和::表示注释。
2)shell filewindows
小结:
-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来定义变量,=两边不能够使用空格。
-变量间用;隔开。
-使用%%来使用变量的值。
2) shell filepost
小结:
-变量直接定义,且=两边不能有空格。
-变量间用:隔开。
-使用$来使用变量的值。
三 特殊变量
小结:
-能够使用shift来使用超过10个变量。
-windows的batchfiles中%~dp0%表示当前文件的目录。this
四 变量的特殊用法
变量的替换:
1)batch filelua
2)shell filespa
变量求子串:
1)batch file.net
2) shell file3d
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 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九 完!