Korn Shell在许多操作系统上都是可用的,这些系统包括IBM AIX and HP-UX。Korn Shell努力支持Portable Operating System Interface for UNIX(POSIX)Shell语言的标准。
我们来看一下在Korn Shell中统计可执行文件数目脚本的例子:
代码2:在Korn Shell中统计可执行文件的数目
- #!/usr/bin/ksh
- # find all executables
-
- count=0
-
- # Test arguments
- if [ $# -ne 1 ] ; then
- echo "Usage is $0 <dir>"
- exit 1
- fi
-
- # Ensure argument is a directory
- if [ ! -d "$1" ] ; then
- echo "$1 is not a directory."
- exit 1
- fi
-
- # Iterate the directory, emit executable files
- for filename in "$1"/*
- do
- if [ -x "$filename" ] ; then
- echo $filename
- count=$((count+1))
- fi
- done
-
- echo
- echo "$count executable files found."
-
- exit 0
|
Bourne-Again Shell
Bourne-Again Shell,即bash,是一个开源的GNU项目,旨在替换Bourne Shell。Bourne-Again Shell由Brian Fox开发,现在已经成为最流行的Shell之一,被广泛应用在Linux、Darwin、Windows和Cygwin之上。
除了支持脚本的向后兼容性,bash还吸收了Korn Shell和C Shell的一些特性。例如,命令历史记录,命令行编辑,目录堆栈,很多实用的环境变量,命令行自动完成,等等。
Bash继续发展了一些新的特性,如支持正则表达式和关联数组。
虽然一些特性是bash独有的,但我们仍然可以编写与其他脚本语言相兼容的脚本。下面是在bash中统计可执行文件数目的脚本示例。
代码3:在Bourne-Again Shell中统计可执行文件的数目
- #!/bin/bash
- # find all executables
-
- count=0
-
- # Test arguments
- if [ $# -ne 1 ] ; then
- echo "Usage is $0 <dir>"
- exit 1
- fi
-
- # Ensure argument is a directory
- if [ ! -d "$1" ] ; then
- echo "$1 is not a directory."
- exit 1
- fi
-
- # Iterate the directory, emit executable files
- for filename in "$1"/*
- do
- if [ -x "$filename" ] ; then
- echo $filename
- count=$((count+1))
- fi
- done
-
- echo
- echo "$count executable files found."
-
- exit 0
|
这些Shell之间的一个关键区别是它们使用了不同的授权。Bash是一个GNU项目,遵循GPL授权,而C Shell则遵循了BSD许可,Korn Shell则遵循了通用公共许可证。
早期Shell的许多理念和接口在35年之后依然保持不变,这对其作者是一个巨大的肯定。任何一个行业都在不断重塑自我,Shell也在发生着历史的变迁。尽管许多新的Shell被开发出来,但Bourne Shell及其后继者依然是现在的首选。