0

I have a piece of python code that looks as following:

import subprocess

function = """my_function () \{
echo "test"
\}

"""
alias = 'alias my-function="my_function"'
command = "my-function"
process = subprocess.Popen([function,alias,command],
         stdout=subprocess.PIPE,
         stderr=subprocess.PIPE, shell = True)
stdout, stderr = process.communicate()
print(stderr.decode())

I would like to define my_function and call it using the command my-function

The abovementioned program prints out /bin/sh: 3: }: not found, meaning that it doesn't recognize the supplied closing bracket sign }. How can I properly define and call this function in this manner?

0

1 Answer 1

0

I think you want something like this:

import subprocess

function = 'my_function(){ echo test; }'
alias = 'alias myFunc=my_function'
cmd = 'myFunc'

# Make combined set of commands
commands = f'''
{function}
{alias}
{cmd}'''

# Run them
process = subprocess.check_output(commands,shell = True)

print(process)
b'test\n'
Sign up to request clarification or add additional context in comments.

2 Comments

Aliases are not normally expanded in noninteractive shells. The alias appears to serve absolutely no useful purpose here anyway. Simply name the function myFunc if that's what you want its name to be.
@triplee I agree - I only kept the alias because OP wanted to use one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.