Giter Site home page Giter Site logo

xontrib-sh's Introduction

Paste and run commands from bash, fish, zsh, tcsh in xonsh shell.

If you like the idea click ⭐ on the repo and and tweet.

Install

xpip install -U xontrib-sh
echo 'xontrib load sh' >> ~/.xonshrc
# Reload xonsh

Usage

Start the line with ! (exclamation mark with space) then paste the sh-compatible commands after it and run. The commands syntax will be tested in the shells from list (if installed) and the commands will be run in the first matching shell. By default list of shells contains bash and sh.

The commands will be executed in the environment that will be inherited from current but if the commands modify the environment there will no changes in source xonsh environment.

To set the list of shells use environment variable before loading the xontrib:

$XONTRIB_SH_SHELLS = ['bash', 'sh']  # default
xontrib load sh
! echo hello

Also you can set the shell explicitly i.e. !bash or !b (the first letter of the shell).

The main use case

The main use case of xontrib-sh is when you copy and paste the sh-commands from some article or instruction and this commands are environment agnostic and you want to run it without rewriting it on xonsh or run sh-shell.

For example you've found snippet of bash commands that check existing of curl:

TMP=/tmp && cd $TMP && ( [[ -x $(command -v curl) ]] && echo "Yes" || echo "No" )  

You hesitate how xonsh will execute this and you're absolutely right there will be syntax error. To run this just start with ! or !b or !bash and paste the commands. As result you'll see the right message.

Examples

Bash brace expansion

! echo 01.{05..10}
# Or explicitly:
!b echo 01.{05..10}
!bash echo 01.{05..10}
bash:
01.05 01.06 01.07 01.08 01.09 01.10

Multiline loop

! for i in 1 2 3
do
   echo $i
done
bash:
1
2
3

Use environment variables to pass values from xonsh to sh

$ENV = 'hello'
! echo $ENV!
bash:
hello!

Known issues

Determining the shell on short command

In case of usage many different shells the detection of the shell works perfect when the commands contain shell-specific syntax. But if you run the short command that could be valid in all shells the first matched shell will be chosen but it's could be wrong.

For example you have bash and fish in the list of shells. The short fish command may be determined as bash command. As result the command will be failed:

$XONTRIB_SH_SHELLS = ['bash', 'fish']
xontrib load sh
# Run fish command:
! set -U var1 value1
# bash: line 0: set: -U: invalid option

To avoid this use the explicit setting the shell i.e. !fish set -U var1 value1.

Also, since pwsh and cmd shells don't have an option to detect their own syntax, they can only be invoked:

  • explicitly by their name, i.e. !p or !pwsh
  • implicitly via the ! prefix only when there is one shell in $XONTRIB_SH_SHELLS

Why it's better than xonsh subprocess macros?

Xonsh subprocess macros is not supporting multiline commands and require more keystrokes.

Additional options

  • $XONTRIB_SH_USEFULL (default True) - enables to set the shell explicitly by the name of the shell i.e. !bash .
  • $XONTRIB_SH_USEFIRST (default True) - enables to set the shell explicitly by the first letter i.e. !b instead of !bash .

Links

xontrib-sh's People

Contributors

anki-code avatar eugenesvk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

xontrib-sh's Issues

Using subprocess macros

I just stumbled across this package. I don't know if this is possible. But here is an idea of using subprocess macros to identify the shell language.

So something like

bash! export var=12
zsh! export var=12

would become possible.

Is there a way to make an alias that will be interpreted the same as manually entering this?

For example the only way I can use nvm in xonsh is with

Doing this in terminal works fine

! [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && nvm ls

If i setup an alias to make it easier to do that it fails

aliases['nvm'] = '! [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm'
# or like
aliases['nvm'] = '!bash [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm'

I get an error like this

@ nvm
Traceback (Most recent call last):
523 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/parsers/base.py parse --> tree = self.parser.parse(input=s, lexer=self.lexer, debug=debug_level)
335 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/ply/ply/yacc.py parse --> return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
1203 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/ply/ply/yacc.py parseopt_notrack --> tok = call_errorfunc(self.errorfunc, errtoken, self)
194 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/ply/ply/yacc.py call_errorfunc --> r = errorfunc(token)
3459 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/parsers/base.py p_error --> self._parse_error(msg, self.currloc(lineno=p.lineno, column=p.lexpos))
650 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/parsers/base.py _parse_error --> raise_parse_error(msg, loc, self.xonsh_code, self.lines)
238 /home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/parsers/base.py raise_parse_error --> raise err
SyntaxError: <exec-alias:nvm>:1:0: ('code: !',)
!bash [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm
^
~ [✖ ERROR] at 18:40:48
 
@

If i try to put all but the ! in the alias and then use ! nvm which is an alias for [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm it also doesn't like that I think as its being expanded in bash and skipping the xontrib loading.

Seems like I should be able to create an alias that calls it through the sh thing like doing it manually would, but I probably missed that option.

Any tips would be super appreciated. This plugin is what really ensured I didn't give up on xonsh. The convenience of being able to prefix is amazing. I'm just curious if there is a way to make an alias or callable that mimics me entering the ! at the beginning before its executed.

Support updating environment variables

The idea:

  1. Get prev_env = $(env) before running the sh-command
  2. Run sh-command. Example ! export QWE=123.
  3. Add saving env at the end of command: ! export QWE=123 ; env > /tmp/post_env.
  4. By comparing prev_env and post_env update the environment in the current session i.e. $QWE='123'

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

PowerShell hasn't syntax checking option

On Windows when including PowerShell 7 ($XONTRIB_SH_SHELLS = ['pwsh']) trying a ! echo 1 fails with

pwsh:
Invalid argument '-nc', did you mean:
  -encodedcommand

I guess this is due to the fact that there is no -n flag in PowerShell, and I don't know what the alternative for noexec syntax check is

check_output = __xonsh__.subproc_captured_stdout([s, '-nc', shell_cmd, '2>&1']).strip()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.