我们在使用shell调用其他语言的程序的时候,希望能够便捷的从shell中输入参数,然后由目标程序接收参数并运行,这样就省去了每次需要在原程序进行修改带来的麻烦,这里介绍一下如何从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之间的参数传递的更多相关文章

  1. shell和matlab之间的参数传递

        shell和matlab之间的参数传递比shell和Python之间的参数传递要简单,在matlab程序中(以.m脚本文件为例,其他程序如函数等未测试)不需要进行任何配置,直接使用即可,见下面 ...

  2. python调用shell, shell 引用python

    python 调用 shell get_line_num="wc -l as_uniq_info | awk '{print $1}'" ###get the lines of & ...

  3. 萧墙HTML5手机发展之路(53)——jQueryMobile页面之间的参数传递

    基于单个页面模板HTTP通过路POST和GET请求传递参数.在多页模板,并且不需要server沟通,通常有三种方式在多页模板来实现页面之间的参数传递. 1.GET道路:上一页页生成参数并传递到下一个页 ...

  4. python函数的参数传递问题---传值还是传引用?

    摘要:在python中,strings, tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的对象.不可更改对象的传递属于传值,可更改对象属于传引用.想要在函数中传递 ...

  5. [转] C++ 和 python之间的互相调用

    转载自:https://www.cnblogs.com/apexchu/p/5015961.html 一.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不 ...

  6. Ubuntu16.04系统中不同版本Python之间的转换

    Ubuntu系统自带的版本是2.7.12 安装好python3.6之后,改变一下Python的优先级(需要root权限). 在使用下面这个命令查看电脑里面有几个Python版本 update-alte ...

  7. shell调用python脚本,并且向python脚本传递参数

    1.shell调用python脚本,并且向python脚本传递参数: shell中: python test.py $para1 $para2 python中: import sys def main ...

  8. python中的参数传递和返回值

    python中的参数传递类似java,有着自己的内存回收机制,这和C++有着很大的差别. 1.函数的参数传递: >>> a = [, , ] >>> def fun ...

  9. 使用shell调用python中的函数

    最近遇到一个需求,需要通过shell调用python中的一个函数,发现其实也挺简单的: python脚本如下: test.py: import ConfigParser config = Config ...

随机推荐

  1. 关于Unity发布iOS平台代码混淆问题

    之前在越狱手机里找到<永恒战士3>的程序发现是用Unity做的,拷出资源出来看的时候发现里面有游戏程序集,立马抽出来反编译了一下,发现里面的代码只有方法签名,没有方法体,还以为用什么高端混 ...

  2. Python Twisted系列教程10:增强defer功能的客户端

    作者:dave@http://krondo.com/an-introduction-to-asynchronous-programming-and-twisted/ 译者:杨晓伟(采用意译) 可以从这 ...

  3. Python特殊语法:filter、map、reduce、lambda

    filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决 ...

  4. ADO.NET主要组件

  5. Sql Server 2005如何导入DBF文件?

    提问者采纳   select * into 要生成的SQL表名 from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','dBase IV;HDR=NO;IMEX=2;DA ...

  6. MYSQL中str_to_date函数的用法

    str_to_date(str,format) 函数的用法 str_to_date函数将str转化为日期型的数据,format表示转化后的格式. format参数格式: 常用: %Y  年 %m  月 ...

  7. webService调用模式比较

  8. OK6410&nbsp;tftp下载内核、文件系…

    飞凌官方提供了一键下载烧写linux的方式,相对来说比较方便,但是对于开发来说不够灵活,因此这篇文章把tftp相关的点介绍一下,整理下其中遇到的一些问题. 一键烧写本质上是启动位于SD卡中的Uboot ...

  9. conda create -n AlphaPose2018 python=2.7

    conda create -n AlphaPose2018 python=2.7Solving environment: done ==> WARNING: A newer version of ...

  10. CSS变量教程

    今年三月,微软宣布 Edge 浏览器将支持 CSS 变量. 这个重要的 CSS 新功能,所有主要浏览器已经都支持了.本文全面介绍如何使用它,你会发现原生 CSS 从此变得异常强大. 一.变量的声明 声 ...