If I write a Scala script (or App) that just prints out the command line arguments, I notice that it ignores all the '!' characters. For example, if my script looks like:
println(args(0))
And I run it with:
scala MyScript "Hello!"
I get the output:
Hello
And if I run it with:
scala MyScript "Hello! World!"
I also get:
Hello
What's going on? What is the purpose of the ! character in arguments that causes this behaviour?
! is history substitution.
Try scala MyScript hello! with ! at EOL to see it work as if quoted.
http://www.gnu.org/software/bash/manual/bashref.html#Event-Designators
http://www.gnu.org/software/bash/manual/bashref.html#Single-Quotes
On windows, the "scala.bat" command script has delayed expansion enabled.
http://en.wikibooks.org/wiki/Windows_Batch_Scripting
http://en.wikibooks.org/wiki/Windows_Batch_Scripting#SETLOCAL
http://en.wikibooks.org/wiki/Windows_Batch_Scripting#Command-line_arguments
To disable interpretation of !myvar!:
C:\Users\you> scala greeting.script "hello^!"
hello!
C:\Users\you> set world= Bob
C:\Users\you> scala greeting.script "hello!world!"
hello Bob
Related
Need to execute the scala script through spark-shell with silent mode. When I am using spark-shell -i "file.scala", after the execution, I am getting into the scala interactive mode. I don't want to get into there.
I have tried to execute the spark-shell -i "file.scala". But I don't know how to execute the script in silent mode.
spark-shell -i "file.scala"
after execution, I get into
scala>
I don't want to get into the scala> mode
Updating (October 2019) for a script that terminates
This question is also about running a script that terminates, that is, a "scala script" that run by spark-shell -i script.scala > output.txt that stopts by yourself (internal instruction System.exit(0) terminates the script). See this question with a good example.
It also needs a "silent mode", it is expected to not pollute the output.txt.
Suppose Spark v2.2+.
PS: there are a lot of cases (typically small tools and module/algorithm tests) where Spark interpreter can be better than compiler... Please, "let's compile!" is not an answer here.
spark-shell -i file.scala keeps the interpreter open
in the end, so System.exit(0) is required to be at the end of your script. The most appropriate solution is to place your code in try {} and put System.exit(0) in finally {} section.
If logging is requiered you can use something like this:
spark-shell < file.scala > test.log 2>&1 &
If you have limitations on editing file and you can't add System.exit(0), use:
echo :quit | scala-shell -i file.scala
UPD
If you want to suppress everything in output except printlns you have to turn off logging for spark-shell. The sample of configs is here. Disabling any kind of logging in $SPARK-HOME/conf/log4j.properties should allow you to see only pritnlns. But I would not follow this approach with printlns. Using general Logging with log4j should be used instead of printlns. You can configure it so obtain the same results as with printlns. It boils down to configuring a pattern. This answer provides an example of a pattern that solves your issue.
The best way is definitively to compile your scala code to a jar and use spark-submit but if you're simply looking for a quick iteration loop, you can simply issue a :quit after parsing your scala code:
echo :quit | scala-shell -i yourfile.scala
Adding onto #rluta's answer. You can place the call to spark-shell command inside a shell script. Say the below in a shell script:
spark-shell < yourfile.scala
But this would require you to keep the lines of code within a line in case a statement is written on different lines.
OR
echo :quit | spark-shell -i yourfile.scala
This should
I am studying Scala and I am using Scala for win 10
I have write a script "helloarg.scala" with 1 line println("Hello, " + args(0) + "!")
When i used cmd to run file (not in scala shell), it worked.
C:\Users\Darkntnt>scala D:\Scala\helloarg.scala planet
Hello, planet!
However, It got the error when i load file from scala shell
Error:
scala> :load D:\Scala\helloarg.scala planet
usage: :load -v file
Please help me to fix this problem.
Thank you.
That's a good one.
:load doesn't respect args; there are a few different idioms for "run an app as a script".
I'll add this example to the issue about unifying them.
As a workaround, maybe val args = Array("planet") or similar.
This question already has answers here:
Why does Scala use a reversed shebang (!#) instead of just setting interpreter to scala
(3 answers)
Closed 7 years ago.
See the following script from scala tutorial, What does the !# mean?
#!/bin/sh
exec scala "$0" "$#"
!#
object HelloWorld extends App {
println("Hello, world!")
}
HelloWorld.main(args)
See: http://www.scala-lang.org/files/archive/nightly/docs-master/manual/html/scala.html
#! and !# mark script header. All content between those marks are ignored by scala interpreter. In that section you can put shell script which will launch the actual scala script.
The !# is called a "bangshe" (opposite of a "shebang"). It is a bit like a closing brace on the header statement.
In case of "ls" command it runs with and without the prefix "!". In case of "cat fileName" it's the same, but when you consider "wc -l fileName" it works only with "!" prefix.
When you combine cat and wc command "cat fileName | wc -l" executed successfully without "!" prefix.
I don't understand the logic behind this prefix "!" in ipython.
Thank you in advance
(I am new to python programming, if it sounds silly question please forgive me.)
IPython tries to make interactive programming as comfortable as possible. Some shell builtins like ls, cd or cat are basic commands to navigate in unix shells. IPython, as a "Python Shell" provides the same functionally for convenience. Along with features like colored output, etc.
The !command is for executing arbitrary shell code and is much more powerful. It can be used to run any command you can type in a normal shell and can also catch its output.
Compare ls with !ls. The former will print the content in your current directory with nice coloring. The latter will print the same list, but just plain text.
But note that you can do really cool things with !command:
files = !ls
for f in files:
print("I like this file:", f)
Which reads the output of ls into a python array files which you can use in your code just like any other array.
To sum up: if you just want to navigate, you usually use the standard commands, if available. If you need to capture the output or run programs you have to use the !command syntax.
In python, I can run a script and enter interactive mode in the context of that script. This lets me mess with global variables and what not to examine program state.
$ python -i hello.py
Can I do this with Coffeescript? I've tried the following:
$ coffee -i hello.coffee
doesn't load hello.coffee. It's equivalent to coffee -i
$ cat hello.coffee | coffee -i
runs the script line by line in REPL but ends REPL after the EOF.
I've recently started a project to create an advanced interactive shell for Node and associated languages like CoffeeScript. One of the features is loading a file or string in the context of the interpreter at startup which takes into account the loaded language.
http://danielgtaylor.github.com/nesh/
Example:
# Load a string
nesh -c -e 'hello = (name) -> "Hello, #{name}"'
# Load a file
nesh -c -e hello.coffee
Then in the interpreter you can access the hello function. Also a good idea to create an alias in bash:
alias cs='nesh -c'
cat foo.coffee - | coffee -i
tells cat to first output your code and then output stdin, which gives you what you're looking for I think.
I am confronted with this problem as well. The one provide by #int3 doesn't solve this problem, for CoffeeScript is one indentation based language. stdin will pass the code line by line, but the repl is not smart enough to realize this. Since you post this question, I suggest you create one issue (feature request) on CoffeeScript