Linux 英文解释为 Linux is not Unix。
Linux 内核最初只是由芬兰人林纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的。
Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 UNIX 的多用户、多任务、支持多线程和多 CPU 的操作系统。
Linux 能运行主要的 UNIX 工具软件、应用程序和网络协议。它支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。
什么是 Shell?
Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。
Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。
Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。
Shell 脚本(shell script),是一种为 shell 编写的脚本程序。
Shell 编程跟 JavaScript、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。
Linux 的 Shell 种类众多,常见的有:
Bourne Shell(/usr/bin/sh或/bin/sh)
Bourne Again Shell(/bin/bash)
C Shell(/usr/bin/csh)
K Shell(/usr/bin/ksh)
Shell for Root(/sbin/sh)
……
本文关注的是 Bash,也就是 Bourne Again Shell,由于易用和免费,Bash 在日常工作中被广泛使用。同时,Bash 也是大多数Linux 系统默认的 Shell。
在一般情况下,人们并不区分 Bourne Shell 和 Bourne Again Shell,所以,像 #!/bin/sh,它同样也可以改为 #!/bin/bash。
#! 告诉系统其后路径所指定的程序即是解释此脚本文件的 Shell 程序。
我发现使用 Shell 脚本还是没有使用 Kotlin Script 来得强大自由。所以,我在这里讲 Shell 的同时,给出了 Kotlin 脚本的实例。
Hello World
shell 脚本
echo "Hello World!"
运行:
$ ./hello_world.sh
Hello World!
kotlin 脚本
// hello world
println("Hello World!")
运行:
$ kotlinc -script KtsDemo.kts
Hello World!
使用变量
shell 脚本
# 变量:注意 shell 中的变量等于号前后不要有空格
a="I am cat"
echo $a
kotlin 脚本
// variable
val a = "I am cat"
println(a)
数组与循环
shell 脚本
# 数组 & for loop
nums=(1 2 3 4 5 6 7 8 9 10)
declare -i sum=0
for i in ${nums[*]} ; do
((sum=sum+i))
done
echo "sum is $sum"
kotlin 脚本
// for loop
var sum = 0
val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
for (i in nums) {
sum += i
}
println("sum is $sum")
条件判断
shell 脚本
# 条件判断 condition
x=1
y=1
if ((x == y))
then
echo "x=y"
elif ((x>y))
then
echo "x>y"
else
echo "x<y"
fi
kotlin 脚本
// condition
val x = 1
val y = 1
if (x == y) {
println("x=y")
} else if (x > y) {
println("x>y")
} else {
println("x<y")
}
文件 IO 操作
shell 脚本
# io
curPath=$(pwd)
echo $curPath
kotlin 脚本
// io
val f = File(".")
val path = f.absolutePath
println("path:$path")
定义函数
shell 脚本
# function
function sum(){
v1=$1
v2=$2
v=$((v1+v2))
echo $v
}
sum 1 2
s=$(sum 1 2)
echo "s is $s"
kotlin 脚本
// fun
fun sum(a: Int, b: Int): Int {
return a + b
}
val c = sum(1, 2)
println("c is $c")
kscript - Having fun with Kotlin scripting
Enhanced scripting support for Kotlin on *nix-based systems.
Kotlin has some built-in support for scripting already but it is not yet feature-rich enough to be a viable alternative in the shell.
In particular this wrapper around kotlinc
adds
- Compiled script caching (using md5 checksums)
- Dependency declarations using gradle-style resource locators and automatic dependency resolution with jcabi-aether
- More options to provide scripts including interpreter mode, reading from stdin, local files or URLs
- Embedded configuration for Kotlin runtime options
- Support library to ease the writing of Kotlin scriptlets
- Deploy scripts as stand-alone binaries
Taken all these features together, kscript
provides an easy-to-use, very flexible, and almost zero-overhead solution to write self-contained mini-applications with Kotlin.
kscript
presentation from KotlinConf2017!
- Installation
- Script Input Modes
- Script Configuration
- Text Processing Mode
- Treat yourself a REPL with
--interactive
- Boostrap IDEA from a
kscript
- let
- Deploy scripts as standalone binaries
- Embed kscript installer within your script
- FAQ
- Support
- How to contribute?
- Acknowledgements
Installation
To use kscript
just Kotlin and Maven are required. To install Kotlin we recommend sdkman:
curl -s "https://get.sdkman.io" | bash # install sdkman
source ~/.bash_profile # add sdkman to PATH
sdk install kotlin # install Kotlin
Once Kotlin is ready, you can install kscript
with
sdk install kscript
To test your installation simply run
kscript --help
This will check and inform about udpates. To update kscript
simply install it again as described above.
Installation without sdkman
If you have Kotlin and Maven already and you would like to install the latest kscript
release without using sdkman
you can do so by unzipping the latest binary release. Don’t forget to update your $PATH
accordingly.
Installation with Homebrew
On MacOS you can install kscript
also with Homebrew
brew install holgerbrandl/tap/kscript
To upgrade to latest version
brew update
brew upgrade holgerbrandl/tap/kscript
Build it yourself
To build kscript
yourself, simply clone the repo and do
./gradlew assemble
## Run kscript from output dir
./build/libs/kscript
Script Input Modes
The main mode of operation is kscript <script>
.The <script>
can be a Kotlin *.kts
script file , a script URL, -
for stdin, a process substitution file handle, a *.kt
source file with a main method, or some kotlin code.
Interpreter Usage
To use kscript
as interpreter for a script just point to it in the shebang line of your Kotlin scripts:
println("Hello from Kotlin!")
for (arg in args) {
println("arg: $arg")
}
更多内容参考:https://www.kotlinresources.com/library/kscript/
Kotlin 开发者社区
国内第一Kotlin 开发者社区公众号,主要分享交流:编程语言、Spring Boot、Android、React.js/Node.js、函数式编程、编程思想、系统架构设计、领域建模等相关主题。