Welcome to the Pythonista Community Forums!

Pythonista is a Python programming environment for iOS. To learn more, head over to the Pythonista Website.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Twitter
Advanced shell - shellista
  • https://gist.github.com/4139094

    Commands and features:
    • cd - change directory
    • pwd - print current working directory
    • ls - list directory contents (and file sizes)
    • cat - print the contents of a file
    • q/quit/exit/logoff/logout - exit the shell
    • mkdir - make a directory
    • mv - move or rename files / directories
    • cp - copy files / directories (recursively)
    • rm - delete files / directories (recursively)
    • unzip - unzip zip archives
    • untar - untar tar archives
    • ungzip / gunzip - ungzip gzipped files
    • Supports wildcard matches (matching any number of characters)
    • Supports ? / question mark matches (matching exactly one of any character)
    • Supports [ranges] (like [a-z], [0-9], [a-f0-9], etc.)
    • Supports ~ / tilde replacement (defaults to Documents folder)
    • Supports environment variables ($HOME, but you can expand it)
    • Supports single quote escaping (special characters are disabled)
    • Supports backslash escaping (for individual special characters)
    • Supports double quotes (for preserving spaces, but allowing special sequences)


    Examples of advanced usage:
    ls *.py */*.py (lists all .py files in current directory and at the top level of any folders in current directory)
    cp ~/*.py backup (copies all python scripts in the root scripts directory to a folder named 'backup')
    rm test[1-3]?.txt (removes files test1a.txt, test2a.txt, test2b.txt, test2c.txt, test3-.txt, etc.)

    This is an intial (rough) port of a script I put together for another python interpreter on the app store. I'll be porting the rest of the commands soon: unzip, ungzip, untar, wget/curl (basic download functionality)


    Enjoy :)



    For the interested programmer:

    This script uses a pure python re-implementation of some of the more basic globbing of "bash" which I wrote up myself. It's a little different than shlex, glob, or shlex+glob in that matching happens in a single parsing run through. It depends on glob for wildcard handling, but only after my code has already handled all character escapes and word parsing.

    An example where shlex/glob fails:

    In the current working directory are three files: 'test apple', 'peach', 'test a*' (with an asterisk as part of the name)

    The command to parse is the string: rm 'test a*' peach

    shlex interprets it as ['rm', 'test a*', 'peach'], which glob then interprets the file portion as a match of three files: ['test apple', 'test a*', 'peach']. shlex unfortunately clobbers quotes that in a bash environment specify that the special character should be treated as a literal. This would result in deletion of all 3 files.

    With my parser, the single quotes around 'test a*' are interpreted bash-style to mean escaping of the special character '*' - disabling it as a wildcard match and turning it into a literal match, resulting in the deletion of only two files: 'test a*' and 'peach'.
  • Wow, this is pretty cool! Thanks for sharing.

    As a minor improvement, I would suggest to call editor.reload_files() after a mv, cp, or rm command, so that the library view is updated correctly. Actually, this would only be necessary if those commands affect .py files within the Documents directory, but I guess always calling it doesn't hurt much either.
  • Changes:
    • Corrected a bug in the 'cp' command.
    • Added unzip command.
  • Changes:
    • Corrected a bug in the 'unzip' command.
    • Added untar command.
  • Changes:
    • Added ungzip/gunzip command.
  • Nice, tried cd, ls, mv and rm , works on Documents nicely
    cd .. several time is not allowed (the Pythonista app dir e.g.)

  • YES this is a big tool used e.g. As follows:
    Programming the RPN caculator, I did not use unittests, ;-( but decided to debug, works but is not really nice. So searching the web it is sugested to use logging facility of Python and that works ...
    And looking into the log-file as well removing it your script is superb!

    Still one wish/question how should I switch easy from one application to the other?

    Maybe a button in the calculator to stop itself (Scene app.) and call shellista after the run-command? I will try ;-) this approach ...
  • Nice. Thanks. I could have done with this when I started with pythonista.

    (We could do with a 'thanks' button on here.)
  • You're welcome :)
  • Added bare bones wget: https://gist.github.com/4367145
    Tabs are a mess, sorry. Haven't got to grips with Pythonista's tabbing yet
  • This is handy, thanks for sharing!

    Is there a way to get sub directories to show up in the Library view?
  • @DanG - if you're talking about the script list GUI in Pythonista, omz would have to add that. The script list only shows files directly inside it ending in .py.
  • @DanG @pudquick The library view will also show directories in the next update (1.3).
  • Took me a few minutes to get used to it, but once I did I found it VERY useful! Now I can manage those files. :) Thank you!

    Cubbarooney
  • @pudquick - Thanks Michael, this is very cool. Shell prompt with no need to jailbreak. :)

    The thing I'm missing most so far is "-la" on ls. Not being able to see file ownership, permissions, size, and date, is a bit frustrating.

    But, I suppose if I get frustrated enough I'll fix it myself. :)
  • I'll add in the permissions bit, possibly, but considering that Pythonista on iOS can't launch a +x file / executable anyways ... I'm not sure what it is you're caring about in regards to permissions/modes.