如何将python脚本的打印值作为命令重定向到linux终端

2024-04-23 12:19:54 发布

您现在位置:Python中文网/ 问答频道 /正文

from itertools import permutations
from itertools import combinations
import itertools
import random
import os
def split(arr, size):
     arrs = []
     while len(arr) > size:
         pice = arr[:size]
         arrs.append(pice)
         arr   = arr[size:]
     arrs.append(arr)
     return arrs
print("Enter the GCAP utility command:")
N=raw_input()
A= [N]
A1=[]
S7=[]
S6=[]
X=[]
X1=[]
m1=[]
G=0
A1=0
G1=[]
g1=[]
u=[]

def HELP():
   print('SYNTAX : ABC')

def comp():
    for x11, y11 in zip(Y, m1[G-1]):
        if x11 == y11:
            X1 = sorted(m1[G-1])
            A1= X1
    return A1

def my_containsAll(str1, Y):
    for c in Y:
        if c not in str1: return 0;
    return 1;

def mysplit(s):
    head = s.rstrip('0123456789')
    tail = s[len(head):]
    return head, tail

def imcon(u,U3):
     U1= ['imconfig','-i']
     U2=U1+u+U3
     return U2


if N == 'GCOA.01':     ## for first GCOA.01 command
    print("Enter the command in this format ")
    S=raw_input()
    print ("Enter the user interface for example : ")
    U= raw_input()
    for s in ['GCOA.01']:
              g = mysplit(s)
    g1.append('-c')
    g1.append(g[1])
    u.append(U)
    #print(g1)
    if S == "GCOA.01":   ## if no other parameter typed
        U4= imcon(u,g1)
        ss= ' '.join(U4)
        #print(ss)    
    else:
         S=S.split()
         S1 =[i.split('\t', 1)[0] for i in S] ## spliting the command into list
         #print(S1)
         G1= S1[0]
         #print(G1)
         for s in ['GCOA.01']:
              g = mysplit(s)
         #print(g[1])
         S11=S1[0]
         S5=[S11]
         del S1[0]
         for x in S1:   ## separate the parameters from the whole command
             print(x)
             X.append(x[:2])
         Y=list(X)
         S2=['-c','-a','-h']
         S12 =sorted(S2)
         str1 = ''.join(S12)
         #print(str1)
         A2= my_containsAll(str1, Y)
         if A2 == 1:
             #print(S2)
             S3=[]
             for i in range(1, len(S2)+1):    ## creating the permutation and combinations of the parameters
                  S3.extend(itertools.combinations(S2, i))
                  if i >= 1:
                       for k in S3:
                            for c in permutations(k):
                                 S6.append(c)
                            for l in S6:
                                 if l not in S7:
                                      S7.append(l)
            # print(S7)
             for m in S7:   ## creating the list of the different possible combination of P&C
                  m=list(m)
                  m1.append(m)
             #print (m1) 
             for i1 in m1:  ## for finding the index of the parameters given by the user from the P&C list of parameters
                  G=G+1
                  if Y == i1:
                          #print (G)
                          break;
             g1=g1+S1
             #print(g1)           
             if Y == m1[0]:
                  R=imcon(u,g1)
                  ss=' '.join(R)
                  print(ss)    ## this print are basically command which is to direct on terminal window
             elif Y == m1[1]:
                  R=imcon(u,g1)
                  ss=' '.join(R)
                  print(ss)

         else:
            HELP()

输出: imconfig-i th0-c 01-h-a

示例:(在命令提示符中)

c:/python34> python ssm9.py
 imconfig -i th0 -c 01 - h -a

c:/python34> imconfig -i th0 -c 01 -h -a 
/// i want something like that means the above printed value redirect here. but in linux terminal window

Tags: theinimportforreturnifdefss
2条回答

我写了这样一个示例代码

vivek@vivekUbuntu:~$ cat sample.py
#! /usr/bin/python
import socket
hostname = socket.gethostname()

def getPingCommand(host):
    return "ping %s" %host

print getPingCommand(hostname)

然后像这样执行:

vivek@vivekUbuntu:~$ `python sample.py`
PING vivekUbuntu (127.0.1.1) 56(84) bytes of data.
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=1 ttl=64 time=0.020 ms
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=2 ttl=64 time=0.027 ms
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=3 ttl=64 time=0.050 ms
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=4 ttl=64 time=0.050 ms
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=5 ttl=64 time=0.049 ms
^C
 - vivekUbuntu ping statistics  -
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 0.020/0.039/0.050/0.013 ms
vivek@vivekUbuntu:~$

甚至S0III0s提到的选项也是正确的。这对我很有用。你知道吗

`python ssm9.py`

或者

$(python ssm9.py)

或者

eval `python ssm9.py`

或者

eval $(python ssm9.py)

应该都管用。你知道吗

(你可以这样试试:)

> $(echo "echo test")
test

相关问题 更多 >