shell脚本中怎么调用python脚本中的带参函数呀
PHPz
PHPz 2017-04-18 09:16:27
0
4
318

已经可以传值了,哈哈哈!

如题呀……我是小白,求各位大神解惑!

build_using_xctool.sh
auto_send_email.py

build_using_xctool.sh 中运行 auto_send_email.py 中的 sendMessage(a,b,c,d,...) 函数,a,b,c,d,... 某些参数需要从 build_using_xctool.sh 中传过去!

事情是这样的,我在网上找了一个自动打包分发的shell脚本,然后我想在打包完成上传至fir上面之后发邮件通知自己和同事,然后又找了一个发送邮件的python脚本!

build_using_xctool.sh

#!/bin/bash

#前面的略...

python /Users/gyd/Desktop/auto_send_email.py "这是标题" "这是内容" 

auto_send_email.py

# -*- coding: UTF-8 -*-
import sys, os, re, urllib, urlparse
import smtplib
import traceback
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

password = "******"
fromaddr = "123456@qq.com"
smtpaddr = "smtp.qq.com"
toaddrs = ["654321@qq.com"]

def sendemail(title,content):
    mail_msg = MIMEMultipart()
    if not isinstance(title,unicode):
        title = unicode(title, 'utf-8')
    mail_msg['Subject'] = title
    mail_msg['From'] =fromaddr
    mail_msg['To'] = ','.join(toaddrs)
    mail_msg.attach(MIMEText(content, 'html', 'utf-8'))
    try:
        s = smtplib.SMTP_SSL("smtp.qq.com", 465)
        s.login(fromaddr,password)  #登录邮箱
        s.sendmail(fromaddr, toaddrs, mail_msg.as_string()) #发送邮件
        s.quit()
        return True

    except Exception,e:
        return flase

if sendemail(sys.argv[1], sys.argv[2]):
    print "邮件已发送!"
else:
    print "邮件发送失败!"
PHPz
PHPz

学习是最好的投资!

reply all(4)
阿神

Python can use sys.argv to get the arguments on the command line:

$ python test.py 1 2 3

test.py:

import sys

print(sys.argv)

Result:

['test.py', '1', '2', '3']

So you can schedule python like this in build_using_xctool.sh:

python /Users/gyd/Desktop/auto_send_email.py subject msg toaddrs fromaddr smtpaddr password

Then in auto_send_email.py:

import sys  # 自己 import sys
...

if __name__ == '__main__':
    sendmail(*sys.argv[1:])

Questions I answered: Python-QA

阿神

I think you need a python cookbook

伊谢尔伦

You need the third-party library sh

洪涛
#!/bin/bash

name=$1
age=$2
echo "name=${name}, age=${age}"

./a.sh 'abc' 11

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template