Shell one-liner: Difference between revisions
added BASIC |
→{{header|E}}: {{lines_too_long}} template |
||
Line 69: | Line 69: | ||
=={{header|E}}== |
=={{header|E}}== |
||
{{lines_too_long}} |
|||
<lang sh>rune --src.e 'println("Hello")'</lang> |
<lang sh>rune --src.e 'println("Hello")'</lang> |
||
Revision as of 03:55, 26 January 2010
You are encouraged to solve this task according to the task description, using any language you may know.
Show how to specify and execute a short program in the language from a command shell.
Avoid depending on the particular shell or operating system used as much as is reasonable; if the language has notable implementations which have different command argument syntax, or the systems those implementations run on have different styles of shells, it would be good to show multiple examples.
ALGOL 68
$ a68g -e 'print(("Hello",new line))'
Output:
Hello
For an ELLA ALGOL 68 one-liner, merge these lines of shell code:
code='print(("Hello", new line))' a=/tmp/algol$$ s=/usr/share/algol68toc; echo -e "PROGRAM algol$$ CONTEXT VOID\nUSE standard\nBEGIN\n$code\nEND\nFINISH\n" > $a.a68 && a68toc -lib $s -dir $s -uname TMP -tmp $a.a68 && rm $a.a68 && gcc $s/Afirst.o $a.c -l{a68s,a68,m,c} -o $a && rm $a.c && $a; rm $a
Output:
Hello
AutoHotkey
<lang autohotkey>RunWait, %comspec% /c systeminfo > tmp,,Hide FileRead, var, tmp FileDelete, tmp MsgBox,% var</lang>
AWK
Maybe the most common way one can use awk is from the command line for one-liners, feeding the interpreter with an input.
$ awk 'BEGIN { print "Hello"; }'
A more "complex" and "real" example:
$ awk '/IN/ { print $2, $4; }' <input.txt
Select field 2 and 4 of lines matching the regular expression /IN/ (i.e. where IN appears)
BASIC
(I don't know if this is standard for BASIC, or just Microsoft's flavors.)
<lang qbasic>SHELL "echo Hello, world!"</lang>
C
The following code leaves the file a.out in the current directory (it does not delete it to avoid to call another shell/system dependent command/program). The current directory is not specified by ./ in every system...
$ echo 'main() {printf("Hello\n");}' | gcc -w -x c -; ./a.out
Clojure
clj-env-dir comes with clojure-contrib.
<lang sh>$ clj-env-dir -e "(defn add2 [x] (inc (inc x))) (add2 40)"
- 'user/add2
42 </lang>
Common Lisp
Varies by implementation
<lang sh>sbcl --noinform --eval '(progn (princ "Hello") (terpri) (quit))'</lang>
<lang sh>clisp.exe -q -x "(progn (format t \"Hello from CLISP\") (quit))"</lang>
E
<lang sh>rune --src.e 'println("Hello")'</lang>
The --src
option ends with the the filename extension the provided type of program would have:
rune --src.e-awt 'def f := <swing:makeJFrame>("Hello"); f.show(); f.addWindowListener(def _{to windowClosing(_) {interp.continueAtTop()} match _{}}); interp.blockAtTop()'
Emacs Lisp
<lang sh>emacs -batch -eval '(princ "Hello World!\n")' </lang>
Or another example that does something useful: indent a C source file:
<lang sh>emacs -batch sample.c --eval '(indent-region (point-min) (point-max) nil)' -f save-buffer</lang>
Erlang
Erlang always starts other applications that can run in parallel in the background, and as such will not die by itself. To kill erl, we sequentially run the 'halt' function from the 'erlang' module (the -S is there to guarantee 'halt' will be evaluated after the io function).
$ erl -noshell -eval 'io:format("hello~n").' -s erlang halt hello
Factor
$ factor -run=none -e="USE: io \"hi\" print"
Forth
$ gforth -e ".( Hello) cr bye" Hello
F#
> echo printfn "Hello from F#" | fsi --quiet Hello from F#
Haskell
$ ghc -e 'putStrLn "Hello"' Hello
J
$ jconsole -js "exit echo 'Hello'" Hello
JavaScript
$ js -e 'print("hello")' hello
OCaml
$ ocaml <(echo 'print_endline "Hello"') Hello
Perl
$ perl -e 'print "Hello\n"' Hello
Perl 6
$ perl6 -e 'say "Hello, world!"' Hello, world!
PHP
assuming you have the PHP CLI (command-line interface) installed, not just the web server plugin
$ php -r 'echo "Hello\n";' Hello
Pike
<lang pike>$ pike -e 'write("Hello\n");' Hello</lang>
PowerShell
> powershell -Command "Write-Host 'Hello'" Hello
Python
$ python -c 'print "Hello"' Hello
R
$ echo 'cat("Hello\n")' | R --slave Hello
Alternatively, using the Rscript front-end,
$ Rscript -e 'cat("Hello\n")' Hello
REBOL
rebview -vswq --do "print {Hello!} quit"
Output:
Hello!
Ruby
$ ruby -e 'puts "Hello"' Hello
Scala
C:\>scala -e "println(\"Hello\")" Hello
The escaping of quotes is required by Windows. On Unix, one could just use single quotes around the code. In either case, any required libraries should have their JAR files pointed at by the environment variable CLASSPATH.
Slate
<lang slate>./slate --eval "[inform: 'hello'] ensure: [exit: 0].".</lang>
Tcl
This is an area where Tcl is lacking:
$ echo 'puts Hello' | tclsh Hello
Ursala
The command to execute the Ursala compiler is fun. An expression supplied as a parameter to the --main option is compiled and evaluated. If the expression evaluates to a list of character strings, it can be displayed on standard output with --show. If it's some other type, it can be formatted for display by --cast <type expression>,
$ fun --main=-[hello]- --show hello $ fun --main="power/2 32" --cast %n 4294967296 $ fun --m="..mp2str mpfr..pi 120" --c %s '3.1415926535897932384626433832795028847E+00'
- Programming Tasks
- Programming environment operations
- ALGOL 68
- AutoHotkey
- AWK
- BASIC
- C
- Clojure
- Common Lisp
- E
- Examples needing attention
- Emacs Lisp
- Erlang
- Factor
- Forth
- F Sharp
- Haskell
- J
- JavaScript
- OCaml
- Perl
- Perl 6
- PHP
- Pike
- PowerShell
- Python
- R
- REBOL
- Ruby
- Scala
- Slate
- Tcl
- Ursala
- C sharp/Omit
- Java/Omit
- TI-83 BASIC/Omit
- TI-89 BASIC/Omit
- Modula-3/Omit