What is the difference in using ! and % before a python command? [duplicate] - ipython

This question already has an answer here:
In Jupyter Notebooks on Google Colab, what's the difference between using % and ! to run a shell command?
(1 answer)
Closed 1 year ago.
I know that using ! before a command in python runs the command using the terminal.
For example:
!ls
!unzip zippedfile.zip
However, I noticed that using % also works for some bash commands but not for others.
So
%ls
will work, but
%unzip
will NOT work.
What is the difference between these two prefixes?

The ! escape runs an external shell command, like ping -c 3 www.google.com, not a Python command. Python or ipython has no idea what ping does, it just passes over control to it, and displays its output.
The % escape runs an ipython built-in command or extension, i.e. something that ipython itself understands.
To quote the documentation,
User-extensible ‘magic’ commands. A set of commands prefixed with % or %% is available for controlling IPython itself and provides directory control, namespace information and many aliases to common system shell commands.
The source of confusion here is probably that e.g. ls is also available as a "magic" command for portability and convenience. (It's portable in that it works even on platforms where there is no system ls command, like Windows.)

Related

Want to activate a virtual environment from terminal with Octave/Matlab

I would like to execute a bash command to activate a virtual environment with Octave using Linux. What I actually want to do is run DeepSpeech using Octave/Matlab.
The command I want to use is
source $HOME/tmp/deepspeech-venv/bin/activate
The line of code I tried on my own is system("source $HOME/tmp/deepspeech-venv/bin/activate")
And the output I'm getting is sh: 1: source: not found
I saw this answer on a post and tried this command setenv('PATH', ['/source $HOME/tmp/deepspeech-venv/bin/activate', pathsep, getenv('PATH')]) but with no help it returned the same error.
It's not completely clear from your question, but I'm assuming you're trying to do is run python commands within octave/matlab, and you'd like to use a python virtual environment for that.
Unfortunately, when you run a system command from within octave, what most likely happens is that this creates a subshell to execute your command, which is discarded once the command has finished.
You have several options to rectify this, but I think the easiest one would be to activate the python virtual environment first, and run your octave instance from within that environment. This then inherits all environmental variables as they existed when octave was run. You can confirm this by doing getenv( 'VIRTUAL_ENV' ).
If that's not an option, then you could make sure that all system commands intended to run python scripts, are prefixed with a call to the virtual environment first (e.g. something like system( 'source ./my/venv/activate; python3 ./myscript.py') ).
Alternatively, you can try to recreate the virtual environment from its exported variables manually, using the setenv command.

Shell alias expansion under ESXi host does not auto complete - AND - detecting if running interactively

Since ESXi does not come with bash, I am sourcing an .sh file to set up some custom aliases for common commands while connected via ssh.
On other distros like RHEL, I can type part of an alias and hit tab to autocomplete. This does not seem to work under ESXi 7.x.
Is there a switch or something that I can turn on to make autocomplete work for custom aliases, or is this just a limitation of the shell that ESX offers?
NOTE: If type l<tab> then I DO get the built in commands that start with L.
Also while I'm on the topic of the ESXi shell… On RHEL I have this line in my .bashrc file
[[ $- != *i* ]] && return
Purpose of this code being that if the current session is not being ran interactively then return else run rest of code.
When I run this on esx I get the error sh: *i*: unknown operand. Does the shell not support this substring methodology?
If I run echo $- then I get “smi” as the output.
Thanks
As you probably know ESXi provides ash (NOT bash) and Busybox. Although Busybox includes ash as I recall ESXi uses a custom built executable (check where /bin/ash points or doesn't point to). The Wikipedia article on ash gives a good overview of ash and its minimalist philosopy. Shell history was originally not included and autocompletion is definitely regarded as a nice to have that didn't make the cut. Sorry to be the bearer of bad news.

In Jupyter Notebooks on Google Colab, what's the difference between using % and ! to run a shell command?

Question is in the title.
I know that % usually denotes a "magic variable" in IPython. That's not a concept I'm terribly familiar with yet, but I have read about it.
However, today I saw a tutorial where someone was using it to run a shell command. Normally I have seen and used !.
Is there a difference? Both seem to be doing the same thing when I try them.
The difference is this:
When you run a command with !, it directly executes a bash command in a subshell.
When you run a command with %, it executes one of the magic commands defined in IPython.
Some of the magic commands defined by IPython deliberately mirror bash commands, but they differ in the implementation details.
For example, running the !cd bash command does not persistently change your directory, because it runs in a temporary subshell. However, running the %cd magic command will persistently change your directory:
!pwd
# /content
!cd sample_data/
!pwd
# /content
%cd sample_data/
!pwd
# /content/sample_data
Read more in IPython: Built-in Magic Commands.

ipython magic/macro/alias guidance for invoking shell and dispatching result

(Note: I have plenty of python and unix shell experience, but fairly new to ipython -- using 7.5)
I'm trying to replicate a UNIX shell function that I use all the time, so that it works in the ipython shell.
The requirement is that I want to type something like to myproj, and then have ipython process the resulting text by doing a cd to the directory that comes back from to. (This is a quick-directory-change utility I use in unix)
The way it works in unix is that a shell function invokes an external command, that command prints its result to stdout, and the shell function then invokes the internal cd to the target dir.
I've been trying to wrap my head around %magic and macros and aliases in ipython, but so far I don't see how to get this done. Any ideas?

How to make command line tool work in windows and linux?

Making my PHP Command line application support Linux and Windows. Currently it has this code below to work from command line on Linux/unix
How can I make it work on Windows? I lan on having a setting to determine if the sytem is Linux or Windows and using the correct commands based on that but I do not know how to make these function below work in Windows
exec() is a PHP function to run stuff through the command line
exec("rm -f $dest_file", $var);
exec("mv $quant_file {$this->tmp_path}/{$src_filename}-quant.png");
You could test which platform you're on using the PHP_OS constant and run commands accordingly.
I would, however, suggest that you use the PHP provided filesystem functions (if possible).
Here are some links:
http://www.php.net/manual/en/ref.filesystem.php
http://www.php.net/manual/en/ref.dir.php