Shell one-liner: Difference between revisions

From Rosetta Code
Content added Content deleted
(awk)
m (→‎[[Shell One Liner#ALGOL 68]]: try to clean up /tmp)
Line 32: Line 32:
</pre>
</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:
For an [[ELLA ALGOL 68]] one-liner, merge these lines of shell code:
<pre>
<pre>
echo 'PROGRAM hello CONTEXT VOID;USE standard;BEGIN;print(("Hello",new line));END;FINISH;' | tr ";" "\n" > hello.a68;
code='print(("Hello", new line))'
a68toc -lib /usr/share/algol68toc -dir /usr/share/algol68toc -uname SEEDXXX hello.a68;
a=/tmp/algol$$ s=/usr/share/algol68toc;
echo -e "PROGRAM algol$$ CONTEXT VOID\nUSE standard\nBEGIN\n$code\nEND\nFINISH\n" > $a.a68 &&
gcc /usr/share/algol68toc/Afirst.o hello.c -la68s -la68 -lm -lc -o hello;
a68toc -lib $s -dir $s -uname TMP -tmp $a.a68 && rm $a.a68 &&
./hello
gcc $s/Afirst.o $a.c -l{a68s,a68,m,c} -o $a && rm $a.c &&
$a; rm $a
</pre>
</pre>
Output:
Output:

Revision as of 22:30, 9 February 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.

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)


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

C

Works with: Linux

Not orthodox, but working...

<lang c> $ touch /tmp/T0.c /tmp/T && chmod 600 /tmp/T0.c /tmp/T && echo -e "#include<stdio.h>\nint main(){printf(\"Hello\\\n\");return 0;}" >/tmp/T0.c &&

 gcc /tmp/T0.c -o /tmp/T && /tmp/T && rm -f /tmp/T0.c /tmp/T

</lang>

Hello

Common Lisp

Varies by implementation; in SBCL,

<lang sh>sbcl --noinform --eval '(progn (princ "Hello") (terpri) (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()'

Haskell

<lang> $ ghc -e 'putStrLn "Hello"' Hello </lang>

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>

Python

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

Ruby

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