473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

os.popen('alias')

>From an interactive python shell, I execute the following:

import os
for line in os.popen('alias').readlines():
print line
No aliases are printed.

I started python from an bash environment that had many aliases
defined. I expected to see the list of aliases from within the
interactive python shell. What could I do to see those aliases defined
in the shell from where I started python?

Thanks:

Belebele

Nov 22 '05 #1
6 5449
> import os
for line in os.popen('alias').readlines():
print line
No aliases are printed.

I started python from an bash environment that had many aliases
defined. I expected to see the list of aliases from within the
interactive python shell. What could I do to see those aliases defined
in the shell from where I started python?


You can't, really. The 'alias' command is a shell built-in, not an external
command, so you can't meaningfully run it from a Python script (and any
aliases defined in your shell will probably not be available to Python).

Matters are complicated a little bit because when you use os.popen(),
your command line is actually passed to *a* shell, usually /bin/sh, so
the final command line looks like this:

/bin/sh -c 'alias'

However, even if /bin/sh is actually bash, dotfiles such as .profile and
..bashrc aren't read when using the '-c' option.

If you really want to do something to your bash aliases with python, you
could pipe them into a python command:

$ alias | python myscript.py

-- Lars

--
Lars Kellogg-Stedman <82*************@jetable.net>
This email address will expire on 2005-11-23.

Nov 22 '05 #2
> import os
for line in os.popen('alias').readlines():
print line
No aliases are printed.

I started python from an bash environment that had many aliases
defined. I expected to see the list of aliases from within the
interactive python shell. What could I do to see those aliases defined
in the shell from where I started python?


You can't, really. The 'alias' command is a shell built-in, not an external
command, so you can't meaningfully run it from a Python script (and any
aliases defined in your shell will probably not be available to Python).

Matters are complicated a little bit because when you use os.popen(),
your command line is actually passed to *a* shell, usually /bin/sh, so
the final command line looks like this:

/bin/sh -c 'alias'

However, even if /bin/sh is actually bash, dotfiles such as .profile and
..bashrc aren't read when using the '-c' option.

If you really want to do something to your bash aliases with python, you
could pipe them into a python command:

$ alias | python myscript.py

-- Lars

--
Lars Kellogg-Stedman <82*************@jetable.net>
This email address will expire on 2005-11-23.

Nov 22 '05 #3
On 2005-11-18, Belebele wrote:
From an interactive python shell, I execute the following:
import os
for line in os.popen('alias').readlines():
print line
No aliases are printed.

I started python from an bash environment that had many aliases
defined. I expected to see the list of aliases from within the
interactive python shell.


Since bash does not export aliases, they cannot be seen by a child
process.
What could I do to see those aliases defined in the shell from where
I started python?


Store them in a file before calling python, and read that file.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Nov 22 '05 #4
On 2005-11-18, Belebele wrote:
From an interactive python shell, I execute the following:
import os
for line in os.popen('alias').readlines():
print line
No aliases are printed.

I started python from an bash environment that had many aliases
defined. I expected to see the list of aliases from within the
interactive python shell.


Since bash does not export aliases, they cannot be seen by a child
process.
What could I do to see those aliases defined in the shell from where
I started python?


Store them in a file before calling python, and read that file.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Nov 22 '05 #5
On 2005-11-19, Chris F.A. Johnson wrote:
On 2005-11-18, Belebele wrote:
From an interactive python shell, I execute the following:


import os
for line in os.popen('alias').readlines():
print line
No aliases are printed.

I started python from an bash environment that had many aliases
defined. I expected to see the list of aliases from within the
interactive python shell.


Since bash does not export aliases, they cannot be seen by a child
process.
What could I do to see those aliases defined in the shell from where
I started python?


Store them in a file before calling python, and read that file.


Or redefine them as functions and use:

import os
for line in os.popen('typeset -f').readlines():
print line

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Nov 22 '05 #6
On 2005-11-19, Chris F.A. Johnson wrote:
On 2005-11-18, Belebele wrote:
From an interactive python shell, I execute the following:


import os
for line in os.popen('alias').readlines():
print line
No aliases are printed.

I started python from an bash environment that had many aliases
defined. I expected to see the list of aliases from within the
interactive python shell.


Since bash does not export aliases, they cannot be seen by a child
process.
What could I do to see those aliases defined in the shell from where
I started python?


Store them in a file before calling python, and read that file.


Or redefine them as functions and use:

import os
for line in os.popen('typeset -f').readlines():
print line

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Nov 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Belebele | last post by:
>From an interactive python shell, I execute the following: import os for line in os.popen('alias').readlines(): print line No aliases are printed. I started python from an bash...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
8
by: Steven D'Aprano | last post by:
I have a PC behind a firewall, and I'm trying to programmatically determine the IP address visible from outside the firewall. If I do this: '127.0.0.1' ('localhost.localdomain', , ) I get...
1
by: Erick Perez - Quadrian Enterprises, S.A. | last post by:
Hi, I have a MS Windows AD domain, and have one OU with more tan 1000 users objects. When I try to read it, I hit the 1000 limit of AD while returning objects, so I'm asking for advice as to how...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.