如何处理从动态命令到创建目录的 For 循环中的白色 Space

How To Handle White Space in Forloop from Dynamic Command to Create Directories

所以我有一个 for 循环,可以输出我想要的信息(OpenStack 项目列表),我希望它为我创建目录。然而,有时,人们用空格命名他们的项目,我需要考虑到这一点。这是我的命令的输出示例:

openstack project list
+----------------------------------+-----------------------------------------+
| ID                               | Name                                    |
+----------------------------------+-----------------------------------------+
| 0c1abff4e920448ba86638bd0d78f7ca | testing_grounds                         |
| 0dc5bed65a6b4a168c7531b1b4972e95 | Michaud                                 |
| 0eab4c57a6f34c01a0de65f433c1539c | Ansible Automation                      |
| 13d98b9d994c40029dcd2e9e4e38a26d | Sustaining Development                  |
| 2ec59a3a665946f4b9d5098d8562ebbb | Data Center Services                    |
| 379e46cbe60d420f977bf939ceed6961 | MF-DevStack                             |
| 63cecde0c0384e18a949247f03b8c490 | Kubernetes Labs                         |
| 677edd03e4ce439d884b017db19efd03 | mso-ems                                 |
| 6865e32d9f8646b6aa449f440f1a1bb1 | Network Development                     |
| 6a239dc6ce9c4fdca4ba2ef38849fa3b | Data Engineering                        |
| 71ceb6e4c5f14f168ec18c069b636c02 | Data Center Operations Service Scanning |
| 785f0b62484549048e6772e058bb0eb6 | admin                                   |
| 786b43df2b79470ea14da7468f9df1cc | service                                 |
| 7ca98362cb98428e9ad94c880f3efcae | OpenStack-Example                       |
| 89498a55aa374fb4915c5a08b903fc23 | Petes-place                             |
| 92c3fabbddb54e1696cffa223655c5d9 | Image Factory                           |
| 9f342bd754e748e7a20bd383eab9f47d | Network Automation                      |
| bdcba2785f334af5a2379f79d77f1308 | Fault and Performance Management        |
| cab8e3e810c24f29ace13ca3771cc41c | Microseg                                |
| cb41e6dea9744744a304c4ab77719062 | Development                             |
| ccf5970b1aa74f9ebb7a542b40110462 | Docker Enterprise                       |
| eb7d98031ec0407fb208b537a6888be5 | Application Architecture                |
| f33c8dd6acfc467aa05215a5fe8e7798 | casa-de-pedro                           |
+----------------------------------+-----------------------------------------+

我的命令允许我实际仅为此指定名称:

openstack project list -c Name -f value

看起来像这样:

testing_grounds
Michaud
Ansible Automation
Sustaining Development
Data Center Services
MF-DevStack
Kubernetes Labs
mso-ems
Network Development
Data Engineering
Data Center Operations Service Scanning
admin
service
OpenStack-Example
Petes-place
Image Factory
Network Automation
Fault and Performance Management
Microseg
Development
Docker Enterprise
Application Architecture
casa-de-pedro

所以我考虑制作一个脚本来收集每个项目的所有资源,并将资源存储在以项目名称标记的目录中。所以我考虑包装一个 for 循环以创建以项目名称命名的目录。我尝试了以下方法,但没有奏效:

for i in $(openstack project list -c Name -f value); do mkdir -p -- 'echo $i'; done
for i in $(openstack project list -c Name -f value); do mkdir -p -- "$i"; done
for i in $(openstack project list -c Name -f value); do mkdir -p -- $i; done

none 似乎真的有效,而不是将它封装在一个字符串中,它不起作用,而是仍然将名称分开,如 Network Development 变成 NetworkDevelopment。我怎样才能让它工作,以便它能解释项目名称中可能存在的任何空格。

很遗憾,我无法使用常规的 for 循环语法来实现此目的。相反,我不得不使用 while 循环(非常感谢 Jetchisel 帮助我实现了这个结果)。 以下方法对我有用:

[openstack_backup]$ while IFS= read -r name; do mkdir -p "$name"; done < <(openstack project list -c Name -f value)
[openstack_backup]$ ls
admin                     Data Center Operations Service Scanning  Docker Enterprise                 MF-DevStack  Network Automation   service
Ansible Automation        Data Center Services                     Fault and Performance Management  Michaud      Network Development  Sustaining Development
Application Architecture  Data Engineering                         Image Factory                     Microseg     OpenStack-Example    testing_grounds
casa-de-pedro             Development                              Kubernetes Labs                   mso-ems      Petes-place

你最好以 JSON 格式输出并用 jq

解析它

openstack project list --long -f json

或者使用更合适的语言,例如 python。