推荐学习书目
› 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 · May 23, 2018 · 9043 views
    This topic created in 3098 days ago, the information mentioned may be changed or developed.

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

    Supplement 1  ·  May 23, 2018

    为什么没有卵用????

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

    os.system("sudo bash -c 'cd a; ./test.sh'")
    nongmei
    nongmei
    May 23, 2018
    5
    subprocess 了解一下
    wsds
    wsds
    OP
    May 23, 2018
    6
    @ceyes 我执行的是 shell 不是系统命令
    wsds
    wsds
    OP
    May 23, 2018
    7
    @lieh222 确定
    xia0chun
    xia0chun
    May 23, 2018
    8
    直接用绝对路径不可以吗?
    walleL
    walleL
    May 23, 2018
    9
    应该是 cd test; 或者 cd test\n ?
    xiaket
    xiaket
    May 23, 2018❤️1
    10
    这种问题我已经不知道该建议你先多学下 shell 还是先多学下 python 了...
    wsds
    wsds
    OP
    May 23, 2018
    11
    @lieh222 确定
    @walleL shell 中有分号吗,没见过啊
    wsds
    wsds
    OP
    May 23, 2018
    12
    @xiaket 要会的话,我就不来这里提问了
    xiaket
    xiaket
    May 23, 2018
    13
    @wsds 根据#11 来看,你先多学下 shell 吧
    shuizhengqi
    shuizhengqi
    May 23, 2018
    14
    我第一次知道 shell 居然还有纯 shell 脚本一说
    lieh222
    lieh222
    May 23, 2018
    15
    @wsds 你要先进一个目录在执行脚本其实是两条命令,bash 多条命令需要用;分隔,你执行的是脚本需要用 bash 执行,用./的话需要在脚本第一行申明解析器,os.system 不接受标准输入,所以你的密码输不进去,可以 sudo 执行 python 脚本,这里就不需要用 sudo 了
    einvince
    einvince
    May 23, 2018
    16
    cd test && sudo ./start.sh
    princelai
    princelai
    May 23, 2018
    17
    pexpect,支持输入密码,等待加载等功能。subprocess.run()只支持一步操作,反正多个操作我没模拟成功
    ClutchBear
    ClutchBear
    May 23, 2018
    18
    绝对路径就是了
    cctv6
    cctv6
    May 23, 2018 via iPhone
    19
    &&
    virusdefender
    virusdefender
    May 23, 2018
    20
    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
    wsds
    OP
    May 23, 2018
    21
    @virusdefender 看上去很 6,学习一下
    Qzier
    Qzier
    May 23, 2018
    22
    试试

    import subprocess

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

    https://www.picb.cc/image/2bCbEa
    virusdefender
    virusdefender
    May 23, 2018
    25
    @wsds #23 ...
    natscat
    natscat
    May 23, 2018
    26
    @wsds #23 ...错误不已经报了么 类型不对。。。
    araraloren
    araraloren
    May 23, 2018
    27
    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
    araraloren
    May 23, 2018
    28
    错了还不能改。。重新发一次 :(
    #!/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
    wsds
    OP
    May 23, 2018
    29
    @virusdefender 怎么了老铁
    alvin666
    alvin666
    May 23, 2018 via Android
    30
    @wsds 哥,这水平劝你先学学英语,再学学 python,再来 v 站提问题吧。。。在这之前你的问题都可以问百度
    noFound
    noFound
    May 23, 2018
    31
    os.popen("cd /tmp/test;sudo ./start.sh").read()
    绝对路径和分号了解一下
    ipwx
    ipwx
    May 23, 2018
    32
    你先得确定 os.system 用了啥 shell。不一定是 bash
    bao3
    bao3
    May 23, 2018 via iPhone
    33
    @ipwx 无所谓,后面强制使用 bash 执行就可以了。
    wsds
    wsds
    OP
    May 23, 2018
    34
    @alvin666 不好意思,哥不会英语
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1863 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 66ms · UTC 06:05 · PVG 14:05 · LAX 22:05 · JFK 01:05
    ♥ Do have faith in what you're doing.