【shell】 shell调用python脚本,并且向python脚本传递参数_shell脚本调用python脚本_云川之下的博客-CSDN博客

使用运行python程序

1
2
3
4
cd \d:
cd \PyShell
python SDKts.py
exec /bin/bash

向python传参

1
2
3
4
5
cd \d:
cd \PyShell
para="D:\\collection\\1.png"
python SDKts.py ${para}
exec /bin/bash

向shell传参

Python脚本和Shell脚本之间的参数传递_Micro Will的博客-CSDN博客 在python中print就能在shell中echo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


# -*- coding: utf-8 -*-
import oss2
import re

import os
import sys
# 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
auth = oss2.Auth('LTAI5tSUsnok7MdqfhSmyjz2', 'qbq8fdJXbjsJl2RknzTZCsN1gDathu')
# yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
# 填写Bucket名称。
bucket = oss2.Bucket(auth, 'https://oss-cn-shanghai.aliyuncs.com', 'bzjh')

def main(para):

regex = re.compile(r"\\")
name = para
name = regex.sub('-', name)
regex = re.compile(r"\:")
name = regex.sub('', name)
bucket.put_object_from_file('Typora_img/'+name , para)
url = "https://bzjh.oss-cn-shanghai.aliyuncs.com/Typora_img/" + name
print("Upload Success:\n")
print(url)
return url

# 通过sys.argv获得入参
#
main (sys.argv[0])

错误代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cd \d:
cd \PyShell
arr=()
#para="D:\\collection\\1.png"
for img_path in "$@"
#echo Upload Succseefully
do
r=`echo python SDKts.py ${img_path}`
arr[${#arr[@]}]=$r
done
echo 'Upload Success:'
for a in "${arr[@]}"
do
echo $a
done
exec /bin/bash
循环体上传两张图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
cd \d:
cd \PyShell
arr=("D:\\collection\\1.png" "D:\\collection\\5.png")
para="D:\\collection\\1.png"
for a in "${arr[@]}"
do
r=`echo python SDKts.py $a`
echo $r
done
exec /bin/bash
````
```shell
cd \d:
cd \PyShell
arr=()
#para="D:\\collection\\1.png"
for img_path in "$@"
#echo Upload Succseefully
do
r=`echo python SDKts.py ${img_path}`
arr[${#arr[@]}]=$r
done
if [ ${#arr[@]} -eq $# -a $# -gt 0 ]
then
echo 'Upload Success:'
for a in "${arr[@]}"
do
echo $a
done
else
echo 'Upload Failed'
fi

1
2
3
4
5
6
7
8
file_path='D:\PyShell\PyShell SDKts.py'
arr=("D:\\collection\\1.png" "D:\\collection\\2.png")
for a in "${arr[@]}"
do
r=`$file_path$a`
echo $r
done
exec /bin/bash
1
2
3
4
5
6
7
8
file_path='D:\PyShell\SDKts.py'
arr=("D:\\collection\\1.png" "D:\\collection\\2.png")
for a in "${arr[@]}"
do
r=`${file_path} $a`
echo $r
done
exec /bin/bash
成功运行
1
2
3
4
5
6
7
8
file_path='D:\PyShell\SDKts.py'
arr=("D:\\collection\\1.png" "D:\\collection\\2.png")
for a in "${arr[@]}"
do
r=`echo ${file_path} $a`
echo $r
done
exec /bin/bash
成功运行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
set "D:\\collection\\1.png" "D:\\collection\\2.png"
file_path='D:\PyShell\SDKts.py'
arr=()
for a in "$@"
do
r=`echo ${file_path} $a`
arr[${#arr[@]}]=$r
done
echo 'Upload Success:'
for b in "${arr[@]}"
do
echo $b
done
exec /bin/bash
封装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fun()
{
file_path='D:\PyShell\SDKts.py'
arr=()
for a in "$@"
do
r=`echo ${file_path} $a`
arr[${#arr[@]}]=$r
done
echo 'Upload Success:'
for b in "${arr[@]}"
do
echo $b
done
}
1
2
file_path='D:\1.sh' 
$file_path
python中参数改为[1]
1
2
3
4
5
para="D:\\collection\\1.png"
cd \d:
cd \PyShell
python SDKts.py $para
exec /bin/bash

typora成功脚本

Typora中自定义命令上传图片 - 知乎 (zhihu.com)

如何使用typora适配自定义图片上传服务 - 掘金 (juejin.cn)

1
2
3
4
5
6
7
file_path='D:\PyShell\SDKts.py'
arr=()
echo 'Upload Success:'
for a in "$@"
do
python $file_path $a
done

Typora最终脚本

这个软件中文支持不好,最终决定自己返回url,让软件无法识别Success

1
2
3
4
5
6
7
8
#!/bin/bash
file_path='D:\PyShell\SDKts.py' arr=()
echo Upload Success11:
for a in "$@"
do
python $file_path $a
done
#exec /bin/bash

python最终脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

# -*- coding: utf-8 -*-
import oss2
import re

import pyperclip as cb



import os
import sys
# 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
auth = oss2.Auth('LTAI5tSUsnok7MdqfhSmyjz2', 'qbq8fdJXbjsJl2RknzTZCsN1gDathu')
# yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
# 填写Bucket名称。
bucket = oss2.Bucket(auth, 'https://oss-cn-shanghai.aliyuncs.com', 'bzjh')




# # 上传文件。
# # 如果需要在上传文件时设置文件存储类型(x-oss-storage-class)和访问权限(x-oss-object-acl),请在put_object中设置相关Header。
# # headers = dict()
# # headers["x-oss-storage-class"] = "Standard"
# # headers["x-oss-object-acl"] = oss2.OBJECT_ACL_PRIVATE
# # 填写Object完整路径和字符串。Object完整路径中不能包含Bucket名称。
# # result = bucket.put_object('exampleobject.txt', 'Hello OSS', headers=headers)
# result = bucket.put_object('exampleobject.txt', 'Hello OSS')
#
# # HTTP返回码。
# print('http status: {0}'.format(result.status))
# # 请求ID。请求ID是本次请求的唯一标识,强烈建议在程序日志中添加此参数。
# print('request_id: {0}'.format(result.request_id))
# # ETag是put_object方法返回值特有的属性,用于标识一个Object的内容。
# print('ETag: {0}'.format(result.etag))
# # HTTP响应头部。
# print('date: {0}'.format(result.headers['date']))
# para ="D:\\collection\\1.png"
def main(para):
# with open(para, 'rb') as fileobj:
# # Seek方法用于指定从第1000个字节位置开始读写。上传时会从您指定的第1000个字节位置开始上传,直到文件结束。
# fileobj.seek(1000, os.SEEK_SET)
# # Tell方法用于返回当前位置。
# current = fileobj.tell()
# # 填写Object完整路径。Object完整路径中不能包含Bucket名称。
# # bucket.put_object('img//img.png', fileobj)
regex = re.compile(r"\\")
name = para
name = regex.sub('/', name)
regex = re.compile(r"\:")
name = regex.sub('', name)
bucket.put_object_from_file('Typora_img/'+name , para)
url = "http://bzjh.oss-cn-shanghai.aliyuncs.com/Typora_img/" + name
# print("Upload Success:\n")
##print(url)
x="备份图片: "+para+"\n"+"![ "+name+"]("+url+")"
cb.copy(x) # 复制到剪切板
# print(cb.paste()) # 从剪切板粘贴(获取内容),并打印
##return url

# 通过sys.argv获得入参
#
main (sys.argv[1])

配合右键直接上传

shell

1
2
3
4
5
6
7
8
9
stty -echo
file_path='D:\PyShell\Common_upload.py' arr=()
#echo “Upload Success:”
for a in "$@"
do
python $file_path $a
done
#exec /bin/bash

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

# -*- coding: utf-8 -*-
import oss2
import re

import pyperclip as cb



import os
import sys
# 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
auth = oss2.Auth('LTAI5tSUsnok7MdqfhSmyjz2', 'qbq8fdJXbjsJl2RknzTZCsN1gDathu')
# yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
# 填写Bucket名称。
bucket = oss2.Bucket(auth, 'https://oss-cn-shanghai.aliyuncs.com', 'bzjh')




# # 上传文件。
# # 如果需要在上传文件时设置文件存储类型(x-oss-storage-class)和访问权限(x-oss-object-acl),请在put_object中设置相关Header。
# # headers = dict()
# # headers["x-oss-storage-class"] = "Standard"
# # headers["x-oss-object-acl"] = oss2.OBJECT_ACL_PRIVATE
# # 填写Object完整路径和字符串。Object完整路径中不能包含Bucket名称。
# # result = bucket.put_object('exampleobject.txt', 'Hello OSS', headers=headers)
# result = bucket.put_object('exampleobject.txt', 'Hello OSS')
#
# # HTTP返回码。
# print('http status: {0}'.format(result.status))
# # 请求ID。请求ID是本次请求的唯一标识,强烈建议在程序日志中添加此参数。
# print('request_id: {0}'.format(result.request_id))
# # ETag是put_object方法返回值特有的属性,用于标识一个Object的内容。
# print('ETag: {0}'.format(result.etag))
# # HTTP响应头部。
# print('date: {0}'.format(result.headers['date']))
# para ="D:\\collection\\1.png"
def main(para):
# with open(para, 'rb') as fileobj:
# # Seek方法用于指定从第1000个字节位置开始读写。上传时会从您指定的第1000个字节位置开始上传,直到文件结束。
# fileobj.seek(1000, os.SEEK_SET)
# # Tell方法用于返回当前位置。
# current = fileobj.tell()
# # 填写Object完整路径。Object完整路径中不能包含Bucket名称。
# # bucket.put_object('img//img.png', fileobj)
regex = re.compile(r"\\")
name = para
name = regex.sub('/', name)
regex = re.compile(r"\:")
name = regex.sub('', name)
bucket.put_object_from_file(name , para)
url = "http://bzjh.oss-cn-shanghai.aliyuncs.com/" + name

cb.copy(url) # 复制到剪切板
#print(cb.paste()) # 从剪切板粘贴(获取内容),并打印
##return url

# 通过sys.argv获得入参
#
main (sys.argv[1])