user
user

Reputation: 503

Run commands using the output of one command and using them as input to the next

Based on the values in the second column, I need to run another command on these values.

[root@box ~]# for i in $(openstack server list --all -c ID -f value); do openstack server show $i -f value -c name -c project_id | xargs printf '%-60s %s\n'; done
cf1-0                                  ebf0f23f424c4417afd3e7bbf4e3900f
sf1-3                                  ebf0f23f424c4417afd3e7bbf4e3900f
sf1-2                                  ebf0f23f424c4417afd3e7bbf4e3900f
sf1-0                                  ebf0f23f424c4417afd3e7bbf4e3900f

Here's an example of what I'm trying to do.

[root@box ~]# for i in $(openstack server list --all -c ID -f value); do openstack server show $i -f value -c name -c project_id | xargs printf "%-60s eval(openstack project show %s -f value -c name)"; done
 cf1-0                                  eval(openstack project show ebf0f23f424c4417afd3e7bbf4e3900f -f value -c name)

The substitution from the second %s in printf works correctly, but I can't figure out how to execute the command.

The command needs to be executed in the same shell, and I cannot use environment variables.

Thanks.

** OUTPUT OF COMMANDS **

Here, -c var1 -c var2 can be placed in any order. -f just means print out the values, not the identifier.

[root@box~]# openstack server list --all -c Name -c ID -f value
2534ce5a-04da-4c7d-9ad5-b7bc466ae612 cf1-0

[root@box ~]# openstack server show 2534ce5a-04da-4c7d-9ad5-b7bc466ae612 -f value -c name -c project_id
cf1-0
ebf0f23f424c4417afd3e7bbf4e3900f

[root@box ~]# openstack project show ebf0f23f424c4417afd3e7bbf4e3900f -f value -c name
core

Desired output
===============
cf1-0 core

Upvotes: 0

Views: 357

Answers (2)

Inian
Inian

Reputation: 85895

Don't make things further convoluted by adding further levels of xargs. Recommend using a while loop reading the output at each stage and processing them further. The below works in bash with <(..) process substitution

#!/usr/bin/env bash

while read -r id instname; do
    proj="$(openstack server show "$id" -f value -c project_id)"
    [ -z "$proj" ] && break
    name="$(openstack project show "$proj" -f value -c name)"
    printf '%s %s\n' "$instname" "$name"
done < <(openstack server list --all -c ID -c Name -f value)

Upvotes: 2

Maverick
Maverick

Reputation: 156

Not very familiar with the openstack command, but a simple way of doing this in one line would be using awk with system function.

The command should look similar to this:

openstack server list --all -c ID -f value | awk -F'\t' '{ printf $1; system("eval openstack project show " $2 " -f value -c name") }'

Upvotes: 0

Related Questions