简体   繁体   中英

Accessing python script return value from bash script as an array

I have a bash command that is as below:

($(echo my-profiles --my-profiles pytest))

The above bash command returns an array from the python script as below:

['mock-alchemy', 'pytest-mock', 'pytest-datafixtures', 'pytest-describe', 'pytest-unordered', 'requests-mock']

From the answer of this example, I am accessing the return values as below:

dependencies=($(echo /path/to/my-script.py --my-profiles pytest}))

When I access the return values from dependencies , I get the following results:

> echo ${dependencies[0]}
my-profiles

How can I get 'mock-alchemy' instead of the above result?

My python script is as follows:

my-script.py
def main():
    parser = argparse.ArgumentParser(description='My script')
    parser.add_argument('--tox-profiles', dest="profiles", 
                        type=str,
                        default='')
    parsed_args = parser.parse_args()
    dependencies = get_dependencies(args.profiles)

def get_dependencies(profiles):
    return ['mock-alchemy', 'pytest-mock', 'pytest-datafixtures', 'pytest-describe', 'pytest-unordered', 'requests-mock']

($(echo my-profiles --my-profiles pytest))

(... ) execute a subshell. Then $(... ) executes echo . Command echo outputs my-profiles --my-profiles pytest . Then the result of $(...) undergoes word splitting expansion and my-profiles --my-profiles pytest is split into 3 words my-profiles --my-profiles pytest . Then the result of splitting is like "rescanned" and becomes a new command to execute, so my-profiles is executed. Which actually outputs the output of your program.

This is all convoluted. Just run my-profiles --my-profiles pytest , what is echo doing there?

 dependencies=($(echo my-profiles --my-profiles pytest))

dependencies=(... ) is an array assignment. First, $(...) is replaced by the output of command inside. echo my-profiles --my-profiles pytest outputs my-profiles --my-profiles pytest . Then the result my-profiles --my-profiles pytest is word split, and becomes 3 words, which are assigned each to a separate array element.

How can I get 'mock-alchemy' instead of the above result?

You should output the data from your tool in machine-readable format if you intent to use them by machines. Because they are quite easy, you could output for example space separated data.

def get_dependencies(profiles):
    return ' '.join([
       'mock-alchemy', 'pytest-mock', 'pytest-datafixtures', 'pytest-describe', 'pytest-unordered', 'requests-mock'
       ])

# actually print it too!
print(get_dependencies(None))

Then you could read space separated values in bash:

output=$(my-profiles --my-profiles pytest)
IFS=' ' read -r -a arr <<<"$output"
declare -p arr
echo "${arr[0]}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM