You will be surprised to know that you can also create Scala script and execute it on Windows OS(as a bat script) as well as on Linux/Unix OS(as a shell script) without any compilation. Scripting with a very high level language makes things very easier for the programmer who has a little bit experience in Scripting. If you don’t know any scripting on Windows or Linux, No problem….!!! Scala script will enable you to write your scripts in programming language.
Let’s explore more details about it…..!!!!!
Scala Script on Windows
Windows user can create some of the useful .bat
scripts using Scala. But before you proceed further just make sure that Scala is installed on your machine and %SCALA_HOME%/bin
path is appended to your PATH
variable.
Quick Scala Installation Steps on Windows
- Download
.msi
installer file from Scala Downloads page - Install Scala by double clicking
.msi
installer- This will install Scala on
C:\Program Files (x86)\scala
ORC:\Program Files\scala
path
- This will install Scala on
OR
- You can download
scala-2.11.7.tgz
file from Scala Downloads page and extract it toC:\Program Files
path
After one of the above steps,
- Go to My Comupter > Properties > Advance System Settings > Environment Variables
- Create
SCALA_HOME
variable with the path where Scala is extracted- In our case it will be
C:\Program Files\scala
- In our case it will be
- Update/Create
PATH
variable and append;%SCALA_HOME%/bin;
to the value
That’s it you are done with the installation. You can check your Scala version by opening command prompt and typing below command
1 | scala -version |
Now let’s create our scala script for printing simple message,
message.bat
1 2 3 4 5 6 | ::#! @echo off call scala %0 %* goto :eof ::!# println("Hello, Welcome to Scala Script.....!!!!!") |
When you write any .bat
scala script, just make sure that you write first 5 lines of above script first and than start your scripting.
Executing message.bat
file from windows command prompt you will get “Hello, Welcome to Scala Script…..!!!!!” as an output
In above 5 lines, call scala command is responsible for executing your scala script where as %0
and %*
are parameters
- Parameter “%0” replaces itself with the script file name
- Parameter “%*” passes other subsequent parameters to your script
Let’s understand this by an example,
Problem: Create a script file using Scala which accepts name and age as a parameter and print them in a message.
Solution:
message.bat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ::#! @echo off call scala %0 %* goto :eof ::!# object Message { def main(args: Array[String]) { println("Hello, " + args(0) + ".....!!!!!") println("Welcome to Scala Script.....!!!!!") println("You are " + args(1)+ " years old." ) } } Message.main(args) |
Place above script in C:/
drive and execute as below,
1 2 3 4 | C:\>message.bat Varun 29 Hello, Varun.....!!!!! Welcome to Scala Script.....!!!!! You are 29 years old. |
Quite Simple, right?????
Scala Script on Linux/Unix
Same as Windows in Linux also you have to make sure that your Scala installation and PATH
variables are proper. You can refer to my post 4 Steps to Setup Scala On CentOS for the same.
Similar to .bat
files you have to write below lines st the beginning of your shell scripts.
1 2 3 | #!/bin/sh exec scala "$0" "$@" !# |
Command exec will work same as call command in Windows and here also we have two parameters "$0"
and "$@"
- Parameter “$0” replaces itself with the script file name
- Parameter “$@” passes other subsequent parameters to your script
Let’s have the same example in shell script,
message.sh
1 2 3 4 5 6 7 8 9 10 11 12 | #!/bin/sh exec scala "$0" "$@" !# object Message { def main(args: Array[String]) { println("Hello, " + args(0) + ".....!!!!!") println("Welcome to Scala Script.....!!!!!") println("You are " + args(1)+ " years old." ) } } Message.main(args) |
Before you execute above script make sure that you have execution rights to your shell script. Use below command for the same.
1 | chmod +x message.sh |
Now execute your shell script and check your output.
1 2 3 4 5 6 7 8 9 10 11 | [root@backtobazics varun]# ls -l total 4 -rw-r--r--. 1 root root 251 Jul 18 08:48 message.sh [root@backtobazics varun]# chmod +x message.sh [root@backtobazics varun]# ls -l total 4 -rwxr-xr-x. 1 root root 251 Jul 18 08:48 message.sh [root@backtobazics varun]# ./message.sh Varun 29 Hello, Varun.....!!!!! Welcome to Scala Script.....!!!!! You are 29 years old. |
Congratulations…..!!!!! Now you can do scripting very well in Windows as well as Linux using your programming knowledge in Scala.
References
https://www.artima.com/pins1ed/scala-scripts-on-unix-and-windows.html