Scala Shell #! 惊叹号井号与!#

本文探讨了Shell脚本中的#!及!#符号的作用,并详细解释了如何利用这些符号结合Scala执行脚本。此外,还介绍了exec命令在脚本执行过程中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://www.winseliu.com/blog/2014/09/03/linux-shell-shebang-tanjinghao/

工作中主要是写java代码,shell也只是用于交互性操作,写脚本的次数比较少。对于 #! 井号叹号仅仅是教条式的添加在脚本开头,并且基本上都是#!/bin/sh 。

今天在看scala官方的入门教程尽然发现!# 的写法,很是困惑,Google查询也不知道怎么描述关键字,一般搜索引擎都把这些操作符过滤掉了的。

#!/bin/sh
exec scala "$0" "$@"
!#
object HelloWorld extends App {
println("Hello, world!")
}
HelloWorld.main(args)

首先了解下#! 作用:如果#! 在脚本的最开始,脚本程序会把第一行的剩余部分当做解析器指令;使用当前的解析器来执行程序,同时把当前脚本的路径作为参数传递给解析器。

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark (that is, “#!”) at the beginning of a script.

Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script’s initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.

如果把!# 去掉,再执行上面的脚本则会报错: error: script file does not close its header with !# or ::!#,查寻一番后,这原来是Scala的脚本功能的内部处理。通过SourceFile.scala关键字搜索到了 该文列出了具体的位置,还有 A Scala shell script example和我有同样疑问。

可以在《Programing in Scala – A comprehensive step-by-step guide》一书的附录A中 Scala scripts on Unix and Windows 查找到相应的描述:把 #!和 !#之间的内容忽略掉了。

语法糖的疑惑解决了,针对上面的脚本还有个问题:exec执行完了,下面的内容不执行了?在exec命令的前面打上调试语句,也只输出了sh start

winse@Lenovo-PC ~
$ cat script.scala
#!/bin/sh
echo 'sh start'
exec scala "$0" "$@"
echo 'sh end'
!#
object HelloWorld extends App {
print("hello world")
}

HelloWorld.main(args)

winse@Lenovo-PC ~
$ sh script.scala
sh start
hello world

exec 使用 exec 方式运行script时, 它和 source 一样, 也是让 script 在当前process内执行, 但是 process 内的原代码剩下部分将被终止. 同样, process 内的环境随script 改变而改变.

所以,整个脚本流程就是:执行shell,调用exec来调用scala的解释器执行整个脚本内容,而解释器会过滤掉 #!和 !#之间内容,执行完后,exec退出脚本,实现scala脚本执行的功能。这样折中的使用方式,应该是为了处理 参数传递*的问题!

参考

–END



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值