一、起源和平台
Shell 脚本
起源:
Shell 脚本起源于 Unix 操作系统。它是一种基于 Unix Shell(如 Bash、Sh、Ksh 等)的脚本语言,用于自动化系统管理任务和执行命令序列。
平台:
主要应用于 Unix、Linux 和类 Unix 系统。不同的 Unix/Linux 发行版可能使用不同的默认 Shell,但都支持 Shell 脚本的运行。例如,Linux 中的 Bash(Bourne - Again Shell)是非常常见的 Shell,许多系统管理脚本都是用 Bash 编写的。
PowerShell 脚本
起源:
PowerShell 由微软开发,首次发布于 2006 年。它是专为 Windows 系统设计的自动化和配置管理框架,基于.NET 框架构建。
平台:
主要运行在 Windows 操作系统上。不过,随着 PowerShell 的开源(PowerShell Core),它也可以在 Linux 和 macOS 等非 Windows 平台上运行,但在 Windows 环境中功能最为强大和完整。
二、语法举例
1.Shell 脚本
1.1条件语句:
条件语句通常使用if、elif、else结构,并且条件判断使用方括号[ ],例如:
if [ $a -gt $b ]; then
echo "$a is greater than $b"
fi
1.2循环语句:
常见的循环有for循环和while循环。例如,for循环的形式如下:
for i in 1 2 3 4 5; do
echo $i
done
2.PowerShell 脚本
2.1条件语句:
条件语句使用if
、elseif
、else
结构,但条件判断使用圆括号,例如:
if ($a -gt $b) {
Write - Host "$a is greater than $b"
}
2.2循环语句:
PowerShell 有for
、foreach
、while
、do - while
等循环结构。例如,foreach
循环如下:
$numbers = 1, 2, 3, 4, 5
foreach ($num in $numbers) {
Write - Host $num
}