Use python to execute shell scripts and dynamically pass parameters

Method 1 :
Script (2.sh):

#!/usr/bin/env sh
root_passwd=$1
echo $root_passwd

function main()
{
    
    
  echo "hello shell"
}

main

Python program (sus.py):

import subprocess
p = subprocess.Popen(args=['./2.sh','888'],shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
cc = p.stdout.read().strip()
dd = bytes.decode(cc)
print(dd)
print(type(dd))

print:

[root@localhost demo01]# python3 sus.py 
888
hello shell
<class 'str'>

Popen usage source code explanation

class Popen(object):

    _child_created = False  # Set here since __del__ checks it

    def __init__(self, args, bufsize=-1, executable=None,
                 stdin=None, stdout=None, stderr=None,
                 preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
                 shell=False, cwd=None, env=None, universal_newlines=False,
                 startupinfo=None, creationflags=0,
                 restore_signals=True, start_new_session=False,
                 pass_fds=()):

The parameters are:

args should be a string or a series of program parameters. The program to be executed is usually the first item in the args sequence or string, but it can be explicitly set using executable parameters.

On UNIX, with shell=False (default): In this case, the POPEN class uses os.execvp() to execute the subroutine. args should usually be a sequence. A string will be treated as a sequence of strings as the only item (program to be executed).

On UNIX, use shell=True: if args is a string, it specifies the command string to be executed through the shell. If args is a sequence, the first item specifies the command string, and any other items will be treated as additional shell parameters.
The script cannot get the parameters and the shell parameter value and the args parameter value are related.
Method 2:
Script (2.sh):

#!/usr/bin/env sh
root_passwd=$1

function main()
{
    
    
  echo $root_passwd
  return 1
}

main

Python program (sus.py):

import subprocess
path = './2.sh'
pwd = 'mima'
cmd = path + ' ' + pwd
a,b=subprocess.getstatusoutput(cmd)
print(a)
print(b)

print:

[root@localhost demo01]# python3 sus.py 
1
mima

Getstatusoutput usage source code explanation

getstatusoutput(cmd):
    Return (status, output) of executing cmd in a shell.

    Execute the string 'cmd' in a shell with 'check_output' and
    return a 2-tuple (status, output). Universal newlines mode is used,
    meaning that the result with be decoded to a string.

    A trailing newline is stripped from the output.
    The exit status for the command can be interpreted
    according to the rules for the function 'wait'.  Example:

    >>> subprocess.getstatusoutput('ls /bin/ls')
    (0, '/bin/ls')
    >>> subprocess.getstatusoutput('cat /bin/junk')
    (256, 'cat: /bin/junk: No such file or directory')
    >>> subprocess.getstatusoutput('/bin/junk')
    (256, 'sh: /bin/junk: not found')

Guess you like

Origin blog.csdn.net/qq_34663267/article/details/112600433