How can I run a shell command ( written in Vim ) using Ipython? [duplicate] - shell

Is there a way to use "magic commands" from IPython from an outside file? For example if I have a file, "rcode.py" with the code:
%load_ext rmagic
%R a=c(1,2,3);b=c(2,3,4);print(summary(lm(a~b)))
This gives me a SyntaxError for the first line when I run it using ipython rcode.py in the command line. However when I type these lines straight into the interactive shell with ipython it runs fine. Is this because you only do magic in the interactive shell?
Thanks!

If you name your file with a .ipy extension, ipython will parse it properly. You can simply make a symlink if you want:
$ ln -s rcode.py rcode.ipy
$ ipython rcode.ipy

Related

zshell command named "prompt" in macOS?

I'm using the zshell for macOS Catalina (10.15.6). The book "Learning Shell Scripting With Zsh" describes an executable command named prompt, usable in such forms as:
$ prompt -p
$ prompt -h
$ prompt <theme_name>
$ prompt adam1 red yellow magenta
But zsh report "command not found: prompt".
How do I obtain that command, or an equivalent one.
(Everywhere I've tried to search about this just gives me links to places explaining how to set the zsh prompt, with all the usual escape sequences, "$..." symbols, etc.)
From man zshcontrib:
PROMPT THEMES
Installation
You should make sure all the functions from the Functions/Prompts
directory of the source distribution are available; they all begin with
the string `prompt_' except for the special function`promptinit'. You
also need the `colors' and `add-zsh-hook' functions from Func-
tions/Misc. All these functions may already be installed on your sys-
tem; if not, you will need to find them and copy them. The directory
should appear as one of the elements of the fpath array (this should
already be the case if they were installed), and at least the function
promptinit should be autoloaded; it will autoload the rest. Finally,
to initialize the use of the system you need to call the promptinit
function. The following code in your .zshrc will arrange for this;
assume the functions are stored in the directory ~/myfns:
fpath=(~/myfns $fpath)
autoload -U promptinit
promptinit
So, to get the prompt command, you can just run
$ autoload -U promptinit
$ promptinit

Bash script works in Terminal but in Applescript gives "python3: command not found"

SOLVED: see solution in replies
I have a bash script that looks like this:
#!/bin/bash
python3 /Users/me/path/to/my/file.py
python3 /Users/me/path/to/my/file2.py
Rscript /Users/me/path/to/my/rfile.R
python3 /Users/me/path/to/my/file.py
When I execute it from terminal with the command
bash /Users/me/path/to/my/shellscript.sh
it executes perfectly. However, I would like to be able to run it from Keyboard Maestro or AppleScript. In AppleScript, when I run
do shell script "bash /Users/me/path/to/my/shellscript.sh"
I get the following error message:
error "/Users/me/path/to/my/shellscript.sh: line 2: python3: command not found
/Users/me/path/to/my/shellscript.sh: line 3: python3: command not found
/Users/me/path/to/my/shellscript.sh: line 4: Rscript: command not found
/Users/me/path/to/my/shellscript.sh: line 5: python3: command not found" number 127
I receive the same error message if I run the equivalent command in Keyboard Maestro. I have tried changing python3 to python, but it fails anyway because it runs the files in Python 2 (despite the shebang of the Python files specifying Python 3), and the Rscript command would surely still throw an error.
What is the difference between the environments that causes the shell script to fail in Keyboard Maestro and AppleScript but not Terminal?
I got it to work by changing the command to PATH=$PATH:/usr/local/bin; bash /Users/me/path/to/my/shellscript.sh.

Bash cell magic in IPython script

This is my first post on SO, so please let me know if the problem is not well defined. I have a script process.ipy in which I am trying to implement a cell magic as follows,
#!/usr/bin/env ipython
%%bash
ls
When I run this on the command line (Ubuntu and zsh shell), I get the following error,
$ ipython process.ipy
File "<ipython-input-1-f108be8d32f2>", line 3
%%bash
^
SyntaxError: invalid syntax
However, I can run this in the ipython session without a problem,
In [1]: %%bash
...: ls
...:
process.ipy
More confusing, is that the single line version works in the script,
#!/usr/bin/env ipython
!ls
What am I doing wrong? Is something not setup correctly?
Short Answer: magic commands are understood only in an IPython interactive session, not in scripts.
Longer Answer: they can be called in scripts, but only using a library call, not the %% notation as described in How to run an IPython magic from a script (or timing a Python script). Note that the mechanism is IPython version dependent.
You do not need the #!/usr/bin/env ipython line at the top.
The error :
%%bash
ls
You do not need the #!/usr/bin/env ipython line at the top.
The cell magic should be first line in the cell.
just try the following and it should work
%%bash
ls

Cygwin Terminal and zsh strange characters used in username

Hi I've recently installed zsh using cygwin on my Windows machince but when I type zsh to start this I get the following:
GG#GG-PC ~
$ zsh
\[\e]0;\w\a\]\n\[\e[32m\]\u#\h \[\e[33m\]\w\[\e[0m\]\n\$
On my mac I am using iTerm2 and this is so much easier to setup on here. Also I am having trouble in setting up the aliases and this is becauses its not setup properly in terms of config file where I can set this up in a separate file.
Any ideas how I can resolve?
It looks like zsh is inheriting the value of PS1 from the previous shell. The PS1 environment variable sets the shell prompt, and zsh used a different format for prompt substitutions than other shells. Try entering the following command after you start zsh:
PS1=$'%{\e]0;%d\a%}\n%F{green}%n#%m %F{yellow}%d%f\n%# '
If that works, add that line to your ~/.zshrc file.
That's also probably a good place to put your aliases.
There might be an issue because you launch zsh from bash actually and not cygwin.
One thing you can do is to launch zsh as the starting shell of mintty (the window that wraps your shell)
Create a shortcut with this inside:
c:\<cygwin-folder>\bin\mintty.exe -i /Cygwin-Terminal.ico /usr/bin/zsh --login -
Yo need to update .zshrc with your required theme and then
source .zshrc

Using a #! comment in ruby running in Ubuntu

I am new to programming and am trying to follow an example which uses #! comment in ruby.
I am consistently get the message:
bash: matz.rb: command not found
I am using this comment:
#! /usr/bin/env ruby
I have tried it with and without the space after ! as well as with and without the env.
when I use the
$ which ruby
ruby is in: /usr/bin/ruby
I have also gone into the operating system and changed the permissions on the file matz.rb to rwx for all users w/ no effect. Am I doing something wrong or do I have my system set up incorrectly?
The /usr/bin/env part is fine. You need to give bash the path to matz.rb when you run it. If you're in the directory with matz.rb, type "./matz.rb". The directory "." means the current directory - bash doesn't look there by default when running programs (like Windows does).
The env program (/usr/bin/env) searches the executable search path - the PATH environment variable - for the ruby program as if you typed it at the command prompt, and runs that program. The shebang comment doesn't do this. If you want to give your script to other people who might not have ruby installed in the same place as you, then you should use the "#!/usr/bin/env ruby" comment so that it will work as long as they can run ruby by typing "ruby".
If you're in the same directory as the matz.rb file, be sure to run it as
$ ./matz.rb
and not just
$ matz.rb
Here's a shell session demonstrating this working:
$ ls -la m*
-rwxr-xr-x 1 gareth gareth 32 8 Jan 08:46 matz.rb
$ cat matz.rb
#!/usr/bin/env ruby
puts "Matz"
$ matz.rb
-bash: matz.rb: command not found
$ ./matz.rb
Matz
Your file wasn't created on Windows was it? If it has \r\n line endings, that will upset bash. You can open it with Vim and check:
vi matz.rb
:set ff=unix
:wq
If when you tab-complete the "ff=" part it says dos, then it has the wrong file format. Alternatively, run dos2unix and try to run the file again:
apt-get install sysutils
dos2unix matz.rb
It sounds like you're on a Unix/Linux system and just typing matz.rb on the command line. If you're trying to execute a command in the current directory, you need to call it like ./matz.rb. The "./" tells it to look in the current directory rather than just /usr/bin and friends.
I failed to see any answer indicating you to change the executable mode of the file, so you might wanna try and do
chmod +x matz.rb
before you go and try doing
./matz.rb
Also it might be better not to attach a .rb extension to the file, such is the case for normal ruby / rails scripts e.g. script/generate, script/console etc.
You can use the 'shebang' line with either:
#!/usr/bin/ruby
#!/usr/bin/env ruby
But the script needs to be executable (you indicated it is) and in your shell $PATH.
echo $PATH
Put the script in one of those directories, or modify your path, otherwise specify the full path to it, for example:
export PATH=$PATH:/home/user/bin
or one of these:
./matz.rb
/home/user/bin/matz.rb
You can also run the Ruby interpreter passing the script filename as an argument, and it will be executed. This is particularly useful if you have another version of Ruby installed on your system (say, for testing, like Ruby Enterprise Edition, REE):
/usr/bin/ruby matz.rb
/opt/ree/bin/ruby matz.rb
Have you tried the ShaBang as following to directly point to ruby?
#! /usr/bin/ruby
Then you call the script from the commandline as
./matz.rb
Under Unix/Linux systems the dot in front of a command to search for the command in the current directory. If you give a path like /usr/bin/ruby, it will search in the current directory for a directory called usr...
A command without a dot/ in front is searched in locations specified by the path variable of the environment.
A command with a / on the beginning is searched exactly from root following the specified path.
Inside your ShaBang, you want to specify the exact path to the interpreter so "/usr/bin/ruby" is the correct one. On the commandline, where you want your script to be executed, you need to call the script with "./matz.rb" otherwise the bash will search a command like /usr/bin/matz.rb what leads to your errormessage.

Resources