简体   繁体   English

如何在bash cli中将多个命令组合成单个?

[英]How to combine multiple commands into single for in bash cli?

For deleting the volumes ,first it should be taken into offline then deleted. 要删除卷,首先应将其置于脱机状态,然后删除。 Below is the command to do it. 以下是执行此操作的命令。

for i in {1..16}; do  vol --offline StressIO-v$i --force || break; done
for i in {1..16}; do  vol --delete StressIO-v$i || break; done

How to combine these commands and execute as single command? 如何组合这些命令并作为单个命令执行?

You can do all 16 in parallel, halting on first error, with GNU Parallel like this: 您可以并行执行所有16个操作,在第一个错误时暂停,使用GNU Parallel,如下所示:

parallel --halt now,fail=1 -k --dry-run 'vol --offline StressIO-v{} --force && vol --delete StressIO-v{}' ::: {1..16}

Sample Output 样本输出

vol --offline StressIO-v1 --force && vol --delete StressIO-v1
vol --offline StressIO-v2 --force && vol --delete StressIO-v2
vol --offline StressIO-v3 --force && vol --delete StressIO-v3
vol --offline StressIO-v4 --force && vol --delete StressIO-v4
vol --offline StressIO-v5 --force && vol --delete StressIO-v5
vol --offline StressIO-v6 --force && vol --delete StressIO-v6
vol --offline StressIO-v7 --force && vol --delete StressIO-v7
vol --offline StressIO-v8 --force && vol --delete StressIO-v8
vol --offline StressIO-v9 --force && vol --delete StressIO-v9
vol --offline StressIO-v10 --force && vol --delete StressIO-v10
vol --offline StressIO-v11 --force && vol --delete StressIO-v11
vol --offline StressIO-v12 --force && vol --delete StressIO-v12
vol --offline StressIO-v13 --force && vol --delete StressIO-v13
vol --offline StressIO-v14 --force && vol --delete StressIO-v14
vol --offline StressIO-v15 --force && vol --delete StressIO-v15
vol --offline StressIO-v16 --force && vol --delete StressIO-v16

If you like how that looks, remove the --dry-run and run it again for real. 如果您喜欢它的外观,请删除--dry-run并再次运行它。

If you are not worried about stopping on fail or the order of execution, you can simplify that right down to: 如果您不担心停止失败或执行顺序,您可以将其简化为:

parallel 'vol --offline StressIO-v{} --force && vol --delete StressIO-v{}' ::: {1..16}

You can check exit status of first offline comment and execute delete in single loop: 您可以检查第一个offline注释的退出状态,并在单个循环中执行delete

for i in {1..16}; do
    vol --offline StressIO-v$i --force && vol --delete StressIO-v$i || break
done

Put && in between these two commands. 把&&放在这两个命令之间。 This will execute both the commands in a single go. 这将在一次执行中执行这两个命令。

Otherwise create a script as vi example.sh 否则,创建一个脚本为vi example.sh

And paste the below lines in the script files as: 并将以下行粘贴到脚本文件中:

#!/bin/bash
for i in {1..16}; do  vol --offline StressIO-v$i --force || break; done
for i in {1..16}; do  vol --delete StressIO-v$i || break; done

Save this file by ESC+ wq! 通过ESC + wq保存此文件!

Then make it executable 然后使其可执行

chmod +x example.sh

Run this script as : 运行此脚本为:

./example.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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