Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
One-liner program
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Python=== Performing one-liners directly on the Unix command line can be accomplished by using [[Python (programming language)|Python]]'s -cmd flag (-c for short), and typically requires the import of one or more modules. Statements are separated using ";" instead of newlines. For example, to print the last field of unix long listing: ls -l | python -c "<syntaxhighlight lang="python" inline> import sys;[sys.stdout.write(' '.join([line.split(' ')[-1]])) for line in sys.stdin]</syntaxhighlight>" ====Python wrappers==== Several open-source scripts have been developed to facilitate the construction of Python one-liners. Scripts such as [http://code.google.com/p/pyp/ pyp] or [http://code.activestate.com/recipes/437932-pyline-a-grep-like-sed-like-command-line-tool/ Pyline] import commonly used modules and provide more human-readable variables in an attempt to make Python functionality more accessible on the command line. Here is a redo of the above example (printing the last field of a unix long listing): <syntaxhighlight lang="bash"> ls -l | pyp "whitespace[-1]" # "whitespace" represents each line split on white space in pyp ls -l | pyline "words[-1]" # "words" represents each line split on white space in pyline </syntaxhighlight> ====Executable libraries==== The Python CGIHTTPServer module for example is also an executable library that performs as a web server with CGI. To start the web server enter: <syntaxhighlight lang="console">$ python -m CGIHTTPServer Serving HTTP on 0.0.0.0 port 8000 β¦</syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)