shell和python之间的参数传递
0. shell 参数的自身调用
shell参数的自身调用是通过'$'运算符实现的,比如,有如下脚本,脚本的名字为:run.sh:
###
echo $0 $1 $2
###
运行命令为:sh run.sh abc 123
则会输出:run.sh abc 123
由此可以看出sh命令后面的脚本名字为第一个参数($0),往后的参数为第二个和第三个($1,$2)
1.shell和Python之间的参数传递
shell和Python之间的参数传递用的是argparse模块,程序如下:(程序的名字为test.py)
###
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-id", "--input_dims",type=int,help="the dims of input")
parser.add_argument("-pl", "--pre_lr", type=float,help="the learning rate of pretrain")
parser.add_argument("-pe", "--pre_epochs", type=int,help="the epochs of pretrain")
parser.add_argument("-fl", "--fine_lr", type=float,help="the learning rate of finetune")
parser.add_argument("-fe", "--fine_epochs", type=int,help="the epochs of finetune")
parser.add_argument("-bs", "--batch_size", type=int,default=36,help="batch_size ")
parser.add_argument("-hls",'--hid_layer_size',type=int,nargs='+',help='network depth and size')
parser.add_argument("-an", "--attname",type=str,help="the name of features")
parser.add_argument("-dt", "--data_type",type=str,help="the type of data")
args = parser.parse_args()
print args
input_dims = args.input_dims
pre_lr = args.pre_lr
pre_epochs = args.pre_epochs
fine_lr = args.fine_lr
finetune_epochs = args.fine_epochs
bs = args.batch_size
attName = args.attname+"/"
hid_layers_size = args.hid_layer_size
dataType = args.data_type+'/'
###
shell的调用脚本如下:
###
inputDims=54
preLr=0.001
preEpochs=3
fineLr=0.1
fineEpochs=3
batchSize=36
hls1=60
hls2=80
hls3=60
attName='b_w_r_db_dw_dr'
dataType='9box_max'
#dataType='9box_max_over'
#dataType='9box_max_smote'
#dataType='1box_20'
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python '/media/ntfs-1/test.py' -id $inputDims -pl $preLr -pe $preEpochs -fl $fineLr -fe $fineEpochs -bs $batchSize -hls $hls1 $hls2 $hls3 -an $attName -dt $dataType
###
说明:
1. 在“parser.add_argument("-id", "--input_dims",type=int,help="the dims of input")”中,第一个参数“-id”,是在shell要输入的指定参数;第二个参数“--input_dims”是在Python程序中要使用的变量,“type=int”是参数的类型,help是参数的帮助信息。
2.命令:parser.add_argument("-hls",'--hid_layer_size',type=int,nargs='+',help='network depth and size')中,“nargs='+'”意思指的是参数可以为多个,从shell中"-hls $hls1 $hls2 $hls3"可以看到有3个参数,这三个参数都赋给了Python中的hid_layers_size变量,hid_layers_size 的类型为list
3.shell中定义变量的时候变量名,等号,变量的值之间不能有空格,否则会失败。
shell和python之间的参数传递的更多相关文章
- shell和matlab之间的参数传递
shell和matlab之间的参数传递比shell和Python之间的参数传递要简单,在matlab程序中(以.m脚本文件为例,其他程序如函数等未测试)不需要进行任何配置,直接使用即可,见下面 ...
- python调用shell, shell 引用python
python 调用 shell get_line_num="wc -l as_uniq_info | awk '{print $1}'" ###get the lines of & ...
- 萧墙HTML5手机发展之路(53)——jQueryMobile页面之间的参数传递
基于单个页面模板HTTP通过路POST和GET请求传递参数.在多页模板,并且不需要server沟通,通常有三种方式在多页模板来实现页面之间的参数传递. 1.GET道路:上一页页生成参数并传递到下一个页 ...
- python函数的参数传递问题---传值还是传引用?
摘要:在python中,strings, tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的对象.不可更改对象的传递属于传值,可更改对象属于传引用.想要在函数中传递 ...
- [转] C++ 和 python之间的互相调用
转载自:https://www.cnblogs.com/apexchu/p/5015961.html 一.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不 ...
- Ubuntu16.04系统中不同版本Python之间的转换
Ubuntu系统自带的版本是2.7.12 安装好python3.6之后,改变一下Python的优先级(需要root权限). 在使用下面这个命令查看电脑里面有几个Python版本 update-alte ...
- shell调用python脚本,并且向python脚本传递参数
1.shell调用python脚本,并且向python脚本传递参数: shell中: python test.py $para1 $para2 python中: import sys def main ...
- python中的参数传递和返回值
python中的参数传递类似java,有着自己的内存回收机制,这和C++有着很大的差别. 1.函数的参数传递: >>> a = [, , ] >>> def fun ...
- 使用shell调用python中的函数
最近遇到一个需求,需要通过shell调用python中的一个函数,发现其实也挺简单的: python脚本如下: test.py: import ConfigParser config = Config ...
随机推荐
- WP8.1通过StreamSocket连接C++服务器
注:当服务端和手机模拟器运行在一台机器时,会有奇怪错误.将服务端放在其它机器上更改客户端连接地址,运行正常.或者直接用本机modern调试也可以. 实例化一个对象 StreamSocket clien ...
- NetBeans配置Xdebug 远程调试PHP
1.配置PHP 说明:xdebug.trace_output_dir和xdebug.profiler_output_dir需要增加权限 #chmod 755 /usr/xdebug-tmp xdebu ...
- component to string 自定义窗体
component to string string to component StringToComponent ComponentToString ObjectTextToBinary Objec ...
- 配置Linux接收H3C路由器日志
(1)H3C Device上的配置 # 开启信息中心. <Sysname> system-view [Sysname] info-center enable # 配置发送日志信息到IP地 ...
- clock函数返回负值~ (转)
使用clock() 函数来进行计时,时不时的返回一个很大的负数,怎么检查也检查不出错误,现在找出错误原因,给大家分享一下. 来源网页:http://kebe-jea.blogbus.com/logs/ ...
- List扩展方法汇总(仅备注)
不管在c/s还是b/s的c#语言开发程序中,经常会用到List的扩展方法,尤其在json格式的数据和服务端交互越来越流行,很多时候总是在开发使用到的时候才去搜索有些扩展方法或者linq的用法,在这里, ...
- Uboot详细解析2
1.第二阶段的主线函数位于u-boot-2010.06/arch/arm/lib/board.c. 第二阶段的功能: <1> 初始化本阶段要使用到的硬件设备. 设置时钟.初始化串口. bo ...
- xml和configparser模块
一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, 但至今很多传统公司如金融行业的很多系统的接口还主要是xml. xml的格式如下,就是通过 ...
- 算法描述》关于LIS的nlogn方法
上次TYVJ有一道裸LIS,然而我当时直接打了一个N^2暴力就草草了事,然后就ZZ了,只拿了60分,其实NlogN的LIS和N^2的差的不多,只是没有N^2,好想罢了,鉴于某学弟的要求,所以就重现一下 ...
- mfs教程(二)
mfs文件系统(二) 编译和安装 MooseFS部署的首选方法是从源代码安装 源代码包安装支持标准./configure && make && make install ...