shell 学习

shell 学习指南

#!/usr/bin/env python3
env 指向环境变量,从设置环境变量里查找 python3编辑器
参考于https://www.cnblogs.com/oxspirt/p/8185550.html
1.shell调用python脚本,并且向python脚本传递参数:

shell中:

python test.py $para1 $para2


python中:

import sys


def main($canshu1, $canshu2)

  .....



main(sys.argv[1], sys.argv[2])

复制代码
2.使用shell调用python中的函数:

python脚本如下:

test.py:

复制代码

import ConfigParser  
  
config = ConfigParser.ConfigParser()  
config.read("test.conf")  
  
def get_foo():  
    return config.get("locations", "foo")  
  
def get_bar():  
    return config.get("locations", "bar")  

我想通过shell调用里面的get_foo,只需要在shell中执行一个调用的命令行即可:

python -c 'import test; print test.get_foo()'  

-c选项只是告诉python来执行一些python命令。

为了将结果存储在变量中,你可以因此这样做:

RESULT_FOO=`python -c 'import test; print test.get_foo()'`

或者,等效于:

RESULT=$(python -c 'import test; print test.get_foo()')  

我们也可以一次调用所有方法,放入一个集合中,再调用切割方法获取相应的值:

ALL_RESULTS=$(python -c 'import test; print test.get_foo(), test.get_bar()')  

如果需要第二个结果,并将其放入RESULT_BAR:

 RESULT_BAR=$(echo $ALL_RESULTS | cut -d' ' -f2)  

本文转载自:https://blog.csdn.net/tanlon_0308/article/details/40423751

shell常用命令

vim test.sh

sh test.sh

chmod +x test.sh
./test.sh

sh -x test.sh# 加 -x 显示运行过程

#! /bin/bash
a=1
b=2
c=$[ $a + $b ]
echo "sum is $c "   #echo相当于打印

echo"从键盘中输入一个 x 值
read x 
echo"从键盘中输入一个 y 值
read y
sum = $[ $x + $y]
echo" sum is $sum"

# $0 指向执行脚本本身
# $1,$2 预设置变量1,2
vim test.sh 1 2
sum $[ $1+$2]

转载于https://www.cnblogs.com/zhang-jun-jie/p/9266858.html,更多功能学习请前往

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值