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
wsds
V2EX  ›  Python

Python 如何执行纯 shell 脚本

  •  
  •   wsds · 2018-05-23 09:13:53 +08:00 · 7429 次点击
    这是一个创建于 2136 天前的主题,其中的信息可能已经有所发展或是发生改变。

    查到的一些方法如 os.system(),os.popen(),os.exec() 等,都不是执行纯 shell 脚本啊,最简单的,我想通过 python 执行 shell,进入到某个文件夹中操作,该怎么操作?os.system("cd test sudo ./start.sh")没用啊

    第 1 条附言  ·  2018-05-23 10:49:29 +08:00

    为什么没有卵用????

    os.system("[ -d Pic ] &&  { rm -r Pic ; mkdir Pic; } || mkdir Pic")
    
    34 条回复    2018-05-23 21:09:43 +08:00
    ddrobot
        2
    ddrobot  
       2018-05-23 09:24:05 +08:00
    搜索“ python 切换目录”第一个搜索结果。。。os.chdir('/Users/<username>/Desktop/')
    lieh222
        3
    lieh222  
       2018-05-23 09:25:42 +08:00
    cd test; sudo ./start.sh
    sudo 需要交互输入密码,你确定要用 sudo ?
    ceyes
        4
    ceyes  
       2018-05-23 09:25:52 +08:00
    cd 不是系统命令啊,少年.

    os.system("sudo bash -c 'cd a; ./test.sh'")
    nongmei
        5
    nongmei  
       2018-05-23 09:39:20 +08:00
    subprocess 了解一下
    wsds
        6
    wsds  
    OP
       2018-05-23 09:54:45 +08:00
    @ceyes 我执行的是 shell 不是系统命令
    wsds
        7
    wsds  
    OP
       2018-05-23 09:54:52 +08:00
    @lieh222 确定
    xia0chun
        8
    xia0chun  
       2018-05-23 10:04:08 +08:00
    直接用绝对路径不可以吗?
    walleL
        9
    walleL  
       2018-05-23 10:08:07 +08:00
    应该是 cd test; 或者 cd test\n ?
    xiaket
        10
    xiaket  
       2018-05-23 10:10:45 +08:00   ❤️ 1
    这种问题我已经不知道该建议你先多学下 shell 还是先多学下 python 了...
    wsds
        11
    wsds  
    OP
       2018-05-23 10:12:33 +08:00
    @lieh222 确定
    @walleL shell 中有分号吗,没见过啊
    wsds
        12
    wsds  
    OP
       2018-05-23 10:12:45 +08:00
    @xiaket 要会的话,我就不来这里提问了
    xiaket
        13
    xiaket  
       2018-05-23 10:14:00 +08:00
    @wsds 根据#11 来看,你先多学下 shell 吧
    shuizhengqi
        14
    shuizhengqi  
       2018-05-23 10:18:00 +08:00
    我第一次知道 shell 居然还有纯 shell 脚本一说
    lieh222
        15
    lieh222  
       2018-05-23 10:19:44 +08:00
    @wsds 你要先进一个目录在执行脚本其实是两条命令,bash 多条命令需要用;分隔,你执行的是脚本需要用 bash 执行,用./的话需要在脚本第一行申明解析器,os.system 不接受标准输入,所以你的密码输不进去,可以 sudo 执行 python 脚本,这里就不需要用 sudo 了
    einvince
        16
    einvince  
       2018-05-23 10:20:13 +08:00
    cd test && sudo ./start.sh
    princelai
        17
    princelai  
       2018-05-23 10:39:38 +08:00
    pexpect,支持输入密码,等待加载等功能。subprocess.run()只支持一步操作,反正多个操作我没模拟成功
    ClutchBear
        18
    ClutchBear  
       2018-05-23 10:49:57 +08:00
    绝对路径就是了
    cdlnls
        19
    cdlnls  
       2018-05-23 10:52:10 +08:00 via iPhone
    &&
    virusdefender
        20
    virusdefender  
       2018-05-23 11:15:08 +08:00
    def run_sh(command, strict=True):
    cmd = [b"bash"]
    if strict:
    cmd.append(b"-e")
    return subprocess.run(cmd, input=command.encode("utf-8"), stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=strict)
    wsds
        21
    wsds  
    OP
       2018-05-23 11:52:02 +08:00
    @virusdefender 看上去很 6,学习一下
    Qzier
        22
    Qzier  
       2018-05-23 12:02:29 +08:00
    试试

    import subprocess

    subprocess.call("[ -d Pic ] && { rm -r Pic ; mkdir Pic; } || mkdir Pic", shell=True)
    wsds
        23
    wsds  
    OP
       2018-05-23 12:42:04 +08:00
    @virusdefender 老铁,调不通啊,这个,是缩进不对吗?
    TypeError: a bytes-like object is required, not 'str'

    https://www.picb.cc/image/2bCbEa
    virusdefender
        25
    virusdefender  
       2018-05-23 12:57:20 +08:00
    @wsds #23 ...
    natscat
        26
    natscat  
       2018-05-23 13:27:19 +08:00
    @wsds #23 ...错误不已经报了么 类型不对。。。
    araraloren
        27
    araraloren  
       2018-05-23 16:58:19 +08:00
    shell 脚本就是 bash/zsh/xsh 来执行的

    #!/usr/bin/perl6
    # your code goes here

    my \bash = Proc::Async.new: "/usr/bin/env", "bash", :w;

    react {
    --whenever bash.stdout.lines {
    ----.say;
    --}
    --whenever bash.start { }
    --whenever IN.lines {
    ----bash.say("s:g/^(\s+)/{ "--" x $0.codes }/; .say");
    ----bash.close-stdin if s:g/^(\s+)/{ "--" x $0.codes }/; .say eq "exit";
    --}
    }

    try it online:  https://ideone.com/wvp1ue
    araraloren
        28
    araraloren  
       2018-05-23 17:00:58 +08:00
    错了还不能改。。重新发一次 :(
    #!/usr/bin/perl6
    # your code goes here

    my \bash = Proc::Async.new: "/usr/bin/env", "bash", :w;

    react {
    --whenever bash.stdout.lines {
    ----.say;
    --}
    --whenever bash.start { }
    --whenever IN.lines {
    ----bash.say("$_");
    ----bash.close-stdin if $_ eq "exit";
    --}
    }
    wsds
        29
    wsds  
    OP
       2018-05-23 19:13:46 +08:00
    @virusdefender 怎么了老铁
    alvin666
        30
    alvin666  
       2018-05-23 19:30:26 +08:00 via Android
    @wsds 哥,这水平劝你先学学英语,再学学 python,再来 v 站提问题吧。。。在这之前你的问题都可以问百度
    noFound
        31
    noFound  
       2018-05-23 19:36:01 +08:00
    os.popen("cd /tmp/test;sudo ./start.sh").read()
    绝对路径和分号了解一下
    ipwx
        32
    ipwx  
       2018-05-23 19:56:51 +08:00
    你先得确定 os.system 用了啥 shell。不一定是 bash
    bao3
        33
    bao3  
       2018-05-23 20:48:43 +08:00 via iPhone
    @ipwx 无所谓,后面强制使用 bash 执行就可以了。
    wsds
        34
    wsds  
    OP
       2018-05-23 21:09:43 +08:00
    @alvin666 不好意思,哥不会英语
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1909 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 16:25 · PVG 00:25 · LAX 09:25 · JFK 12:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.