Translations of this page:

PySh

PyWhat ?

PySh is an attempt to create a shell providing both bash and Python features. Try it out ;). Download, chmod it and run. If you like it, consider putting it in you /usr/bin directory, or even as default shell, if you are an adventurer ;)

PySh is still very young. You should not use it as your default shell by now.

Why ?

Because I was somewhat… bored from studying sql :p But also because I spend a lot of time in command line and often miss Python.

License

PySh is released under the terms of the WTFPL, with a no warranty close. In a few words: you just do what the fuck you want to, at your own risk.

Because I see some people complaining about “why not MIT ?”, ok, it's under MIT too. Want GPL ? Ok, it's under GPL too. The only thing I will keep in PySh's license:
It comes without any warranty, to the extent permitted by applicable law.

Otherwise… do whatever you want.

Known bugs

  • autocompletion still hangs on . - and _

Syntax

In order to use the full power of PySh, you will need to know basic python rules. Refer to their website for tutorials.

Standard shell actions

PySh supports standard actions, including pipes, redirections, conditions etc…

~/smallprog/pyShell > ls
CHANGELOG  MANIFEST  pysh-0.2.tar.gz  README    subdir  testdir
dist       pysh      pysh.tar.gz      setup.py  test

~/smallprog/pyShell > stat pysh
  File: `pysh'
  Size: 12597     	Blocks: 32         IO Block: 4096   regular file
Device: 803h/2051d	Inode: 1613966     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: ( 1000/ antoine)   Gid: ( 1000/ antoine)
Access: 2009-06-11 22:54:10.000000000 +0200
Modify: 2009-06-11 22:42:37.000000000 +0200
Change: 2009-06-11 22:51:36.000000000 +0200

~/smallprog/pyShell > true && echo "Hello"
Hello

~/smallprog/pyShell > cat test | grep t
Salut
Gutentag

PySh also fully supports Python. Once You have started a line or block in python, you must go on in python till the end of it. Except if you use backquotes (see below).

~/smallprog/pyShell > import os
~/smallprog/pyShell > def test():
...     print("Test")
... 
~/smallprog/pyShell > test()
Test
~/smallprog/pyShell > class MyClass:
...     def __init__(self):
...         print("Creating an object")
... 
~/smallprog/pyShell > MyClass()
Creating an object
<__builtin__.MyClass instance at 0x9ba2aec>

But PySh becomes interesting when mixing both of the above ;)

~/smallprog/pyShell > for file in `ls`:
...     print file
... 
CHANGELOG
dist
MANIFEST
pysh
pysh-0.2.tar.gz
pysh.tar.gz
README
setup.py
subdir
test
testdir

# You may include python variables in shell parts
~/smallprog/pyShell > dir = 'subdir'
~/smallprog/pyShell > for file in `ls $dir`:
...     `rm $dir/$file`
... 
~/smallprog/pyShell > ls subdir/

Note: When using backquotes ``, PySh tries to guess what you intended to do. If backquotes are preceded by the in keyword, it guesses you want to make a loop or a contain test. It will thus return a list containing each line resulting from the command.

~/smallprog/pyShell > for file in `ls`:
...     print file
... 
CHANGELOG
dist
MANIFEST
pysh
pysh-0.2.tar.gz
pysh.tar.gz
README
setup.py
subdir
test
testdir

~/smallprog/pyShell > if 'pysh' in `ls`:
...     print("PySh found")
... 
PySh found
~/smallprog/pyShell > # This was equivalent to: if 'pysh' in each('ls'):

If noting is found right before ``, PySh will only execute the command, without capturing the output

~/smallprog/pyShell > for i in range(5):
...     `echo "Hello"`
... 
Hello
Hello
Hello
Hello
Hello
~/smallprog/pyShell > # This was equivalent to: system('echo "Hello"')

Otherwise, PySh will return command result as a string.

~/smallprog/pyShell > content = `ls`
~/smallprog/pyShell > content
'CHANGELOG\ndist\nMANIFEST\npysh\npysh-0.2.tar.gz\npysh.tar.gz\nREADME\nsetup.py\nsubdir\ntest\ntestdir\n'
~/smallprog/pyShell > # This was equivalent to: content = shell('ls')

Built in functions

alias

Alias is the equivalent to bash's one.

~/smallprog/pyShell > alias ll="ls -l"
~/smallprog/pyShell > ll
total 64
-rw-r--r-- 1 antoine antoine   368 2009-06-11 22:35 CHANGELOG
drwxr-xr-x 2 antoine antoine  4096 2009-06-11 22:51 dist
-rw-r--r-- 1 antoine antoine    31 2009-06-11 22:51 MANIFEST
-rwxr-xr-x 1 antoine antoine 12597 2009-06-11 22:42 pysh
-rw-r--r-- 1 antoine antoine  4256 2009-06-11 22:54 pysh-0.2.tar.gz
-rw-r--r-- 1 antoine antoine  4256 2009-06-11 23:00 pysh.tar.gz
-rw-r--r-- 1 antoine antoine   101 2009-06-09 23:43 README
-rw-r--r-- 1 antoine antoine  1259 2009-06-11 22:51 setup.py
drwxr-xr-x 2 antoine antoine  4096 2009-06-11 23:17 subdir
-rw-r--r-- 1 antoine antoine    32 2009-06-11 14:02 test
drwxr-xr-x 2 antoine antoine  4096 2009-06-11 17:16 testdir

system, each and shell

See above (mixing both syntaxes)

slurp

Reads a file at once

~/smallprog/pyShell > content = slurp('test')
~/smallprog/pyShell > content
'Salut\nHello\nGutentag\nSalvus sis\n'

lines

Delivers a file line by line

~/smallprog/pyShell > for l in lines('test'):
...     print l
... 
Salut
Hello
Gutentag
Salvus sis

help

Some help about this shell

pyhelp

Python's built in help.

Config file

When starting, PySh attempts to read the file located at ~/.pyshrc. This file may contain statements to prepare your session, just like a .bashrc file. You may define functions, classes and object in this file, but you have to register then. Otherwise they will not be available in your shell.

alias ls="ls --color=auto"
alias ll="ls -l --color=auto"

def test():
    print("Test")

register("test", test)

Writing scripts

Maybe you'll want to build some scripts, and execute them later, the easy way:

./myScript

But maybe you tried to put, as the first line of your script

#!/usr/bin/pysh

And indeed, it doesn't work. The fix is quite simple ;)

#!/usr/bin/env pysh
 
pysh.txt · Last modified: 2010/05/12 13:32 by antoine_935
 
Recent changes RSS feed Valid XHTML 1.0 Valid CSS Driven by DokuWiki