Shell one-liner: Difference between revisions

From Rosetta Code
Content added Content deleted
m (omit Modula-3)
m (whitespace)
Line 6: Line 6:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386 - Interpret straight off}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386 - Interpret straight off}}
<pre>$ a68g -e 'print(("Hello",new line))'</pre>
<pre>
$ a68g -e 'print(("Hello",new line))'
</pre>
Output:
Output:
<pre>
<pre>Hello</pre>
Hello
</pre>
{{works with|ELLA ALGOL 68|Any - tested with release 1.8.8d.fc9.i386 - translate to [[C]] and then compile and run}}
{{works with|ELLA ALGOL 68|Any - tested with release 1.8.8d.fc9.i386 - translate to [[C]] and then compile and run}}
For an [[ELLA ALGOL 68]] one-liner, merge these lines of shell code:
For an [[ELLA ALGOL 68]] one-liner, merge these lines of shell code:
<pre>code='print(("Hello", new line))'
<pre>
code='print(("Hello", new line))'
a=/tmp/algol$$ s=/usr/share/algol68toc;
a=/tmp/algol$$ s=/usr/share/algol68toc;
echo -e "PROGRAM algol$$ CONTEXT VOID\nUSE standard\nBEGIN\n$code\nEND\nFINISH\n" > $a.a68 &&
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 &&
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 &&
gcc $s/Afirst.o $a.c -l{a68s,a68,m,c} -o $a && rm $a.c &&
$a; rm $a
$a; rm $a</pre>
</pre>
Output:
Output:
<pre>
<pre>Hello</pre>
Hello
</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
Line 38: Line 30:
Maybe the most common way one can use awk is from the command line for one-liners, feeding the interpreter with an input.
Maybe the most common way one can use awk is from the command line for one-liners, feeding the interpreter with an input.


<pre>$ awk 'BEGIN { print "Hello"; }'</pre>
<pre>
$ awk 'BEGIN { print "Hello"; }'
</pre>


A more "complex" and "real" example:
A more "complex" and "real" example:


<pre>$ awk '/IN/ { print $2, $4; }' <input.txt</pre>
<pre>
$ awk '/IN/ { print $2, $4; }' <input.txt
</pre>


''Select'' field 2 and 4 of lines matching the regular expression <tt>/IN/</tt> (i.e. where IN appears)
''Select'' field 2 and 4 of lines matching the regular expression <tt>/IN/</tt> (i.e. where IN appears)
Line 56: Line 44:
delete it to avoid to call another shell/system dependent command/program). The
delete it to avoid to call another shell/system dependent command/program). The
''current directory'' is not specified by <tt>./</tt> in every system...
''current directory'' is not specified by <tt>./</tt> in every system...
<pre>$ echo 'main() {printf("Hello\n");}' | gcc -w -x c -; ./a.out</pre>
<lang c>
$ echo 'main() {printf("Hello\n");}' | gcc -w -x c -; ./a.out
</lang>

<pre>
Hello
</pre>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 103: Line 85:


=={{header|Haskell}}==
=={{header|Haskell}}==
<pre>$ ghc -e 'putStrLn "Hello"'
<lang>
Hello</pre>
$ ghc -e 'putStrLn "Hello"'
Hello
</lang>


=={{header|J}}==
=={{header|J}}==
<lang>$ jconsole -js "exit echo 'Hello'"
<lang>
Hello</lang>
$ jconsole -js "exit echo 'Hello'"
Hello
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==


<lang>$ ocaml <(echo 'print_endline "Hello"')
<lang>
Hello</lang>
$ ocaml <(echo 'print_endline "Hello"')
Hello
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
<lang>$ perl -e 'print "Hello\n"'
<lang>
Hello</lang>
$ perl -e 'print "Hello\n"'
Hello
</lang>


=={{header|PHP}}==
=={{header|PHP}}==
assuming you have the PHP CLI (command-line interface) installed, not just the web server plugin
assuming you have the PHP CLI (command-line interface) installed, not just the web server plugin
<lang>$ php -r 'echo "Hello\n";'
<lang>
Hello</lang>
$ php -r 'echo "Hello\n";'
Hello
</lang>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<pre>> powershell -Command "Write-Host 'Hello'"
<pre>> powershell -Command "Write-Host 'Hello'"
Hello
Hello</pre>
</pre>


=={{header|Python}}==
=={{header|Python}}==
<lang>$ python -c 'print "Hello"'
<lang>
Hello</lang>
$ python -c 'print "Hello"'
Hello
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang>$ ruby -e 'puts "Hello"'
<lang>
Hello</lang>
$ ruby -e 'puts "Hello"'
Hello
</lang>


=={{header|Slate}}==
=={{header|Slate}}==
<lang slate>./slate --eval "[inform: 'hello'] ensure: [exit: 0].".</lang>
<lang slate>
./slate --eval "[inform: 'hello'] ensure: [exit: 0].".
</lang>



=={{header|Tcl}}==
=={{header|Tcl}}==
Line 169: Line 133:
be displayed on standard output with --show. If it's some other
be displayed on standard output with --show. If it's some other
type, it can be formatted for display by --cast <type expression>,
type, it can be formatted for display by --cast <type expression>,

<pre>
$ fun --main=-[hello]- --show
<pre>$ fun --main=-[hello]- --show
hello
hello
$ fun --main="power/2 32" --cast %n
$ fun --main="power/2 32" --cast %n
4294967296
4294967296
$ fun --m="..mp2str mpfr..pi 120" --c %s
$ fun --m="..mp2str mpfr..pi 120" --c %s
'3.1415926535897932384626433832795028847E+00'
'3.1415926535897932384626433832795028847E+00'</pre>
</pre>


{{Omit From|C sharp}}
{{Omit From|C sharp}}
{{Omit From|Java}}
{{Omit From|Java}}
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have an external OS/command processor. -->
{{omit from|TI-83 BASIC}}
{{omit from|TI-89 BASIC}} <!-- Does not have an external OS/command processor. -->
{{omit from|Modula-3}}
{{omit from|Modula-3}}

Revision as of 13:22, 28 September 2009

Task
Shell one-liner
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

Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386 - Interpret straight off
$ a68g -e 'print(("Hello",new line))'

Output:

Hello
Works with: ELLA ALGOL 68 version Any - tested with release 1.8.8d.fc9.i386 - translate to C and then compile and run

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)

C

Works with: gcc

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

Common Lisp

Varies by implementation

Works with: SBCL

<lang sh>sbcl --noinform --eval '(progn (princ "Hello") (terpri) (quit))'</lang>

Works with: CLISP

<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


Forth

Works with: GNU 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

<lang>$ jconsole -js "exit echo 'Hello'" Hello</lang>

OCaml

<lang>$ ocaml <(echo 'print_endline "Hello"') Hello</lang>

Perl

<lang>$ perl -e 'print "Hello\n"' Hello</lang>

PHP

assuming you have the PHP CLI (command-line interface) installed, not just the web server plugin <lang>$ php -r 'echo "Hello\n";' Hello</lang>

PowerShell

> powershell -Command "Write-Host 'Hello'"
Hello

Python

<lang>$ python -c 'print "Hello"' Hello</lang>

Ruby

<lang>$ ruby -e 'puts "Hello"' Hello</lang>

Slate

<lang slate>./slate --eval "[inform: 'hello'] ensure: [exit: 0].".</lang>

Tcl

This is an area where Tcl is lacking: <lang>$ echo 'puts Hello' | tclsh Hello</lang>

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'