python 如何呼叫帶引數的shell指令碼

2021-06-08 18:49:49 字數 2615 閱讀 1185

舉例:

shell的指令碼:

t.sh內容:

echo "this is a test shell with arguments"

echo "arg1 = $1; arg2 = $2;"

執行指令碼./t.sh zhao

結果如下:

[noncode@gnode108 knockdown_workflow]$ ./t.sh zhao1 zhao2

this is a test shell with arguments

arg1 = zhao1; arg2 = zhao2;

python指令碼:

[noncode@gnode108 knockdown_workflow]$ cat t.py 

#!/usr/bin/env python

import os

import sys

def main():

print 'hello world!'

if len(sys.argv) < 2 :

print "usage:%s config log" %(sys.argv[0])

sys.exit(1)

arg0 = sys.argv[0]

arg1 = sys.argv[1]

print "arg0 = %s; arg1 = %s" % (arg0, arg1)

print "test ./t.sh: "

os.system('./t.sh ' + arg0 + ' ' + arg1)

print "test method of replacing: "

t = 't.sh'

m = 'zhao'

n = 'zhao'

cmd = "./%s %s %s" % (t,m,n)

print "t = %s; m = %s; n = %s; cmd = %s" % (t,m,n,cmd)

os.system(cmd)

if __name__ == '__main__':

main()

執行指令碼:python t.py t.sh

執行結果:

[noncode@gnode108 knockdown_workflow]$ python t.py t.sh

hello world!

arg0 = t.py; arg1 = t.sh

test ./t.sh:

this is a test shell with arguments

arg1 = t.py; arg2 = t.sh;

test method of replacing:

t = t.sh; m = zhao; n = zhao; cmd = ./t.sh zhao zhao

this is a test shell with arguments

arg1 = zhao; arg2 = zhao;

[noncode@gnode108 knockdown_workflow]$ cat t.sh

echo "this is a test shell with arguments"

echo "arg1 = $1; arg2 = $2;"

[noncode@gnode108 knockdown_workflow]$ ./t.sh zhao1 zhao2

this is a test shell with arguments

arg1 = zhao1; arg2 = zhao2;

說明:兩種方法使用python指令碼呼叫shell指令碼:

第一種方法:

os.system('./t.sh ' + arg0 + ' ' + arg1)

注:./t.sh後面有乙個空格,不同的

第二種方法:

t = 't.sh'

m = 'zhao'

n = 'zhao'

cmd = "./%s %s %s" % (t,m,n)

print "t = %s; m = %s; n = %s; cmd = %s" % (t,m,n,cmd)

os.system(cmd)

注:在之前把字串聚合到一起。

其中連線字串還可以使用這個方法:

t = 't.sh'

m = 'zhao'

n = 'zhao'

cmd1 = os.path.join("./", t, ' ', m, ' ', n)

print "cmd1 = %s" % cmd1

os.system('./t.sh ' + arg1)

print "replace:"

t = 't.sh'

m = 'zhao'

cmd = "./%s %s" % (t,m)

print "t = %s; m = %s; cmd = %s" % (t,m,cmd)

os.system(cmd)

python帶引數打包exe及呼叫

1.舉個栗子 xx.py import sys arg1 sys.ar 1 arg2 sys.ar 2 print arg1 print arg2 平時執行python xx.py arg1 arg2 打包完成後執行xx.exe arg1 arg2 打包完成後執行後並獲得print輸出 output...

儲存過程如何呼叫帶引數的儲存過程

declare value varchar max exec tmfun getsearchstr 陝西 table1 name value output select from users where address value 顯示結果為 解釋 其中,tmfun getsearchstr 為儲存...

9 呼叫http api的姿勢 帶引數呼叫

func callapi s selector.selector 使用生成的pb檔案中的結構體作為引數封裝到請求體中 var resp models.prodlistresponse 這裡使用生成的response物件,客戶端只需要傳入這個就可以了,無需關心服務端返回什麼格式,因為服務端已經用rpc...