473,398 Members | 2,403 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,398 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 5451
> 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.