V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
vhwwls
V2EX  ›  Python

请教: Python 获取 shell 自定义变量的值。

  •  
  •   vhwwls · 2020-03-31 17:19:48 +08:00 · 3296 次点击
    这是一个创建于 1477 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如在当前 shell 进程中,创建一个自定义变量

    test=123
    

    这个时候如果在 shell 中执行

    set | grep -i test
    

    输出结果中,可以看见 test 这个变量是存在的。

    我在当前 shell 中定义了 test 这个自定义变量,同时,我启动了一个 Py 脚本,这个 Py 脚本有没有办法获取当前 shell 的自定义变量(比如这个$test )的值?

    要求:不将自定义变量导出为环境变量。

    望请各位 V 友赐教。

    9 条回复    2020-04-07 18:56:00 +08:00
    aloxaf
        1
    aloxaf  
       2020-03-31 22:01:00 +08:00
    你这需求也太扭曲了,为啥会有这种需求,原始问题是什么?

    而扭曲的需求只有用扭曲的办法解决了:gdb attach 上去找(
    aloxaf
        2
    aloxaf  
       2020-03-31 22:26:15 +08:00   ❤️ 1
    给出一个扭曲的实现: https://i.loli.net/2020/03/31/tVYNr6kWEiTfB18.png

    ```
    #!/usr/bin/env python3
    from pygdbmi.gdbcontroller import GdbController
    import os

    gdbmi = GdbController()
    gdbmi.write(f"attach {os.getppid()}")
    res = gdbmi.write('p (char *)get_string_value("foo")')

    for mes in res:
    if "$1" in str(mes.get("payload", "")):
    print(mes["payload"])
    ```
    xpresslink
        3
    xpresslink  
       2020-03-31 23:26:28 +08:00
    一个变量创建时,它不会自动地为在它之后创建的 shell 进程所知。而命令 export 可以向后面的 shell 传递变量的值。当一个 shell 脚本调用并执行时,它不会自动得到原为脚本(调用者)里定义的变量的访问权,除非这些变量已经被显式地设置为可用。export 命令可以用于传递一个或多个变量的值到任何后继脚本。 ----《 UNIX 教程》
    bwangel
        4
    bwangel  
       2020-04-01 12:31:48 +08:00
    ø> test=123 python3 main.py 12:30:06 (04-01)
    123
    5155 (lazywork) /private/tmp
    ø> python3 main.py 12:30:09 (04-01)
    None
    5156 (lazywork) /private/tmp
    ø> echo $test 12:30:24 (04-01)


    在命令执行的时候设置一个环境变量就可以了。这样设置的环境变量只会传给目标进程。
    bwangel
        5
    bwangel  
       2020-04-01 12:32:36 +08:00
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-"


    import os

    def main():
    print(os.getenv('test'))


    if __name__ == '__main__':
    main()


    "main(.)py" 就是一个很简单的读取环境变量的脚本
    vhwwls
        6
    vhwwls  
    OP
       2020-04-01 14:11:04 +08:00
    @aloxaf 原始问题是我想完全自己用 Python 实现一个和 bash 内置的 echo 命令一模一样的小工具,其实就是想锻炼一下自己的 Python 能力,因为最近在自学,谢谢兄台,受教了。
    aloxaf
        7
    aloxaf  
       2020-04-01 14:56:40 +08:00
    @vhwwls bash 中,变量展开的操作是由 shell 完成的……你直接输出自己的参数就可以了
    ps1aniuge
        8
    ps1aniuge  
       2020-04-01 16:17:03 +08:00
    ?啥玩意?说的不清不楚?想在 shell 中给 py 传值?
    shell 传的是字符串,powershell 传的是.net 强类型变量。
    powershell 可以通过管道传递强类型对象,
    可以远程传递强类型对象。

    powershell:
    $a = 1
    $b = python xxx.py $a
    cominghome
        9
    cominghome  
       2020-04-07 18:56:00 +08:00
    export $test=123 && python xx.py
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3928 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 10:34 · PVG 18:34 · LAX 03:34 · JFK 06:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.