我们在使用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. 使用JAVA爬取博客里面的所有文章

    主要思路: 1.找到列表页. 2.找到文章页. 3.用一个队列来保存将要爬取的网页,爬取队头的url,如果队列非空,则一直爬取. 4.如果是列表页,则抽取里面所有的文章url进队:如果是文章页,则直接 ...

  2. BurpSuite—-decoder模块(编码模块)

    一.简介 Burp Decoder是Burp Suite中一款编码解码工具,将原始数据转换成各种编码和哈希表的简单工具,它能够智能地识别多种编码格式采用启发式技术. 二.模块说明 通过有请求的任意模块 ...

  3. AngularJS框架

    http://www.runoob.com/angularjs/angularjs-intro.html

  4. ParallaxEffect

    [ParallaxEffect] ParallaxEffect是一种用简单的2D贴图来模拟3D效果的简易方法.譬如一棵树,摄像机俯视时,当树远离摄像机时,树顶偏远,当树靠近,树顶偏近.苹果官方Adve ...

  5. Mysql 设置外部访问

    mysql> use mysql; mysql> GRANT ALL ON *.* TO user@' WITH GRANT OPTION; mysql -h172. -uuser -p1 ...

  6. shell 别名alias

    在这说下 shell   命令 alias 别名   看个人爱好 设置. 直接执行命令  显示当前所有别名 alias  别名='新的别名'  该命令在当窗口关闭以后 会失效  想要永久生效  需要在 ...

  7. Angular26 ng-content和ng-container、投影的使用

    1 准备工作 1.1 搭建angular环境 技巧01:本博文基于angular5 1.3 创建一个angular项目 技巧01:根据业务划分模块,每个模块都设定一个主组件 技巧02:利用路由实现模块 ...

  8. Python04 range()方法的使用、turtle.textinput()方法和write()的使用、turtle.numinput()的使用

    1 range() 方法的使用 1.1 range方法介绍 range方法会返回一个range类型的对象,该对象会根据range方法的参数产生一些列整型数据 技巧01:range方法有三个参数,第一个 ...

  9. Java Http 请求

    package zr.weixin.com.utils; import java.io.BufferedReader; import java.io.IOException; import java. ...

  10. laravel的mvc