Hi all. I just installed Ubuntu and I'm learning how to use the bash
shell. Aside from the normal commands you can use, I was wondering if
it's possible to use Python from the terminal instead of the normal bash
commands (e.g. print instead of echo). My main reason for asking is that
I like using Python for everything, and if I don't need to learn the
bash 'language', then I won't just yet.
Thanks. 16 3870
On Mon, 2006-08-07 at 21:03 -0400, John Salerno wrote:
Hi all. I just installed Ubuntu and I'm learning how to use the bash
shell. Aside from the normal commands you can use, I was wondering if
it's possible to use Python from the terminal instead of the normal bash
commands (e.g. print instead of echo).
>From a terminal window typing,
python -c "print 'testing'"
would produce the same results as
echo 'testing'
in bash shell, but obviously not as nice.
My main reason for asking is that
I like using Python for everything, and if I don't need to learn the
bash 'language', then I won't just yet.
Just type python and do your stuff in the python interpreter. This
becomes your shell.
This then got me thinking, how about making python your login shell. So
I modified my shell login entry in /etc/passwd to /usr/bin/python,
logged in and voila, I was presented with my friendly python shell upon
opening a terminal window.
So the answer is "Yes".
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal bash commands (e.g. print instead of echo).
Take a look at ipython <http://ipython.scipy.org/>. It's not precisely what
you've asked for, but it provides some features that help integrate Python
with the shell environment.
Skip sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal bash commands (e.g. print instead of echo).
Take a look at ipython <http://ipython.scipy.org/>. It's not precisely what
you've asked for, but it provides some features that help integrate Python
with the shell environment.
Skip
FWIW, I second this. If you want to use python as your shell use
Ipython. It's just great. Fernando Pérez has done a Good Thing IMHO.
Be sure to read (or at least skim) the manual: http://ipython.scipy.org/doc/manual/index.html
Here's an example of calling "man ls" and then getting the results of
"ls -l" into a python list (one list item per line) and then extracting
the size (as a string) of the MANIFEST file... Enjoy... (I hope
reformatting doesn't screw this up too badly.)
$ ipython
Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
Type "copyright", "credits" or "license" for more information.
IPython 0.7.1.fix1 -- An enhanced Interactive Python.
? -Introduction to IPython's features.
%magic -Information about IPython's 'magic' % functions.
help -Python's own help system.
object? -Details about 'object'. ?object also works, ?? prints more.
In [1]: !man ls
Reformatting ls(1), please wait...
<viewing actual man page here. ~Simon>
In [2]: !!ls -l
Out[2]:
['total 40',
'drwxr-xr-x 3 sforman sforman 4096 2006-08-02 12:41 docs',
'drwxr-xr-x 3 sforman sforman 4096 2006-04-27 23:49 logs',
'-rw-r--r-- 1 sforman sforman 887 2006-04-27 23:49 MANIFEST',
'-rw-r--r-- 1 sforman sforman 173 2006-05-04 14:04 MANIFEST.in',
'-rw-r--r-- 1 sforman sforman 3338 2006-06-14 20:19 README',
'-rw-r--r-- 1 sforman sforman 475 2006-07-16 17:40 setup.py',
'drwxr-xr-x 4 sforman sforman 4096 2006-08-07 15:04 src',
'drwxr-xr-x 3 sforman sforman 4096 2006-06-21 20:59 tests',
'-rw-r--r-- 1 sforman sforman 898 2006-07-09 21:53 TODO',
'drwxr-xr-x 3 sforman sforman 4096 2006-08-05 17:37 util']
In [3]: data = [n.split() for n in _[1:]]
In [4]: print data[2][4]
887
In [5]:
etc...
HTH,
~Simon
John Salerno wrote:
I like using Python for everything, and if I don't need to learn the
bash 'language', then I won't just yet.
And like vim, Ipython works on both windows and ubuntu.
rd sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal bash commands (e.g. print instead of echo).
Take a look at ipython <http://ipython.scipy.org/>. It's not precisely what
you've asked for, but it provides some features that help integrate Python
with the shell environment.
Skip
Can you use IPython to do normal bash things, like installing, etc.?
"John Salerno" <jo******@NOSPAMgmail.comwrote in message
news:44**********************@news.astraweb.com...
Hi all. I just installed Ubuntu and I'm learning how to use the bash
shell. Aside from the normal commands you can use, I was wondering if
it's possible to use Python from the terminal instead of the normal bash
commands (e.g. print instead of echo). My main reason for asking is that
I like using Python for everything, and if I don't need to learn the
bash 'language', then I won't just yet.
The answer is yes and you are getting excellent tips from others.
I am just validating your experience by saying that coming from Python is a
barrier to my learning BASH. The more I work with Linux/BASH, the more I
see how I might have used BASH to script something I have already done in
Python. But the question that always comes up is why bother with BASH at
all ;-) And with Python available everywhere, ....
I've just reconsiled to pick up my BASH by osmosis and concentrate on
(much!) cleaner scripting with Python.
Thomas Bartkus
Thomas Bartkus wrote:
I am just validating your experience by saying that coming from Python is a
barrier to my learning BASH. The more I work with Linux/BASH, the more I
see how I might have used BASH to script something I have already done in
Python. But the question that always comes up is why bother with BASH at
all ;-) And with Python available everywhere, ....
I've just reconsiled to pick up my BASH by osmosis and concentrate on
(much!) cleaner scripting with Python.
Glad to know I'm not alone! But I'm certainly much happier using Python
than bash anyway. All I really want to learn as far as the shell is
concerned are the 'natural' things you can do, such as directory/file
manipulation, installing programs, etc. I don't have much of a need to
learn the actual programming parts of the bash language, especially when
I can do the same with Python, which is so much cooler. :)
>I've just reconsiled to pick up my BASH by osmosis and concentrate on (much!) cleaner scripting with Python.
JohnI don't have much of a need to learn the actual programming parts
Johnof the bash language, ...
As long as you promise never, ever, ever to try programming in csh...
Skip sk**@pobox.com wrote:
>I've just reconsiled to pick up my BASH by osmosis and concentrate on
>(much!) cleaner scripting with Python.
JohnI don't have much of a need to learn the actual programming parts
Johnof the bash language, ...
As long as you promise never, ever, ever to try programming in csh...
Skip
Heh heh, Python all the way!
John Salerno wrote:
sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal bash commands (e.g. print instead of echo).
Take a look at ipython <http://ipython.scipy.org/>. It's not precisely what
you've asked for, but it provides some features that help integrate Python
with the shell environment.
Skip
Can you use IPython to do normal bash things, like installing, etc.?
"normal bash things"? :-) Yes, most commands can be run by putting an
'!' before them. If you ever need to run something that for some
reason doesn't work with this, you can always run !bash and do it in
bash. :-)
Peace,
~Simon
Simon Forman wrote:
"normal bash things"? :-)
forgive my ignorance, i just installed linux on saturday! :)
John Salerno <jo******@NOSPAMgmail.comwrote:
> Can you use IPython to do normal bash things, like installing, etc.?
Most scripts on Linux have a "hash-bang" line as their first line:
#! /bin/sh
When you execute that script, the system knows that it has to load sh or
bash to process it, regardless of what program launched the script. The
same thing works for Python scripts:
#! /usr/bin/python
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
On Tue, Aug 08, 2006 at 12:22:42PM EDT, Simon Forman wrote:
John Salerno wrote:
sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal bash commands (e.g. print instead of echo).
>
Take a look at ipython <http://ipython.scipy.org/>. It's not precisely what
you've asked for, but it provides some features that help integrate Python
with the shell environment.
>
Skip
Can you use IPython to do normal bash things, like installing, etc.?
"normal bash things"? :-) Yes, most commands can be run by putting an
'!' before them. If you ever need to run something that for some
reason doesn't work with this, you can always run !bash and do it in
bash. :-)
Sorry, I'm late with my howmework .. but this is rather cool.
Although .. prefixing all your "system commands" with the far-to-reach
"!" would probably become a royal pain after a while.
Why do you write "most commands" .. what type of command might not be
run by "putting '!' before them?"
In the linux world it would be rather interesting if a distro was
available that uses nothing but python.
The installer .. the startup scripts .. utilities .. etc.
Maybe there is such a thing and I'm just not aware of it?
Since it would be byt-code being executed it would be faster than
regular shell stuff and a lot easier to customize/maintain.
Don't know enough to figure out if such a thing is possible, though ..
Thanks
cga
cga2000 wrote:
On Tue, Aug 08, 2006 at 12:22:42PM EDT, Simon Forman wrote:
John Salerno wrote:
sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal bash commands (e.g. print instead of echo).
Take a look at ipython <http://ipython.scipy.org/>. It's not precisely what
you've asked for, but it provides some features that help integrate Python
with the shell environment.
Skip
>
Can you use IPython to do normal bash things, like installing, etc.?
"normal bash things"? :-) Yes, most commands can be run by putting an
'!' before them. If you ever need to run something that for some
reason doesn't work with this, you can always run !bash and do it in
bash. :-)
Sorry, I'm late with my howmework .. but this is rather cool.
Although .. prefixing all your "system commands" with the far-to-reach
"!" would probably become a royal pain after a while.
I use my computer almost exclusively for programming, so I used xmodmap
to remap the number & symbol keys the other way round, i.e. I get
"!@#$%^&*()" from just pressing the keys and "1234567890" when I press
shift+keys. It's soooo much nicer (except when I use another
machine...) So hitting '!' for me at least ain't so bad. ;P YMMV
I don't actually use Ipython as my shell, but I'd *like* to..
>
Why do you write "most commands" .. what type of command might not be
run by "putting '!' before them?"
Well, I wrote that just to hedge my bets, but here's an example: I set
up lesspipe and source-highlight to add syntax highlighting to various
file types when viewed through less. But using "!less neat.py" in
Ipython doesn't work because (I'm guessing here) Ipython doesn't handle
the color escape codes. It winds up looking like this:
ESC[31m#!/usr/local/bin/python2.5ESC[m
ESC[01;34mfromESC[m random ESC[01;34mimportESC[m random as f
ESC[01;34mdefESC[m
ESC[01;30mgESC[mESC[31m(ESC[mESC[31m)ESC[mESC[31m:ESC[m
n1 ESC[31m=ESC[m ESC[01;30mfESC[mESC[31m(ESC[mESC[31m)ESC[m
ESC[01;34mwhileESC[m TrueESC[31m:ESC[m
n2 ESC[31m=ESC[m ESC[01;30mfESC[mESC[31m(ESC[mESC[31m)ESC[m
yield n1 ESC[31m-ESC[m n2
n1 ESC[31m=ESC[m n2
Ew.
So in this case I'd have to "!bash" then "less neat.py"...
In the linux world it would be rather interesting if a distro was
available that uses nothing but python.
The installer .. the startup scripts .. utilities .. etc.
Maybe there is such a thing and I'm just not aware of it?
IMHO it would be neat, but it'd be kind of a "stunt". There's a ton
of excellent code in most any linux distro *not* written in python.
Since it would be byt-code being executed it would be faster than
regular shell stuff and a lot easier to customize/maintain.
Mmmm... I bet it'd be hard to beat, say, grep... Or any of the
small, custom-purpose C-coded tools. (then there's make... gcc...
not easy to rewrite those.. just off the top of my head...)
>
Don't know enough to figure out if such a thing is possible, though ..
Thanks
cga
Peace,
~Simon
On Tue, Aug 15, 2006 at 06:45:33PM EDT, Simon Forman wrote:
cga2000 wrote:
[..]
Why do you write "most commands" .. what type of command might not be
run by "putting '!' before them?"
Well, I wrote that just to hedge my bets, but here's an example: I set
up lesspipe and source-highlight to add syntax highlighting to various
file types when viewed through less. But using "!less neat.py" in
Ipython doesn't work because (I'm guessing here) Ipython doesn't handle
the color escape codes. It winds up looking like this:
ESC[31m#!/usr/local/bin/python2.5ESC[m
ESC[01;34mfromESC[m random ESC[01;34mimportESC[m random as f
ESC[01;34mdefESC[m
ESC[01;30mgESC[mESC[31m(ESC[mESC[31m)ESC[mESC[31m:ESC[m
n1 ESC[31m=ESC[m ESC[01;30mfESC[mESC[31m(ESC[mESC[31m)ESC[m
ESC[01;34mwhileESC[m TrueESC[31m:ESC[m
n2 ESC[31m=ESC[m ESC[01;30mfESC[mESC[31m(ESC[mESC[31m)ESC[m
yield n1 ESC[31m-ESC[m n2
n1 ESC[31m=ESC[m n2
Ew.
So in this case I'd have to "!bash" then "less neat.py"...
you could use "vim -R" -- or its alter ego "view" ..
"!view neat.py"
I tried that just now under ipython and I end up with the exact same
syntax highlighting that I have when I run vim from the bash prompt.
The problems I am having are mainly due to my bash customization --
aliases, functions .. all that stuff is no longer being recognized ..
and also some bash built-ins such as "cd" do not do anything .. maybe
ipython starts a subprocess that switches and then immediately exits ..
so when I get the prompt back I'm back where I started.
I guess reading the ipython manual and trying to understand what I'm
doing before going any further wouldn't hurt..
:-)
>
In the linux world it would be rather interesting if a distro was
available that uses nothing but python.
The installer .. the startup scripts .. utilities .. etc.
Maybe there is such a thing and I'm just not aware of it?
IMHO it would be neat, but it'd be kind of a "stunt". There's a ton
of excellent code in most any linux distro *not* written in python.
quite a large project, I would imagine.
Since it would be byt-code being executed it would be faster than
regular shell stuff and a lot easier to customize/maintain.
Mmmm... I bet it'd be hard to beat, say, grep... Or any of the
small, custom-purpose C-coded tools. (then there's make... gcc...
not easy to rewrite those.. just off the top of my head...)
Sorry .. couldn't think of a better word .. but by "utilities" I meant
all those shell scripts in /usr/bin that come with gnu/linux distros and
mostly appear to front-end "real" programs.
I guess a python "system shell" could invoke all the C-coded stuff the
same way bash does, no..?
'nuff [OT] for now..
Thanks,
cga
In <ma***************************************@python. org>, cga2000 wrote:
and also some bash built-ins such as "cd" do not do anything .. maybe
ipython starts a subprocess that switches and then immediately exits ..
so when I get the prompt back I'm back where I started.
If you put a ``!`` in front of ``cd`` this is what happens. Just leave
out the ``!``. IPython has it's own built-in ``cd`` command.
Ciao,
Marc 'BlackJack' Rintsch This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jorgen Grahn |
last post by:
I couldn't think of a good solution, and it's hard to Google for...
I write python command-line programs under Win2k, and I use the bash shell
from Cygwin. I cannot use Cygwin's python package...
|
by: Robin Siebler |
last post by:
I have a bunch of command line tests that I need to automate. I've
run into too many bugs with the default Win2k command shell, so I need
to either purchase 4NT or handle the logic and output...
|
by: Rochester |
last post by:
Hi,
I just found out that the general open file mechanism doesn't work
for named pipes (fifo). Say I wrote something like this and it
simply hangs python:
#!/usr/bin/python
import os
|
by: Frank Potter |
last post by:
I learned some python in windows.
And now I've turned to linux.
I read a book and it teaches how to write shell script with bash,
but I don't feel like the grammar of bash.
Since I know about...
|
by: Endless Story |
last post by:
My last version of Python was 2.4, running smoothly on XP with path c:
\Python24 - no need even to include this path in PATH; everything
worked as it's supposed to at the command line.
Just...
|
by: Stephen Cattaneo |
last post by:
Hello all,
I am attempting to execute an automated test (written in Python) via
cron. I have to check the HOSTNAME variable as part of the test, oddly
under cron the HOSTNAME environment...
|
by: Cameron Simpson |
last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote:
Because $HOSTNAME is a bash specific variable, set by bash but NOT
EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
|
by: james.kirin39 |
last post by:
Hi everyone,
After having used Python on Linux for some time, I now have to do
Python coding on Windows. I am big fan of the interactive Python shell
to test, eg, regexps.
Is there an...
|
by: Frantisek Malina |
last post by:
What is the best way to do the regular bash commands in native python?
- create directory
- create file
- make a symlink
- copy a file to another directory
- move a file
- set permissions
...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |