简体   繁体   English

从 python 脚本中获取当前的 shell 类型

[英]Getting the current shell type from python script

My script sets some environment varrible, to be used by the Makefiles, so since every shell, bash(export to set varriable) and tcsh(setenv), I need to get the correct shell type, from where we have run the python, I have tried using $SHELL varrriable, but it always given me sh as my shell. My script sets some environment varrible, to be used by the Makefiles, so since every shell, bash(export to set varriable) and tcsh(setenv), I need to get the correct shell type, from where we have run the python, I曾尝试使用 $SHELL varrriable,但它总是给我 sh 作为我的 shell。

Is there a way, I can get the right shell type from a python script or will it always output "SH" as the $SHELL variable.有没有办法,我可以从 python 脚本中获得正确的 shell 类型,或者它总是 output “SH” 作为 $SHELL 变量。

Sorry to bring this back from the dead but I just did this myself. 很抱歉从死里复活,但我自己也做了。 The following should give you the path to your current shell: 以下内容应该为您提供当前shell的路径:

from os import environ
print(environ['SHELL'])

You should be able to replace shell with any environment variable you're looking for. 您应该能够将shell替换为您正在寻找的任何环境变量。

For the case that a user has launched a shell of a different type from their login shell, and then invoked your Python script in that shell, you need to look at the executable used to run your script's parent process: For the case that a user has launched a shell of a different type from their login shell, and then invoked your Python script in that shell, you need to look at the executable used to run your script's parent process:

import os
os.path.realpath(f'/proc/{os.getppid()}/exe')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM