1. shell脚本中怎么调用python脚本中的带参函数
Python 可以利用 sys.argv 拿到命令列上的 arguments:
$ python test.py 1 2 3
test.py:
import sys
print(sys.argv)
结果:
['test.py', '1', '2', '3']
所以你在 build_using_xctool.sh 中可以这样调度 python:
python /Users/gyd/Desktop/auto_send_email.py subject msg toaddrs fromaddr smtpaddr password
然后在 auto_send_email.py 中:
import sys # 自己 import sys...if __name__ == '__main__':
sendmail(*sys.argv[1:])
2. shell里面怎么调用,执行python文件
首先确保你Python的执行环境在你的path中,不过不在也没关系。
然后像普通的执行其他脚本的方式来执行。
例如:
Python代码
pyfile=...(你的py文件所在的具体路径)
/usr/local/bin/python $pyfile
3. 请教在shell中调用python并获取py返回值的问题
调用并且传入参数就不必说了吧,很简单直接python <file_name>.py <arg1> <arg2>
首先,你知道PYTHON是买有main函数的,要想读到PYTHON里面RETURN的值恐怕办不到PYTHON里面也没有类似的通道。
但是,你却可以读到PYTHON的sterr的error code。PYTHON里面有个sys模块,你可以用sys.exit(<num>)的方式,通过把错误码发送给stderr。然后再Shell里面用$?(事实上$?命令会从stderr里面去读) 来判断python是否执行成功
4. shell里面怎么调用,执行python文件
1.在shell里面写上编码方案
2.把utf-8 改成 gbk,据说win上对中文的支持以gbk为好。
3.如果还不行,整个错误的提示都贴出来
5. shell 脚本调用python自定义类并传参
| 是重定向嘛, echo 'asdfadsf' 先是将'asdfadsf'输出到标准输出也就是屏幕
然后 | 将echo的输出转到 logcat.py 作为 logcat.py 的输入,也就等于是将 'asdfadsf' 作为 logcat.py的输入参数
6. shell脚本里怎样在调用python时传参数
Python 可以利用 sys.argv 拿到命令列上的 arguments:
$ python test.py 1 2 3
test.py:
import sys
print(sys.argv)
结果:
['test.py', '1', '2', '3']
所以你在 build_using_xctool.sh 中可以这样调度 python:
python /Users/gyd/Desktop/auto_send_email.py subject msg toaddrs fromaddr smtpaddr password
然后在 auto_send_email.py 中:
import sys # 自己 import sys...if __name__ == '__main__':
sendmail(*sys.argv[1:])
7. shell脚本里调用python脚本时候怎样传参数
| 是重定向嘛, echo 'asdfadsf' 先是将'asdfadsf'输出到标准输出也就是屏幕 然后 | 将echo的输出转到 logcat.py 作为 logcat.py 的输入,也就等于是将 'asdfadsf' 作为 logcat.py的输入参数
8. 如何用python通过163发送邮件
# 导入 smtplib 和 MIMEText
import smtplib
from email.mime.text import MIMEText
# 定义发送列表
mailto_list=["[email protected]","[email protected]"]
# 设置服务器名称、用户名、密码以及邮件后缀
mail_host = "smtp.126.com"
mail_user = "user"
mail_pass = "pass"
mail_postfix="126.com"
# 发送邮件函数
def send_mail(to_list, sub, context):
'''''
to_list: 发送给谁
sub: 主题
context: 内容
send_mail("[email protected]","sub","context")
'''
me = mail_user + "<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(context)
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
send_smtp = smtplib.SMTP()
send_smtp.connect(mail_host)
send_smtp.login(mail_user, mail_pass)
send_smtp.sendmail(me, to_list, msg.as_string())
send_smtp.close()
return True
except (Exception, e):
print(str(e))
return False
if __name__ == '__mian__':
if (True == send_mail(mailto_list,"subject","context")):
print ("测试成功") www.codesky.net
else:
print ("测试失败")
9. 怎么样在shell脚本中调用python脚本
1、os.system(cmd)
缺点:不能获取返回值
2、os.popen(cmd)
要得到命令的输出内容,只需再调用下read()或readlines()等
例:a=os.popen(cmd).read()
3、commands模块,其实也是对popen的封装。
此模块主要有如下方法:
commands.getstatusoutput(cmd)返回(status, output).
commands.getoutput(cmd)只返回输出结果
commands.getstatus(file)返回ls -ld file的执行结果字符串,调用了getoutput
例:
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x1 root13352 Oct 141994 /bin/ls'
来源:麦子学院
10. 如何使用linux shell发送Email邮件
方法很多呀,mail命令就可以实现,这个命令在很多包里都有,例如sendmail