Linux and Unix provides different flavors of command line interface. bash
is the most popular and use one. But we have alternatives too. ksh
or Korn Shell is one of them. Kornshell is mainly developed for shell based application development. ksh
provides better performance for shell script.
Linux和Unix提供了不同类型的命令行界面。 bash
是最受欢迎和使用的一种。 但是我们也有其他选择。 ksh
或Korn Shell就是其中之一。 Kornshell主要针对基于外壳的应用程序开发而开发。 ksh
为shell脚本提供了更好的性能。
安装Kornshell (Install Kornshell)
We can install Kornshell for most of the Linux, Unix and BSD operating systems. Here are some of them.
我们可以为大多数Linux,Unix和BSD操作系统安装Kornshell。 这里是其中的一些。
Fedora,CentOS,RedHat (Fedora, CentOS, RedHat)
$ yum install ksh
Ubuntu,Debian,Kali (Ubuntu, Debian, Kali)
$ apt install ksh

启动Kornshell (Start Kornshell)
Starting ksh
is very easy. We will just run ksh
command in the current shell like below.
启动ksh
非常容易。 我们将只在当前shell中运行ksh
命令,如下所示。
$ ksh
Kornshell脚本示例 (Kornshell Script Examples)
We can create scripts like in bash. We have to specify the ksh path on the system. In this case we use Ubuntu and the ksh
shell is locate at /usr/bin/ksh. alternative and more clean path is /bin/ksh . We can run this script from bash too because the interpreter line will change current shell to the Korn Shell.
我们可以在bash中创建脚本。 我们必须在系统上指定ksh路径。 在这种情况下,我们使用Ubuntu,而ksh
shell位于/ usr / bin / ksh。 另一种更干净的路径是/ bin / ksh。 我们也可以从bash运行此脚本,因为解释器行会将当前的shell更改为Korn Shell。
#!/bin/ksh
echo "This is ksh shell script"
变数 (Variables)
If you have used bash
for programming both scripting languages are very similar. Variables can be simply defined with the variable name and related value equation. In this example we will define integer variable named ÀGE which holds 33
.
如果您使用bash
进行编程,则两种脚本语言都非常相似。 可以使用变量名称和相关值方程式简单地定义变量。 在此示例中,我们将定义名为33
整数变量ÀGE。
AGE = 33
对于循环 (For Loop)
For loop is very similar to the bash
for loop we can use following example in order to loop though files in the current working directory.
For循环与bash
for循环非常相似,我们可以使用以下示例来循环浏览当前工作目录中的文件。
for file in $(ls)
do
print $file
done
功能定义 (Function Definition)
We can create functions by using function
keyword and the function name we want to use. In this example we will create function named myprint
. Function body is surrounded with curly braces.
我们可以使用function
关键字和要使用的函数名称来创建函数。 在此示例中,我们将创建名为myprint
函数。 功能主体被大括号包围。
function myprint
{
print "This is myprint function"
}