terrycojones / daudin

A Python command-line shell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ZSH syntax failing

reckoner opened this issue · comments

The following ZSH syntax is failing and I don't understand what the debug message is saying.

>>> foreach x ({2015..2019})
                    Processing 'foreach x ({2015..2019})'.
                    Not in pipeline.
                    Trying eval 'foreach x ({2015..2019})'.
                    Could not eval: invalid syntax (<string>, line 1).
                    Trying to compile 'foreach x ({2015..2019})'.
                    SyntaxError: invalid syntax (<input>, line 1).
                    Trying shell 'foreach x ({2015..2019})' with stdin ['/bin/sh: 1: Syntax error: "(" unexpected'].
                    In _shPty, stdin is None
/bin/sh: 1: Syntax error: "(" unexpected
                    Shell returned '/bin/sh: 1: Syntax error: "(" unexpected\n'

Hi. That's because the Python subprocess module passes the command to /bin/sh by default. So the syntax you're using causes an error. Maybe you could just use Python:

>>> for i in range(2015, 2020):
...   print(i)
... 
2015
2016
2017
2018
2019

Or use the UNIX seq command:

>>> seq 2015 2019 | for i in _:
...  print(i + ' hello')
2015 hello
2016 hello
2017 hello
2018 hello
2019 hello

I plan to do 2 things that will help you out. One is to make it possible to have a different shell used, and the second it to just have one shell running, not fire a new shell for each command (that will allow persistent variables in the underlying shell). But maybe the above is enough for you to do what you're after?

I just pushed a change that lets you specify the underlying shell. So you may want to invoke daudin using

$ daudin --shell '/bin/zsh -c'

or you can use DAUDIN_SHELL so you don't have to think about it any more:

$ export DAUDIN_SHELL='/bin/zsh -c'

(which assumes zsh is in /bin and assumes you're using bash or zsh).

Note that your foreach command would need to be on one line because daudin doesn't know how to send multiple command lines to the same underlying shell. So:

$ daudin --shell '/bin/zsh -c'
>>> foreach i ({2015..2019}); do echo $i; done
2015
2016
2017
2018
2019

or using bash:

$ daudin --shell '/bin/bash -c'
>>> for i in {2015..2019}; do echo $i; done
2015
2016
2017
2018
2019

whereas with /bin/sh you'd get:

$ daudin --shell '/bin/sh -c'
>>> for i in {2015..2019}; do echo $i; done
{2015..2019}

Thanks! It makes more sense now. Your comment about not being able to process multiple lines is key.